1.의의
2.구현
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();
}
}
authenticationEntryPoint (인증 진입 지점)란, Spring Security에서 인증(Authentication)이 필요한 리소스에 접근하려는 경우, 인증이 되지 않은 사용자의 요청을 인터셉트하고, 로그인 창 같은 것으로 사용자를 리다이렉트하거나, 인증 실패에 대한 처리를 하는 곳입니다. 즉, 인증을 해야만 접근 가능한 페이지를 요청했으나, 아직 인증되지 않은 사용자가 요청을 보내면, 요청을 막고 인증을 요구하게 되는데 이 때 사용되는 클래스입니다.