JWT - 24강 authenticationManager() 문제 해결책
1288
작성한 질문수 3
버전 문제로 authenticationManager() 안되면
SecurityConfig
@Configuration
@RequiredArgsConstructor
@EnableWebSecurity // 스프링 시큐리티 필터가 스프링 필터체인에 등록된다.
public class SecurityConfig {
private final CorsConfig corsConfig; //
private final CorsFilter corsFilter; // Bean 으로 등록되어 있어서 바로 가져다 써도됨 근데 나는 걍 위에 클래스에서 메소드 호출할거임~
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
// http.addFilterBefore(new MyFilter3(), SecurityContextHolderFilter.class);
http.csrf(CsrfConfigurer::disable);
// http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.sessionManagement((sessionManagement) ->
sessionManagement
.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
http.addFilter(corsConfig.corsFilter()); // @CrossOrigin(인증X), 시큐리티 필터에 등록인증(O)
// http.formLogin().disable();
http.formLogin((form)->
form.disable());
// http.httpBasic().disable();
http.httpBasic((basic)->
basic.disable());
http.addFilter(new JwtAuthenticationFilter(authenticationManager)); // AuthenticationManger
http.authorizeHttpRequests(authorize -> authorize.requestMatchers("/user/**").authenticated()
.requestMatchers("/manager/**").hasAnyRole("ADMIN", "MANAGER")
.requestMatchers("/admin/**")
.hasAnyRole("ADMIN").anyRequest().permitAll());
return http.build();
}
}
JwtAuthenticationFilter
// 스프링 시큐리티에서 UsernamePasswordAuthenticationFilter 가 있음
// /login 요청해서 username, password 전송하면 (post)
// UserPasswordAuthenticationFilter가 동작을 함
@RequiredArgsConstructor
public class JwtAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
private final AuthenticationManager authenticationManager;
// /login 요청을 하면 로그인 시도를 위해서 실행되는 함수
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {
System.out.println("로그인 시도: JwtAuthenticationFilter");
return super.attemptAuthentication(request, response);
}
}
답변 1
-7
안녕하세요, 인프런 AI 인턴입니다.
정정모님, authenticationManager() 관련하여 문제가 발생했을 때 해결할 수 있는 방법을 알려드리겠습니다.
이 경우 SecurityConfig 클래스 내에서 authenticationManagerBean() 메소드를 직접 오버라이드 하여 AuthenticationManager를 빈으로 등록하면 문제를 해결하실 수 있습니다. 예를 들어:
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
위 코드를 SecurityConfig 클래스에 추가해 주시면 됩니다. 이렇게 함으로써 Spring Security의 AuthenticationManager를 외부에서 주입받을 수 있게 되어, JwtAuthenticationFilter에서 사용할 수 있게 됩니다.
위의 해결 방법이 도움이 되셨기를 바랍니다. 문제가 계속된다면 프로젝트의 스프링 시큐리티 설정 부분을 좀 더 면밀히 검토하셔야 할 수도 있습니다.
JWT를 구현한 다음 이 API를 호출해서 사용하는 것은 프론트엔드 쪽에서 하는 역할인가요?
0
94
1
Jwt쓰면 스프링시큐리티는 필수적으로 사용해야하나요?
0
401
1
13:23 system.out 출력문이 다르게 나옵니다.
0
130
1
수료증 문의
0
226
2
9분대에 질문이 있습니다 !
0
114
1
password 비교를 하지 않았는데 어떻게 인증이 통과된 건가요?
0
320
1
이전 강의 참고하라는 말씀
0
253
1
강의 실습하다가 막히는 분들 참고(2024년8월 기준)
2
1116
2
구글 소셜 로그인 302
0
200
1
오류 문의 _ org.springframework.orm.jpa.JpaSystemException: could not deserialize
1
584
1
[자바] 시큐리티 Config 참고
13
953
1
이론강의
0
280
1
SpringSecurity JWT 로그인 URL 2개 설정하는 방법
0
487
1
2024.06기준) 최근 SecurityConfig 설정 문의
0
921
3
구글 로그인시 authentication이 null 값이라고 에러가 발생합니다.
0
677
2
특정 url필터 거는 방법 이슈
0
422
1
강사님께서 말씀하시는 시큐리티세션이 SecurityContext인가요?
0
277
1
25강 마지막 테스트에서 오류
1
1044
2
jwt를 저장하는 위치에 궁금한 점이 있습니다.
0
298
1
mustache를 사용하지 않고 thymeleaf를 사용하려고 하는데
0
696
1
세션 인증방식이 REST 원칙에 위배되는 건가요?
0
338
1
jwt와 실제데이터의 관계
1
242
1
jwt 와 세션ID의 관계
1
312
1
SecurityConfig에서 세션 설정, 인가 설정
0
417
1





