Java 应用程序上下文中某些bean的依赖关系形成一个循环:
我在运行spring启动应用程序时遇到以下错误。我正在尝试使用SpringSecurity实现自定义登录Java 应用程序上下文中某些bean的依赖关系形成一个循环:,java,spring-boot,spring-security,Java,Spring Boot,Spring Security,我在运行spring启动应用程序时遇到以下错误。我正在尝试使用SpringSecurity实现自定义登录 2019-07-24 11:47:25.734 WARN 7640 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframe
2019-07-24 11:47:25.734 WARN 7640 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customUserDetails' defined in file [D:\Swapnil\downloaded code\Web-Wallet\target\classes\com\fidel\webwallet\service\impl\CustomUserDetails.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'customUserDetails': Requested bean is currently in creation: Is there an unresolvable circular reference?
添加下面我觉得与此错误相关的类
CustomUserDetails
/**
*
*/
package com.fidel.webwallet.service.impl;
import java.util.Collection;
import java.util.stream.Collectors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import com.fidel.webwallet.model.UserInfo;
/**
* @author Swapnil
*
*/
@Component
public class CustomUserDetails extends UserInfo implements UserDetails {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
// TODO Auto-generated method stub
return getRoles().stream().map(role -> new SimpleGrantedAuthority("ROLE_" + role.getName()))
.collect(Collectors.toList());
}
public CustomUserDetails(UserInfo userInfo) {
super(userInfo);
}
@Override
public String getPassword() {
// TODO Auto-generated method stub
return super.getUserPassword().getPassword();
}
@Override
public String getUsername() {
// TODO Auto-generated method stub
return super.getEmailId();
}
@Override
public boolean isAccountNonExpired() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isEnabled() {
// TODO Auto-generated method stub
return true;
}
}
如果提供的信息不够,请告诉我,我将尝试添加剩余calssess从您的
CustomUserDetails
类中删除@组件
类不能同时是an和A
在您的示例中,UserInfo
是一个实体(用于数据库存储的类),因此当CustomUserDetails
扩展它时,它应该像所述实体的包装器一样运行,添加额外的功能。我想这就是你最初的意图
由
@Component
注释标记的类必须是可注入的,就像@Service
、@Controller
或@Repository
一样。这些类应该是无状态的,Spring必须能够创建没有问题的实例。从CustomUserDetails
类中删除@组件
类不能同时是an和A
在您的示例中,UserInfo
是一个实体(用于数据库存储的类),因此当CustomUserDetails
扩展它时,它应该像所述实体的包装器一样运行,添加额外的功能。我想这就是你最初的意图
由@Component
注释标记的类必须是可注入的,就像@Service
、@Controller
或@Repository
一样。这些类应该是无状态的,Spring必须能够毫无问题地创建实例。您以某种方式混合了SpringBean(业务行为)和实体(状态)的概念-请后退一步,解释一下您实际想要实现的目标,您当前的方法并没有清楚地说明这一点。我只是尝试将spring安全性包括到我的应用程序中,并希望自定义登录表单用于用户身份验证。我添加了一个GitHub链接。数据对象(如UserDetails
)不应该是SpringBean(服务对象,@Component
)。您以某种方式混合了SpringBean(业务行为)和实体(状态)的概念-请后退一步,解释一下您实际想要实现的目标,您当前的方法并没有清楚地说明这一点。我只是尝试将spring安全性包括到我的应用程序中,并希望自定义登录表单用于用户身份验证。我添加了一个GitHub链接。数据对象(如UserDetails
)不应该是Springbeans(服务对象,@Component
)。
package com.fidel.webwallet.model;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import com.fidel.webwallet.commons.CommonUtils;
/**
* @author Swapnil
*
*/
@Entity
public class UserInfo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer custId;
private String userName;
private String address;
private String contactNo;
private String emailId;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "userInfo")
private Password password;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "users")
private Set<Role> roles;
public UserInfo() {
// TODO Auto-generated constructor stub
}
public UserInfo(UserInfo userInfo) {
this.userName = userInfo.getUserName();
this.password.setPassword(userInfo.getUserPassword().getPassword());
this.roles = userInfo.getRoles();
}
public Integer getCustId() {
return custId;
}
public void setCustId(Integer custId) {
this.custId = custId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
/**
* @return the password
*/
public Password getUserPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setUserPassword(Password password) {
this.password = password;
}
/**
* @return the roles
*/
public Set<Role> getRoles() {
return roles;
}
/**
* @param roles the roles to set
*/
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public String validate() {
String emptyFiled = "";
if (CommonUtils.isEmpty(emailId)) {
emptyFiled = "email";
}
if (CommonUtils.isEmpty(this.contactNo)) {
emptyFiled = "contact";
}
return emptyFiled;
}
}
package com.fidel.webwallet.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* @author Swapnil
*
*/
@Configuration
@EnableWebSecurity
public class WalletWebSecurityConfigurerAdapter 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/**", "/wallet/user/register", "/wallet/**").permitAll()
.antMatchers("/js/*.js").permitAll().antMatchers("/css/**").permitAll().antMatchers("**/fonts/**")
.permitAll().antMatchers("/webfonts/**").permitAll().anyRequest().authenticated();
http.formLogin().loginProcessingUrl("/wallet/user/postReq").loginPage("/wallet/user/login")
.usernameParameter("username").passwordParameter("password").successForwardUrl("/wallet/user/home");
http.logout().permitAll();
}
/*
* @Bean public AuthenticationManager customAuthenticationManager() throws
* Exception { return authenticationManager();
*
* }
*/
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(new PasswordEncoder() {
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
// TODO Auto-generated method stub
return true;
}
@Override
public String encode(CharSequence rawPassword) {
// TODO Auto-generated method stub
return rawPassword.toString();
}
});
}
}