빈 1개 등록 시 다른 해결 방법
63
17 asked
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) {
return http
.authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
.formLogin(Customizer.withDefaults())
.authenticationProvider(customAuthenticationProvider())
.authenticationProvider(new DaoAuthenticationProvider(inMemoryUserDetailsManager()))
.build();
}
@Bean
public AuthenticationProvider customAuthenticationProvider() {
return new CustomAuthenticationProvider();
}
@Bean
public UserDetailsService inMemoryUserDetailsManager() {
UserDetails user = User.withUsername("user")
.password("{noop}1111")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
}
메소드 인자로 AuthenticationManagerBuilder랑 Authenticationconfiguration을 주입 받아서 수동으로 parent Manager가 가진 Provider를 삭제하고 추가하는 작업을 하지 말고,
그냥 안전하게, 대체되었던 DaoAuthenticationProvider를 authenticationProvider()로 다시 추가하면 안 되나요?
Answer 1
0
안녕하세요, 인프런 AI 인턴입니다.
매콤한각설탕님께서 질문 주셨네요.
SecurityFilterChain 구성 시, 별도로 AuthenticationManagerBuilder나 AuthenticationConfiguration을 사용하여 parent Manager의 Provider를 삭제하고 추가하는 방식을 피하고자 하신다면, DaoAuthenticationProvider를 직접 추가하는 방법 역시 가능합니다.
사실상 코드 상에서도 DaoAuthenticationProvider를 authenticationProvider()에 다시 추가해 놓으셨습니다. 이렇게 구현하시면 CustomAuthenticationProvider와 DaoAuthenticationProvider가 모두 사용됩니다.
이처럼 SecurityFilterChain 내에서 authenticationProvider()를 통해 필요한 AuthenticationProvider들을 지정하고 사용하시면 Spring Security에서 여러 가지 인증 경로를 관리하는 데 문제가 없습니다.
계속해서 학습하시면서 문제가 생기시면 언제든지 질문해 주세요!
도움이 되셨기를 바랍니다. 관련된 문서를 참고하시려면 아래의 링크를 사용하세요:
Spring Boot Reference Documentation - Security
감사합니다.
로그아웃-logout()-2 강에서 겟방식 로그아웃 호출 후 화면이동 질문입니다.
0
27
2
단원별 소스코드
0
57
2
CustomAuthenticationProvider 추가 관련 문의
0
67
2
AOP 의존성 명칭 변경
0
61
1
@Bean으로 AuthenticationProvider를 등록 시 http.authenticationProvider 함수를 이용해서 추가해줘야되나요?
0
84
2
OIDC의 id token에 담긴 데이터에 대해
0
72
1
loginPage("/loginPage") 질문드립니다.
0
66
1
@EnableWebSecurity
0
145
1
트랜잭션과 롤백
0
97
1
68. 인증 이벤트 - AuthenticationEventPublisher 활용 강좌 음성 문제
0
86
2
AuthenticationManager 사용 방법
0
146
2
HttpSecurity.authorizeHttpRequests() - 2 강의 부분에 대한 질문
0
100
2
spring security 6.3에서는 HttpSecurity가 만들어지기 전 WebSecurity가 먼저 만들어지는게 맞나요??
0
188
1
init(B Builder), configure(B builder) 에 대하여 질문 드립니다.
0
103
2
메타 주석 질문
0
66
1
동시세션제어 기능에서 로그아웃하기
0
143
3
로그인 후, redirect 에서 error
0
137
3
Session 생성 타이밍에 대한 질문
0
82
2
강의 참고 내용을 개발 로그로 작성해도 될지 문의드립니다.
0
130
2
customAuthentication 관련
0
125
2
authenticationManagerBuilder 주입받은거 vs 만든 거
0
108
1
UserDetailsService()에서 UserDetail이 아닌 타입을 반환할 수 있나요?
0
99
1
9:28 패턴 3의 경우 마지막으로 설정한 것만 적용되는 것 같습니다.
0
156
2
Bean UserDetailsService
0
88
2

