1.의의
2.형태
1)AuthenticationSuccessHandler 인터페이스를 직접 구현하여 홈URL로 리다이렉트하는 기능을 구현 한다.
2)AuthenticationSuccessHandler 인터페이스를 직접 구현하여 로그인에 성공한 후 인증 전에 접근을 시도한 URL로 리다이렉트하는 기능을 구현한다.
3)SavedRequestAwareAuthenticationSuccessHandler는 AuthticationSuccessHandler의 구현클래스이며 ,똑같은 방식으로 인증 전에 접근을 시도한 URL로 리다이렉트하는 기능을 가지고 있다.
package security.starting;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest()
.authenticated();
http
.formLogin()
.loginPage("/loginPage") //사용자 정의 로그인 페이지
.defaultSuccessUrl("/") //로그인 성공후 이동 페이지
.failureUrl("/login") //로그인 실패 후 이동 페이지
//서버에 사용자 입력값을 전송할 때 폼 입력 요소의 인증 정보 파라미터명을 변경한다.
.usernameParameter("userId") //ID 파라미터 설정
.passwordParameter("passwd") //PASSWORD 파라미터 설정
.loginProcessingUrl("/login_proc")
// 실제 로그인 진행 스프링 시큐리티가 로그인 자동 진행 POST방식으로 로그인 진행
.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();
}
}