
SecurityContextHolder의 구조
Bean 등록
그 중, SecurityConfig도 먼저 등록된다.
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable() // Jwt token을 이용할 계획이므로, 상태 유지가 안되기 때문에 필요 없다고 사료됨.
.headers().frameOptions().sameOrigin()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeHttpRequests(authorize -> authorize
.antMatchers("/", "/css/**", "/images/**", "/js/**",
"/h2-console/**", "/profile", "/post/**").permitAll()
.antMatchers("/recruitment", "/setting", "/myPosts").hasRole(USER.name())
.anyRequest().authenticated())
.oauth2Login(oauth2Login -> oauth2Login
.successHandler(oAuth2SuccessHandler)
.userInfoEndpoint()
.userService(customOAuth2UserService))
.logout(logout -> logout
.deleteCookies("JSESSIONID")
.logoutSuccessUrl("/"));
return http.build();
}
내가 설정한 정보들이 일괄적으로 처리된 후 SecurityFilterChain에 등록된다
로그인 요청을 가장 먼저 받아서 처리하는 곳은 CustomOAuth2UserService이다.

OAuth2UserRequest에 담겨서 들어온다
OAuth2UserRequest로 부터 load한 유저의 정보에는 위와 같은 내용들이 담겨있다.

OAuth2AuthenticationToken에 담겨서 SuccessHandler에 전달된다.AuthenticationSuccessHandler를 커스텀한 OAuth2SuccessHandler에서 OAuth2AuthenticationToken을 처리한다

UserDto 객체로 매핑한다
중간에 TokenService 의 generateToken 메서드로 토큰을 생성한다.



위에서 설정한 claim을 기반으로, iat와 exp라는 정보가 Claim에 추가된다.

secretKey와 설정해준 알고리즘을 기반으로 서명을 한다.
만들어진 Jwt 토큰은 SuccessHandler에서 마저 처리한다.


생성된 토큰의 정보를 헤더에 추가하고, JSON형태로 반환하도록 한다.
PrintWriter를 통해 바디에 직접 내용을 작성


많은 필터들을 거친 후, 비로소 토큰이 출력된다.

Auth와 Refresh가 잘 들어가있다.여기까지가 토큰 발급

[Security] Spring JWT 인증 With REST API(OAuth2.0 추가) (3)
