인프런 커뮤니티 질문&답변

나현우님의 프로필 이미지

작성한 질문수

Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)

Users Microservice - AuthenticationFilter 추가

스프링 시큐리티 문의 (webSecurityConfigurerAdapter 취소선)

23.03.02 00:01 작성

·

1.6K

0

안녕하세요. 스프링시큐리티 로그인관련 진행하다가 extends webSecurityConfigurerAdapter 가 취소선이 나와서 확인해보니 현재는 사용하지않는다고 알게되었습니다.

 

나름 바꾸면서 진행중인데

authenticationManager() 를 사용하려면 어떻게해야되는지 알 수 있을까요?

답변 1

4

dawon.hyun님의 프로필 이미지

2023. 03. 16. 15:17

authenticationManager 빈을 하나 생성하셔서 사용하시면 될 것 같습니다.

@Bean
	AuthenticationManager authenticationManager(AuthenticationConfiguration authConfiguration) throws Exception {
		return authConfiguration.getAuthenticationManager();
	}

 

@Bean
	public SecurityFilterChain configure(HttpSecurity http) throws Exception {
    AuthenticationManager authenticationManager = authenticationManager(http.getSharedObject(AuthenticationConfiguration.class));    
    http.~~~
     .addFilter(getAuthenticationFilter(authenticationManager));
    return http.build();
}

private AuthenticationFilter getAuthenticationFilter(AuthenticationManager authenticationManager) {
		AuthenticationFilter authenticationFilter = new AuthenticationFilter(authenticationManager);
		return authenticationFilter;
	}

 

또는

@Bean
	public SecurityFilterChain configure(HttpSecurity http) throws Exception {
    
    http.~~~
     .addFilter(getAuthenticationFilter(http));
    return http.build();
}

private AuthenticationFilter getAuthenticationFilter(HttpSecurity http) {
		AuthenticationFilter authenticationFilter = new AuthenticationFilter(http.getSharedObject(AuthenticationConfiguration.class));
		return authenticationFilter;
	}

이렇게 추가하시고

AuthenticationFilter에서는

@RequiredArgsConstructor
public class AuthenticationFilter extends UsernamePasswordAuthenticationFilter{

	private final AuthenticationManager authenticationManager;

      //attemptAuthentication

      //successAuthentication
}

이렇게 적용해보시면 될 듯합니다.