ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [스프링 시큐리티] Form Login 인증
    강의노트/스프링 시큐리티 2020. 9. 10. 10:42

    WebSecurityConfigurerAdapter

    • WebSecurityConfigurer 객체를 생성하기 위한 추상 클래스를 제공한다.
    • 함수 오버라이딩을 통해서 구현을 커스터마이징 할 수 있다.

    WebSecurityConfigurer

    • WebSecurity를 커스터마이징 할 수 있다.
    • 웹 보안을 활성화 시키기 위해서 @EnableWebSecurity 어노테이션을 적용한다.
    • @EnableWebSecurity 어노테이션을 적용하면 자동으로 WebSecurityConfigureAdapter를 상속받은 Configuration이 WebSecurity에 적용된다.
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.core.Authentication;
    import org.springframework.security.core.AuthenticationException;
    import org.springframework.security.web.authentication.AuthenticationFailureHandler;
    import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @Configuration
    @EnableWebSecurity  // 웹 보안을 활성화 시키는 어노테이션
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            http
                    .authorizeRequests()                  // 보안 검사를 진행
                    .anyRequest().authenticated();        // 어떤 요청이든 인증을 받아야함
    
            http
                    .formLogin()                                // formLogin 인증을 적용
                    .loginPage("/loginPage")                    // 사용자 정의 로그인 페이지
                    .defaultSuccessUrl("/")                     // 로그인 성공 후 이동 페이지
                    .failureUrl("/login")                       // 로그인 실패 후 이동 페이지
                    .usernameParameter("username")              // 아이디 파라미터명 설정            
                    .passwordParameter("password")              // 패스워드 파라미터명 설정            
                    .loginProcessingUrl("/loginProc")           // 로그인 Form Action Url 설정
                    .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();
        }
    }

    참조 : 스프링 시큐리티 - Spring Boot 기반으로 개발하는 Spring Security 강좌

    댓글

Designed by Tistory.