我是使用 Spring Boot 的新手,我正在嘗試創建我的第一個 Spring Boot 應用程式。我一直在查看教程并將看起來很簡單的東西混合起來,以便為 Spring Boot 初學者實作。
我在網上看過似乎唯一一次發生這種情況是如果有人使用存盤庫分配服務而不是自動連接它我沒有做這樣的事情。
我的程式最終為 null 導致以下堆疊:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userAuthentificationServiceImpl': Unsatisfied dependency expressed through field 'bCryptPasswordEncoder'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'webSecurityConfig': Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1389) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:656) ~[spring-beans-5.3.20.jar:5.3.20]
... 23 common frames omitted
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'webSecurityConfig': Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:355) ~[spring-beans-5.3.20.jar:5.3.20]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:227) ~[spring-beans-5.3.20.jar:5.3.20]
我的存盤庫
import com.example.realtyKing.Model.User;
import com.example.realtyKing.Model.UserAuthentification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserAuthentificationRepository extends
JpaRepository<UserAuthentification,
Long> {
public UserAuthentification findByUser(User User);
public UserAuthentification findByAuthUserName(String username);
}
實作類
import java.util.HashSet;
import java.util.Set;
import com.example.realtyKing.Model.Role;
import com.example.realtyKing.Model.UserAuthentification;
import com.example.realtyKing.Repositories.UserAuthentificationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserAuthentificationDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserAuthentificationRepository userAuthetificationRepository;
@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserAuthentification userAuthetification=userAuthetificationRepository.findByAuthUserName(username);
Set < GrantedAuthority > grantedAuthorities = new HashSet < > ();
for (Role role: userAuthetification.getRoles()) {
grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
}
return new org.springframework.security.core.userdetails.User(userAuthetification.getAuthUserName(), userAuthetification.getAuthPassword(), grantedAuthorities);
}
}
安全配置類
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication
.builders.AuthenticationManagerBuilder;
import
org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.
configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration
.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder()
{
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**", "/userAuthentificationForm").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Bean
public AuthenticationManager customAuthenticationManager() throws Exception {
return authenticationManager();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
{
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
}
ps 最后一個的匯入是正確的,很難通過這個小螢屏上的文本來適應那些長的匯入。
uj5u.com熱心網友回復:
您向我們展示了錯誤的代碼,但從錯誤訊息來看,您似乎有另一個名為UserAuthentificationServiceImpl(與 不同UserAuthentificationDetailsServiceImpl)的類。
問題是這個類是自動裝配的WebSecurityConfig,但也需要你的BCryptPasswordEncoderbean 來自WebSecurityConfig. 所以基本上你有一個回圈依賴:
UserAuthentificationServiceImpl取決于BCryptPasswordEncoder(由于@Autowired)BCryptPasswordEncoder取決于WebSecurityConfig(由于 bean 在該類中)WebSecurityConfig取決于UserAuthentificationServiceImpl(由于@Autowired)
要解決此問題,您可以移動BCryptPasswordEncoder我們的WebSecurityConfig. 例如,通過創建一個新類:
@Configuration
public class PasswordSecurityConfig {
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
如果你這樣做,你打破了回圈依賴,因為 thenBcryptPasswordEncoder不再依賴于WebSecurityConfig.
uj5u.com熱心網友回復:
你的模型UserAuthentification有注釋嗎@Component?也只需交叉檢查 Model 類中的所有屬性是否都有 getter 和 setter。我錯過了一次,我的回復不包含那些沒有 getter 和 setter 的屬性。
uj5u.com熱心網友回復:
嘗試使您的安全配置類中的@Bean 不公開。
做這個:
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder()
{
return new BCryptPasswordEncoder();
}
對此:
@Bean
BCryptPasswordEncoder bCryptPasswordEncoder()
{
return new BCryptPasswordEncoder();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/491894.html
上一篇:GDB是否保證一個執行緒在切換到另一個執行緒后不會超過指定的運行時間?
下一篇:VBA宏-ok除錯ko運行時
