所以我在 AppConfig.java 中有這段代碼:
@Configuration
@EnableWebSecurity
public class AppConfig extends WebSecurityConfigurerAdapter {
private final CurrentUserService currentUserService;
private final SessionFilter sessionFilter;
private final PasswordEncoder passwordEncoder;
@Autowired
@Lazy
public AppConfig(CurrentUserService currentUserService, SessionFilter sessionFilter, PasswordEncoder passwordEncoder) {
this.currentUserService = currentUserService;
this.sessionFilter = sessionFilter;
this.passwordEncoder = passwordEncoder;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(currentUserService).passwordEncoder(passwordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http = http.cors().and().csrf().disable();
http = http.exceptionHandling().authenticationEntryPoint(
(request, response, authException) -> {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
}
).and();
http.authorizeRequests().antMatchers("/api/login").permitAll().anyRequest().authenticated();
http.addFilterBefore(sessionFilter, UsernamePasswordAuthenticationFilter.class);
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
如果我擺脫了@Lazy,它將無法啟動,我試圖擺脫建構式并執行以下操作:
@Autowired
private final CurrentUserService currentUserService;
@Autowired
private final SessionFilter sessionFilter;
@Autowired
private final PasswordEncoder passwordEncoder;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(currentUserService).passwordEncoder(passwordEncoder);
}
同樣的事情,有人可以幫幫我嗎,我真的不想使用@Lazy 實作。這是它回傳的錯誤:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appConfig' defined in file [C:\Users\Blade\Documents\WebMent\modules\backend\build\classes\java\main\com\shortsdesigns\webment\configuration\AppConfig.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'appConfig': Requested bean is currently in creation: Is there an unresolvable circular reference?
當擺脫建構式并使用欄位注入時,它給了我這個錯誤:
Error creating bean with name 'appConfig': Unsatisfied dependency expressed through field 'passwordEncoder'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'appConfig': Requested bean is currently in creation: Is there an unresolvable circular reference?
uj5u.com熱心網友回復:
發生此錯誤是因為您PasswordEncoder在注入它的同一類中創建。
最好的解決方案是根本不自動裝配PasswordEncoder(或CurrentUserService)。
看來這些實體僅在configure(AuthenticationManagerBuilder auth)方法中使用,這是多余的。
PasswordEncoder將and注冊UserDetailsService為 bean 足以讓 Spring Security 檢測到它們并在您的配置中使用它們。
換句話說,您應該洗掉以下代碼:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(currentUserService).passwordEncoder(passwordEncoder);
}
應用程式仍將以完全相同的方式運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/449451.html
