스프링 시큐리티

Ch01. 스프링 시큐리티 기본 API 및 Filter 이해 - Form Login 인증

webmaster 2022. 1. 12. 17:42
728x90

동작 방식
formLogin이 제공하는 API

  • usernameParameter, passwordParameter, loginProcessUrl 같은 경우 UI 화면에서 맞춘 값과 동일하게 설정하면 된다.
  • @Override
     protected void configure(HttpSecurity http) throws Exception {
         http//인가 정책
                 .authorizeRequests() 
                 .anyRequest()    //모든 요청
                 .authenticated();
    
    
         http// 인증 정책
                 .formLogin()
                 //.loginPage("/loginPage") //로그인 페이지 설정
                 .defaultSuccessUrl("/") //인증 성공 URL
                 .failureUrl("/login")//실패 했을때
                 .usernameParameter("userId")
                 .passwordParameter("passwd")
                 .loginProcessingUrl("/login_proc")
                 .successHandler(new AuthenticationSuccessHandler() {
                     @Override
                     public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
                         System.out.println("authentication : " + authentication.getName());
                         response.sendRedirect("/"); //루트 페이지로 이동
                     }
                 }) //성공 했을때 해당 핸들러를 실행한다
                 .failureHandler(new AuthenticationFailureHandler() {
                     @Override
                     public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
                         System.out.println("exception : " + exception.getMessage());
                         response.sendRedirect("/login");
                     }
                 }) //실패했을때의 실행되는 핸들러
                 .permitAll()//해당 페이지 같은 경우 인증을 받지 않아도 접근이 가능해야한다.
             ;
         
    }

 

728x90