SpringBoot整合springsecurity

缺乏、安全感 2023-05-21 08:57 107阅读 0赞

目录

  • 一、SpringBoot整合SpringSecurity

    • 1、pom文件
    • 2、静态资源
    • 3、MySecurityController
    • 4、自定义SpringSecurity的配置类(重点)

1、pom文件

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.thymeleaf.extras</groupId>
  4. <artifactId>thymeleaf-extras-springsecurity5</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-security</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-web</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-devtools</artifactId>
  21. <scope>runtime</scope>
  22. <optional>true</optional>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. <exclusions>
  29. <exclusion>
  30. <groupId>org.junit.vintage</groupId>
  31. <artifactId>junit-vintage-engine</artifactId>
  32. </exclusion>
  33. </exclusions>
  34. </dependency>
  35. </dependencies>

2、静态资源

跳转到目录
在这里插入图片描述
1.html, 其他x.html都一样
在这里插入图片描述
welcome.html

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <h1 align="center">欢迎光临武林秘籍管理系统</h1>
  9. <div sec:authorize="!isAuthenticated()">
  10. <h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/userlogin}">请登录</a></h2>
  11. </div>
  12. <div sec:authorize="isAuthenticated()">
  13. <h2><span sec:authentication="name"></span>,您好,您的角色有:
  14. <span sec:authentication="principal.authorities"></span></h2>
  15. <form th:action="@{/logout}" method="post">
  16. <input type="submit" value="注销"/>
  17. </form>
  18. </div>
  19. <hr>
  20. <div sec:authorize="hasRole('vip1')">
  21. <h3>普通武功秘籍</h3>
  22. <ul>
  23. <li><a th:href="@{/level1/1}">罗汉拳</a></li>
  24. <li><a th:href="@{/level1/2}">武当长拳</a></li>
  25. <li><a th:href="@{/level1/3}">全真剑法</a></li>
  26. </ul>
  27. </div>
  28. <div sec:authorize="hasRole('vip2')">
  29. <h3>高级武功秘籍</h3>
  30. <ul>
  31. <li><a th:href="@{/level2/1}">太极拳</a></li>
  32. <li><a th:href="@{/level2/2}">七伤拳</a></li>
  33. <li><a th:href="@{/level2/3}">梯云纵</a></li>
  34. </ul>
  35. </div>
  36. <div sec:authorize="hasRole('vip3')">
  37. <h3>绝世武功秘籍</h3>
  38. <ul>
  39. <li><a th:href="@{/level3/1}">葵花宝典</a></li>
  40. <li><a th:href="@{/level3/2}">龟派气功</a></li>
  41. <li><a th:href="@{/level3/3}">独孤九剑</a></li>
  42. </ul>
  43. </div>
  44. </body>
  45. </html>

login.html

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <h1 align="center">欢迎登陆武林秘籍管理系统</h1>
  9. <hr>
  10. <div align="center">
  11. <form th:action="@{/userlogin}" method="post">
  12. 用户名:<input name="user"/><br>
  13. 密码:<input name="pwd"><br/>
  14. <input type="checkbox" name="remeber"> 记住我<br/>
  15. <input type="submit" value="登陆">
  16. </form>
  17. </div>
  18. </body>
  19. </html>

3、MySecurityController

跳转到目录

  1. @Controller
  2. public class MySecurityController {
  3. private final String PREFIX = "pages/";
  4. /** * 欢迎页 * * @return */
  5. @GetMapping("/")
  6. public String index() {
  7. return "welcome";
  8. }
  9. /** * 登陆页 * * @return */
  10. @GetMapping("/userlogin")
  11. public String loginPage() {
  12. return PREFIX + "login";
  13. }
  14. /** * level1页面映射 * * @param path * @return */
  15. @GetMapping("/level1/{path}")
  16. public String level1(@PathVariable("path") String path) {
  17. return PREFIX + "level1/" + path;
  18. }
  19. /** * level2页面映射 * * @param path * @return */
  20. @GetMapping("/level2/{path}")
  21. public String level2(@PathVariable("path") String path) {
  22. return PREFIX + "level2/" + path;
  23. }
  24. /** * level3页面映射 * * @param path * @return */
  25. @GetMapping("/level3/{path}")
  26. public String level3(@PathVariable("path") String path) {
  27. return PREFIX + "level3/" + path;
  28. }
  29. }

4、自定义SpringSecurity的配置类(重点)

跳转到目录

  1. @EnableWebSecurity //开启WebSecurity模式
  2. public class MySecurityConfig extends WebSecurityConfigurerAdapter {
  3. @Override
  4. protected void configure(HttpSecurity http) throws Exception {
  5. //super.configure(http);
  6. //定制请求的授权规则
  7. // 不同的角色可以访问的权限不同
  8. http.authorizeRequests().antMatchers("/").permitAll()
  9. .antMatchers("/level1/**").hasRole("vip1")
  10. .antMatchers("/level2/**").hasRole("vip2")
  11. .antMatchers("/level3/**").hasRole("vip3");
  12. //开启自动配置的登陆功能,效果,如果没有登陆,没有权限就会来到登陆页面
  13. //1、/login来到登陆页
  14. //2、重定向到/login?error表示登陆失败
  15. //3、更多详细规定
  16. //4、默认post形式的 /login代表处理登陆
  17. //5、一但定制loginPage;那么 loginPage的post请求 /userlogin就代表处理登陆
  18. http.formLogin().usernameParameter("user").passwordParameter("pwd")
  19. .loginPage("/userlogin");
  20. //开启自动配置的注销功能。
  21. //1、访问 /logout 表示用户注销,清空session
  22. //2、注销成功会返回 /login?logout 页面;
  23. http.logout().logoutSuccessUrl("/");//设置注销成功以后来到首页
  24. //开启记住我功能
  25. //登陆成功以后,将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登录
  26. //点击注销会删除cookie
  27. http.rememberMe().rememberMeParameter("remeber");
  28. }
  29. //定义认证规则
  30. // 需要对密码进行加密 PasswordEncoder
  31. @Override
  32. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  33. // 这些数据正常应该从数据库中读
  34. // 给不同的用户设置不同的角色
  35. auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
  36. .withUser("zhangsan").password(new BCryptPasswordEncoder().encode("123")).roles("vip2", "vip3")
  37. .and()
  38. .withUser("lisi").password(new BCryptPasswordEncoder().encode("123")).roles("vip1")
  39. .and()
  40. .withUser("zy").password(new BCryptPasswordEncoder().encode("123")).roles("vip1", "vip2", "vip3");
  41. }
  42. }

发表评论

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

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

相关阅读