Cộng đồng Hỏi & Đáp của Inflearn
setAuthentication
Đã giải quyết
Viết
·
488
0
안녕하세요
JwtFilter 에서
StringUtils.hasText(jwt) && tokenProvider.validateToken(jwt)
조건을 만족하면 아래와 같이
SecurityContextHolder.getContext().setAuthentication(authentication);
인증정보를 저장하는데
컨트롤러에서 또 인증정보를 저장하는건가요 ??

정보를 그럼 총 2번 저장하는건가요??
위 JwtFilter 에서 (StringUtils.hasText(jwt) && tokenProvider.validateToken(jwt)) 이조건이 만족했을떄
spring-bootjwt
Câu trả lời 1
0
silvernine
Người chia sẻ kiến thức
안녕하세요!
@PostMapping("/authenticate")의 경우 로그인을 위한 API이기 때문에 permitAll() 설정이 적용되어 있습니다. 이로직에서는 로그인이 정상적으로 수행되었을 때 인증 정보를 컨텍스트에 저장합니다.
이때는 JwtFilter의 doFilter가 수행되지 않습니다.
//SecurityConfig
.authorizeHttpRequests(authorizeHttpRequests -> authorizeHttpRequests
.requestMatchers("/api/hello", "/api/authenticate", "/api/signup").permitAll()
.requestMatchers(PathRequest.toH2Console()).permitAll()
.anyRequest().authenticated()
)JwtFilter의 doFilter는 permitAll() 설정이 적용되지 않은 다른 호출에서만 적용됩니다.
//SecurityConfig
.with(new JwtSecurityConfig(tokenProvider), customizer -> {});
//JwtSecurityConfig
@Override
public void configure(HttpSecurity http) {
http.addFilterBefore(
new JwtFilter(tokenProvider),
UsernamePasswordAuthenticationFilter.class
);
}silvernine
Người chia sẻ kiến thức
permitAll()이 적용된 호출은 해당 시큐리티 설정의 영향을 받지 않습니다. 그래서 JwtFilter의 doFilter도 호출되지 않습니다.





JwtFilter의 doFilter는 permitAll() 설정이 되어있으면 실행되지 않는건 내부적으로 코드가 되어있는거라고 생각하면 될까요 ?