当前位置:   article > 正文

spring boot(学习笔记第十二课)

spring boot(学习笔记第十二课)

spring boot(学习笔记第十二课)

  • Spring Security内存认证,自定义认证表单

学习内容:

  1. Spring Security内存认证
  2. 自定义认证表单

1. Spring Security内存认证

  1. 首先开始最简单的模式,内存认证。
    • 加入spring security的依赖。
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-security</artifactId>
      </dependency>
      
      • 1
      • 2
      • 3
      • 4
    • 加入controller进行测试。
       @GetMapping("/security_hello")
       @ResponseBody
          public String hello(){
              return "hello,security";
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
    • 启动应用程序。
      默认的用户名是user,密码会在log中出现。
      Using generated security password: 9b7cd16e-af9e-4804-a6a2-9303df66ace8
      
      • 1
    • 访问controller,可以看到这里在这里插入图片描述 * 输入上面的密码,进行login。
      在这里插入图片描述
  2. 接着开始在内存中定义认证的用户和密码。
    • 定义内存用户,设定安全设置@configuration
      @Configuration
      public class SecurityConfig {
          @Bean
          PasswordEncoder passwordEncoder() {
              return NoOpPasswordEncoder.getInstance();
          }
      
          @Bean
          UserDetailsService userDetailsService() {
              InMemoryUserDetailsManager users =
                      new InMemoryUserDetailsManager();
              users.createUser(User.withUsername("finlay_admin")
                      .password("123456")
                      .roles("ADMIN")
                      .build());
              users.createUser(User.withUsername("finlay_dba")
                      .password("123456")
                      .roles("DBA")
                      .build());
              users.createUser(User.withUsername("finlay_super")
                      .password("123456")
                      .roles("ADMIN", "DBA")
                      .build());
              return users;
          }
      
          @Bean
          SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
              http.authorizeHttpRequests(auth -> auth.requestMatchers
                                      ("/**")//匹配所有/** url
                              .hasRole("ADMIN")//定义/**访问所需要ADMIN的role
                              .anyRequest()//设定任何访问都需要认证
                              .authenticated()//设定任何访问都需要认证
                      )
                      .csrf(csrf -> csrf.disable())//csrf跨域访问无效 Cross-Site Request Forgery
                      .sessionManagement(session -> session.maximumSessions(1).maxSessionsPreventsLogin(true));
              return http.build();
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
    • finlay_dba这个user只设定了DBArole,login是无效的
      在这里插入图片描述
    • finlay_super这个user只设定了DBArole,login是无效的
  3. 进一步 测试详细的权限设定。
    • 定义controller
       @GetMapping("/admin/hello")
          @ResponseBody
          public String helloAdmin() {
              return "hello,admin";
          }
      
          @GetMapping("/user/hello")
          @ResponseBody
          public String helloUser() {
              return "hello,user";
          }
      
          @GetMapping("/db/hello")
          @ResponseBody
          public String helloDB() {
              return "hello,DBA";
          }
      
          @GetMapping("/hello")
          @ResponseBody
          public String hello() {
              return "hello";
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
    • 细化各种url的访问权限。
      @Configuration
      public class SecurityConfig {
          @Bean
          PasswordEncoder passwordEncoder() {
              return NoOpPasswordEncoder.getInstance();
          }
      
          @Bean
          UserDetailsService userDetailsService() {
              InMemoryUserDetailsManager users =
                      new InMemoryUserDetailsManager();
              users.createUser(User.withUsername("finlay_user")
                      .password("123456")
                      .roles("USER")
                      .build());
              users.createUser(User.withUsername("finlay_admin")
                      .password("123456")
                      .roles("ADMIN")
                      .build());
              users.createUser(User.withUsername("finlay_dba")
                      .password("123456")
                      .roles("DBA")
                      .build());
              users.createUser(User.withUsername("finlay_super")
                      .password("123456")
                      .roles("ADMIN", "DBA")
                      .build());
              return users;
          }
      
          @Bean
          SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
              httpSecurity.authorizeHttpRequests(
                              auth ->
                                      auth.requestMatchers("/admin/**")//匹配所有/** url
                                              .hasRole("ADMIN")//只能对于admin的role,才能访问
                                              .requestMatchers("/user/**")//匹配/user/**
                                              .hasRole("USER")//只有对于user的role,才能访问
                                              .requestMatchers("/db/**")//配置/db/**
                                              .hasRole("DBA")//只有对于dba的role,才能访问
                                              .anyRequest()
                                              .authenticated()//设定任何访问都需要认证
                      )
                      .formLogin(form -> form.loginProcessingUrl("/login")//这里对于前后端分离,提供的非页面访问url
                              .usernameParameter("username")//页面上form的用户名
                              .passwordParameter("password"))//页面上form的密码
                      .csrf(csrf -> csrf.disable())//csrf跨域访问无效
                      .sessionManagement(session -> session.maximumSessions(1).maxSessionsPreventsLogin(true));
              return httpSecurity.build();
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
    • 尝试访问/db/hello
      在这里插入图片描述
    • 清除chrome浏览器的session数据。
      因为没有定义logout功能,所以每次login成功之后,都不能消除login情报,这时候可以在chrome浏览器直接使用快捷键ctrl-shift-del之后进行session情报的删除。这样能够进行测试。在这里插入图片描述

2. 自定义认证表单

通常的情况是不用spring security默认提供的页面,下面进行自定义认证页面。

  1. 继续在SecurityConfigcontroller以及html中进行配置`
    • 配置自定义的认证url
       @Bean
          SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
              httpSecurity.authorizeHttpRequests(auth ->
                              auth.requestMatchers("/login*")
                                      .permitAll()
                                      .requestMatchers("/admin/**")//匹配所有/** url
                                      .hasRole("ADMIN")//只能对于admin的role,才能访问
                                      .requestMatchers("/user/**")//匹配/user/**
                                      .hasRole("USER")//只有对于user的role,才能访问
                                      .requestMatchers("/db/**")//配置/db/**
                                      .hasRole("DBA")//只有对于dba的role,才能访问
                                      .anyRequest()
                                      .authenticated()//设定任何访问都需要认证
                      )
                      .formLogin(form -> form.loginPage("/loginPage")
                              .loginProcessingUrl("/doLogin")//这里对于前后端分离,提供的非页面访问url
                              .usernameParameter("uname")//页面上form的用户名
                              .passwordParameter("passwd")
                              .successHandler(new SuccessHandler())//认证成功的处理
                              .failureHandler(new FailureHandler())//认证失败的处理
                              .defaultSuccessUrl("/index")//默认的认证之后的页面
                              .failureForwardUrl("/loginPasswordError"))//默认的密码失败之后的页面
                      .exceptionHandling(exceptionHandling ->
                              exceptionHandling.accessDeniedHandler(new CustomizeAccessDeniedHandler()))
                      .csrf(csrf -> csrf.disable())//csrf跨域访问无效
                      .sessionManagement(session -> session.maximumSessions(-1).maxSessionsPreventsLogin(true));
              return httpSecurity.build();
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 对于认证画面的url,进行permitAll开放,因为对于认证画面,不需要进行认证。
        auth.requestMatchers("/login*")
        .permitAll()
        
        • 1
        • 2
      • 定义login的认证画面url,之后会定义controllerview 注意,仅限于前后端一体程序
        .formLogin(form -> form.loginPage("/loginPage")
        
        • 1
      • 定于处理认证请求的url 注意前后端一体和前后端分离程序都会使用用这个url,springboot不对这个url定义controller
         .loginProcessingUrl("/doLogin")//这里对于前后端分离,提供的非页面访问url
        
        • 1
      • 对自定义页面的input进行设定,这里之后的html会使用。
        .usernameParameter("uname")//页面上form的用户名
        .passwordParameter("passwd")
        
        • 1
        • 2
      • 对于前后端的分离应用,直接访问doLogin,通过这里给前端返回结果对这两个类详细说明。
         .successHandler(new SuccessHandler())//认证成功的处理
         .failureHandler(new FailureHandler())//认证失败的处理
        
        • 1
        • 2
      • 如果直接访问loginPage,那么认证成功后默认的url就是这里
        注意,和successForwardUrl的区别是,successForwardUrlpost请求,这里是get请求
        .defaultSuccessUrl("/index")//默认的认证之后的页面
        
        • 1
      • 配置密码失败之后的url
        .failureForwardUrl("/loginPasswordError"))//默认的密码失败之后的页面
        
        • 1
      • 配置没有权限时候的错误页面的url,之后对CustomizeAccessDeniedHandler类进行说明。
        exceptionHandling(exceptionHandling ->
          exceptionHandling.accessDeniedHandler(new CustomizeAccessDeniedHandler()))
        
        • 1
        • 2
      • success handler类进行定义,主要在前后端的程序中使用。
         //success handler
            private static class SuccessHandler implements AuthenticationSuccessHandler {
                @Override
                public void onAuthenticationSuccess(
                        HttpServletRequest httpServletRequest,
                        HttpServletResponse httpServletResponse,
                        Authentication authentication
                ) throws IOException {
                    Object principal = authentication.getPrincipal();
                    httpServletResponse.setContentType("application/json;charset=utf-8");
                    PrintWriter printWriter = httpServletResponse.getWriter();
                    httpServletResponse.setStatus(200);
                    Map<String, Object> map = new HashMap<>();
                    map.put("status", 200);
                    map.put("msg", principal);
                    ObjectMapper om = new ObjectMapper();
                    printWriter.write(om.writeValueAsString(map));
                    printWriter.flush();
                    printWriter.close();
                }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
      • failure handler类进行定义,主要在前后端的程序中使用。
         //failure handler
            private static class FailureHandler implements AuthenticationFailureHandler {
                @Override
                public void onAuthenticationFailure(
                        HttpServletRequest httpServletRequest,
                        HttpServletResponse httpServletResponse,
                        AuthenticationException authenticationException
                ) throws IOException {
                    httpServletResponse.setContentType("application/json;charset=utf-8");
                    PrintWriter printWriter = httpServletResponse.getWriter();
                    httpServletResponse.setStatus(401);
                    Map<String, Object> map = new HashMap<>();
                    map.put("status", 401);
                    if (authenticationException instanceof LockedException) {
                        map.put("msg", "账户被锁定,登陆失败");
                    } else if (authenticationException instanceof BadCredentialsException) {
                        map.put("msg", "账户输入错误,登陆失败");
                    } else {
                        map.put("msg", "登陆失败");
                    }
                    ObjectMapper om = new ObjectMapper();
                    printWriter.write(om.writeValueAsString(map));
                    printWriter.flush();
                    printWriter.close();
                }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
      • CustomizeAccessDeniedHandler类进行定义,对没有权限等情况进行定义。比如,需要的roleADMIN,但是认证结束后的role确是DBA
        private static class CustomizeAccessDeniedHandler implements AccessDeniedHandler {
                @Override
                public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
                    response.sendRedirect("/loginNoPermissionError");
                }
            }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
    • controller层做出一个LoginController 注意,为了定义permitAll方便,统一采用login`开头
      @Controller
      public class LoginController {
          @GetMapping("/loginPage")
          public String loginPage() {
              return "login";
          }
      
          @GetMapping("/loginNoPermissionError")
          public String loginNoPermission() {
              return "no_permission_error";
          }
      
          @GetMapping("/loginPasswordError")
          public String loginError(Model model) {
              model.addAttribute("message", "认证失败");
              return "password_error";
          }
          @PostMapping("/loginPasswordError")
          public String loginErrorPost(Model model) {
              model.addAttribute("message", "认证失败");
              return "password_error";
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
    • 定义view层的各个html 注意,前后端分离程序
      • login的认证画面view
        <!DOCTYPE HTML>
        <html xmlns:th="http://www.thymeleaf.org/" lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Spring Security 用户自定义认证画面</title>
        </head>
        <body>
        <h1>自定义用户登陆</h1>
        <form th:action="@{/doLogin}" method="post">
            用户名:<input type="text" name="uname"><br>
            密码:<input type="text" name="passwd"><br>
            <input type="submit" value="登陆">
        </form>
        </body>
        </html>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
      • 定义密码错误的认证错误画面view
        <!DOCTYPE HTML>
        <html xmlns:th="http://www.thymeleaf.org/" lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Spring Security 用户自定义-密码输入错误</title>
        </head>
        <body>
        <h1>自定义用户登陆错误-用户密码输入错误"</h1>
        </form>
        </body>
        </html>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
      • 定义没有权限的认证错误画面view,比如需要ADMINrole,但是用户只有DBArole
        <!DOCTYPE HTML>
        <html xmlns:th="http://www.thymeleaf.org/" lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Spring Security 用户自定义-权限不足</title>
        </head>
        <body>
        <h1>"用户自定义画面,权限不足"</h1>
        </form>
        </body>
        </html>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
  2. 验证结果
    • DBA用户访问http://localhost:8080/db/hello在这里插入图片描述
    • 输入错误密码
      在这里插入图片描述
    • USERrole用户登录,但是访问http://localhost:8080/db/hello,需要DBArole
      在这里插入图片描述
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/812938
推荐阅读
相关标签
  

闽ICP备14008679号