SpringBoot整合SpringSecurity

╰+攻爆jí腚メ 2021-06-24 16:12 699阅读 0赞

1.添加相关依赖。

  1. <!--Spring Security和thymeleaf的整合依赖-->
  2. <dependency>
  3. <groupId>org.thymeleaf.extras</groupId>
  4. <artifactId>thymeleaf-extras-springsecurity4</artifactId>
  5. </dependency>
  6. <!--thymeleaf模板引擎-->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  10. </dependency>
  11. <!--Security安全框架-->
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-security</artifactId>
  15. </dependency>

需要注意的是,这里使用了thymeleaf模板引擎来进行前端页面数据交互,默认的thymeleaf版本太低,需要将它修改为更高版本,否则访问页面将会报错。在properties元素中修改版本。

  1. <properties>
  2. <java.version>1.8</java.version>
  3. <!--默认thymeleaf版本太低,需要设置为更高版本-->
  4. <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
  5. <thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
  6. <thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-extras-springsecurity4.version>
  7. </properties>

2.创建控制页面跳转的controller。

  1. @Controller
  2. public class KungfuController {
  3. private final String PREFIX = "pages/";
  4. /**
  5. * 欢迎页
  6. * @return
  7. */
  8. @GetMapping("/")
  9. public String index() {
  10. return "welcome";
  11. }
  12. /**
  13. * 登陆页
  14. * @return
  15. */
  16. @GetMapping("/userLogin")
  17. public String loginPage() {
  18. return PREFIX+"login";
  19. }
  20. /**
  21. * level1页面映射
  22. * @param path
  23. * @return
  24. */
  25. @GetMapping("/level1/{path}")
  26. public String level1(@PathVariable("path")String path) {
  27. return PREFIX+"level1/"+path;
  28. }
  29. /**
  30. * level2页面映射
  31. * @param path
  32. * @return
  33. */
  34. @GetMapping("/level2/{path}")
  35. public String level2(@PathVariable("path")String path) {
  36. return PREFIX+"level2/"+path;
  37. }
  38. /**
  39. * level3页面映射
  40. * @param path
  41. * @return
  42. */
  43. @GetMapping("/level3/{path}")
  44. public String level3(@PathVariable("path")String path) {
  45. return PREFIX+"level3/"+path;
  46. }
  47. }

3.创建对应映射路径的前端页面,这里省略详细创建过程。
4.创建SpringSecurity的配置类,该类必须继承WebSecurityConfigurerAdapter,并且添加注解**@EnableWebSecurity开启WebSecurity,重写configure(HttpSecurity http)方法定制授权规则,重写configure(AuthenticationManagerBuilder auth)**方法定义认证规则。

  1. @EnableWebSecurity
  2. public class MySecurityConfig extends WebSecurityConfigurerAdapter {
  3. @Override
  4. protected void configure(HttpSecurity http) throws Exception {
  5. //super.configure(http);
  6. //定制请求的授权规则
  7. http.authorizeRequests().antMatchers("/").permitAll()//index页面允许所有人访问
  8. .antMatchers("/level1/**").hasRole("VIP1")//level1下面的所有页面必须是VIP1才能访问
  9. .antMatchers("/level2/**").hasRole("VIP2")//level2下面的所有页面必须是VIP2才能访问
  10. .antMatchers("/level3/**").hasRole("VIP3");//level3下面的所有页面必须是VIP3才能访问
  11. //开启自动配置登录功能,如果没有权限,就会来到登录页面
  12. //http.formLogin();
  13. // /login来到登录页
  14. //重定向到/login?error表示登录失败
  15. //更多详细规则查看Spring官网
  16. //定制登录页
  17. //默认post形式的/login代表处理登录
  18. //一旦定制loginpage,那么loginPage的post请求就是登录
  19. http.formLogin().usernameParameter("user").passwordParameter("pwd")
  20. .loginPage("/userLogin");
  21. //.loginProcessingUrl("");//自定义登录url,默认是loginPage中的url
  22. //开启自动配置的注销功能
  23. //http.logout();
  24. //访问/logout表示用户注销,清空Session
  25. http.logout().logoutSuccessUrl("/");//注销成功以后来到首页
  26. //开启记住我功能
  27. //http.rememberMe();
  28. //登录成功后,将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登录
  29. //点击注销会删除cookie
  30. //定制登录页时,可以指定记住我的checkbox的name属性,默认叫rememberMe
  31. http.rememberMe().rememberMeParameter("remeber");
  32. }
  33. @Override
  34. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  35. //super.configure(auth);
  36. //定义认证规则(用户名、密码,权限)
  37. auth.inMemoryAuthentication()
  38. .withUser("zhangsan").password("123456").roles("VIP1","VIP2")
  39. .and()
  40. .withUser("lisi").password("123456").roles("VIP2","VIP3")
  41. .and()
  42. .withUser("wangwu").password("123456").roles("VIP1","VIP3");
  43. }
  44. }

SpringSecurity的更多详细规则可以查看官网:https://spring.io/projects/spring-security

发表评论

表情:
评论列表 (有 0 条评论,699人围观)

还没有评论,来说两句吧...

相关阅读