UsernamePasswordAuthenticationFilter
가 정보를 받아 유저 네임과 비밀번호를 추출합니다.
이후 Authentication
이라는 객체를 생성하고 이 객체에 아이디, 비밀번호를 담아 Authenticationmanager
가 Authentication
을 가지고 인증을 하게 됩니다.
이때, 인증에 실패하면 예외를 발생, 인증에 성공하면 인증 전 객체와 동일한 타입을 만들고 최종 인증 결과를 저장해 주게 됩니다. 이때 암호 값은 보안 상 비워두기도 합니다.
최종적으로 SecurityContextHolder
에 있는 SecurityContext
에 객체를 저장하게 되고 이로써 Authentication authentication = SecurityContextHolder.getContext().getAuthentication()
통해 인증 객체를 전역적으로 사용할 수 있게 됩니다.
@Configuration
@EnableWebSecurity
@Order(0)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/admin/**")
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}
@Configuration
@Order(1)
class SecurityConfig2 extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().permitAll()
.and()
.formLogin();
}
}
@Order()
에 int 자료형 파라미터값을 부여해 주면서 필터 프록시에서 어떤 설정을 먼저 체크할 지 지정해 줍니다. 해당 어노테이션이 없으면 설정 중복으로 오류가 나고 순서가 바뀌면 의미가 없어집니다.
위로 예를 들어서 @Order
파라미터에 넘겨진 0,1을 기준으로 각 0,1로 칭한다면 0은 /admin/**
경로에 대해 체크를 하고 있고 1은 모든 경로를 열어두고 있습니다. 지금은 /admin/**
을 먼저 체크하기 때문에 상관이 없지만 순서가 바뀌면 /admin/**
를 체크하기 이전부터 모든 것을 열어둬서 의미가 없어집니다. 즉, 세분화 된 설정을 항상 먼저 체크해 줘야합니다.