创建名为securityConfig的bean时发生java错误。自动连线依赖项的AdminSecurityConfiguration注入失败
您好,我在两个登录屏幕(用户和管理员)上使用了spring安全配置文件,所以我在同一个安全配置类中使用了单独的配置注释(order1和order2),并且我在两个(管理员、用户)登录凭证上使用了两个不同的用户详细信息表
我已经附加了SecurityConfig、MyUserDetailsService和MyAdminDetailsService java文件
Securityconfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Configuration
@Order(1)
public static class UserSecurityConfiguration extends WebSecurityConfigurerAdapter
{
public UserSecurityConfiguration()
{
super();
}
@Autowired
@Qualifier("userDetailsService")
public UserDetailsService userDetailsService;
@Autowired
AuthenticationSuccessHandler customAuthenticatonSuccessHandler;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.headers().frameOptions().disable().
authorizeRequests()
.antMatchers("/admin/**").access("hasRole('FULL ACCESS')")
.antMatchers("/User_Control/**").access("hasRole('ADMIN ACCESS')or hasRole('FULL ACCESS')")
.antMatchers("/AccountSetting/**").access("hasRole('FULL ACCESS')")
.antMatchers("/Farm/**","/Shed/**","/Item/**","/Customer/**","/Supplier/**").access("hasRole('FULL ACCESS')")
.antMatchers("/Company/**","/General/**","/User_Control/**","/Contact/**","/Listofcharts/**").access("hasRole('FULL ACCESS')")
.antMatchers("/Strain/**","/Strain_Master/**","/Feed_Formulation/**","/Vaccination/**").access("hasRole('FULL ACCESS')")
.antMatchers("/Batch_Register/**","/Batch_Arrival/**","/Batch_Transfer_Register/**","/Batch_Transfer/**").access("hasRole('FULL ACCESS')")
.antMatchers("/Mortality_Register/**","/Mortality_Transaction/**","/Feed_Register/**","/Feed_Transaction/**").access("hasRole('FULL ACCESS')")
.antMatchers("/Egg_Register/**","/Egg_Transaction/**","/Cull_Register/**","/Cull_Transaction/**","/Vaccine_Register/**","/Vaccine_Report_Transaction/**").access("hasRole('FULL ACCESS')")
.antMatchers("/","/dashboard/**").access("hasRole('FULL ACCESS') or hasRole('LIMITTED ACCESS')or hasRole('ADMIN ACCESS')")
.and()
.formLogin().loginPage("/login").failureUrl("/login?error").successHandler(customAuthenticatonSuccessHandler)
.usernameParameter("username")
.passwordParameter("password")
.and().logout().logoutSuccessUrl("/login?logout").invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
.permitAll()
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/403")
.and()
.sessionManagement()
.sessionFixation().changeSessionId()
.invalidSessionUrl("/login?invalid")
.maximumSessions(1)
.maxSessionsPreventsLogin(true)
.expiredUrl("/login?expired")
.sessionRegistry(sessionRegistry());
}
@Override
public void configure(WebSecurity web) throws Exception
{
web.ignoring().antMatchers("/resources/**");
}
@Bean
public PasswordEncoder passwordEncoder()
{
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
@Bean(name = "sessionRegistry")
public SessionRegistry sessionRegistry()
{
return new SessionRegistryImpl();
}
@Autowired
private SessionRegistry sessionRegistry;
public List<SessionInformation> getActiveSessions()
{
List<SessionInformation> activeSessions = new ArrayList<>();
for(Object principal : sessionRegistry.getAllPrincipals())
{
activeSessions.addAll(sessionRegistry.getAllSessions(principal, false));
}
return activeSessions;
}
}
@Configuration
@Order(2)
public static class AdminSecurityConfiguration extends WebSecurityConfigurerAdapter
{
public AdminSecurityConfiguration()
{
super();
}
@Autowired
@Qualifier("adminDetailsService")
public UserDetailsService adminDetailsService;
@Autowired
AuthenticationSuccessHandler customAuthenticatonSuccessHandler;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.userDetailsService(adminDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.headers().frameOptions().disable().
authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ADMIN ACCESS')")
.antMatchers("/","/dashboard/**").access("hasRole('ADMIN ACCESS')")
.and()
.formLogin().loginPage("/login").failureUrl("/login?error").successHandler(customAuthenticatonSuccessHandler)
.usernameParameter("username")
.passwordParameter("password")
.and().logout().logoutSuccessUrl("/login?logout").invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
.permitAll()
.and().exceptionHandling().accessDeniedPage("/403")
.and().csrf().disable()
.sessionManagement()
.sessionFixation().changeSessionId()
.invalidSessionUrl("/login?invalid")
.maximumSessions(1)
.maxSessionsPreventsLogin(true)
.expiredUrl("/login?expired")
.sessionRegistry(sessionRegistry());
}
@Bean
public PasswordEncoder passwordEncoder()
{
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
//
@Bean(name = "sessionRegistry")
public SessionRegistry sessionRegistry()
{
return new SessionRegistryImpl();
}
}
}
MyAdminDetailsService.java
@Service("adminDetailsService")
public class MyAdminDetailsService implements UserDetailsService
{
@Autowired
private AdminUserDao adminUserDao;
@Autowired
private AdminRolePrivilegeDao adminRolePrivilegeDao;
String username;
@Transactional(readOnly=true)
@Override
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException
{
//com.broman.model.m_user m_user = userDao.findByUserName(username);
com.broman.admin.AdminUserModel adminuser1=adminUserDao.findByUserName(username);
int userid=adminuser1.getId();
List<GrantedAuthority> authorities = buildUserAuthority(adminRolePrivilegeDao.geta(userid));
return buildUserForAuthentication(adminuser1, authorities);
}
private User buildUserForAuthentication(com.broman.admin.AdminUserModel adminuser1, List<GrantedAuthority> authorities)
{
return new User(adminuser1.getAdmin_username(), adminuser1.getAdmin_password(),authorities);
}
private List<GrantedAuthority> buildUserAuthority(List<AdminRolePrivilegeModel> adminUserRoles)
{
Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();
// Build user's authorities
for (AdminRolePrivilegeModel userRole : adminUserRoles)
{
setAuths.add(new SimpleGrantedAuthority(userRole.getAdminrole_name()));
}
List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths);
return Result;
}
}
**MyUserDetailsService.java**
package com.broman.service;
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService
{
@Autowired
private UsersDao usersDao;
@Autowired
private UserPrivilegeDao userprivilegeDao;
String username;
@Transactional(readOnly=true)
@Override
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException
{
//com.broman.model.m_user m_user = userDao.findByUserName(username);
com.broman.setting.usercontrol.User user1=usersDao.findByUserName(username);
int userid=user1.getUser_id();
List<GrantedAuthority> authorities = buildUserAuthority(userprivilegeDao.geta(userid));
return buildUserForAuthentication(user1, authorities);
}
// org.springframework.security.core.userdetails.User
private User buildUserForAuthentication(com.broman.setting.usercontrol.User user1, List<GrantedAuthority> authorities)
{
return new User(user1.getUsername(), user1.getUserpassword(),authorities);
}
private List<GrantedAuthority> buildUserAuthority(List<UserPrivilege> userRoles)
{
Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();
// Build user's authorities
for (UserPrivilege userRole : userRoles)
{
setAuths.add(new SimpleGrantedAuthority(userRole.getRole_name()));
}
List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths);
return Result;
}
}
编译后,我得到了以下错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfig.AdminSecurityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.security.core.userdetails.UserDetailsService com.broman.config.SecurityConfig$AdminSecurityConfiguration.adminDetailsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.broman.admin.AdminRolePrivilegeDao com.broman.service.MyAdminDetailsService.adminRolePrivilegeDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.broman.admin.AdminRolePrivilegeDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我得到这个错误这么长时间,任何人都可以提供解决这个问题的办法
共 (0) 个答案