【发布时间】:2017-05-06 11:57:42
【问题描述】:
我正在尝试在我的 authenticationProvider 中使用自动装配的对象,但出现错误。在类中不使用服务对象没有问题,但我当然需要服务对象。
customAuthenticationprovider 类:
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Autowired
private Service service;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
if (service.LoginCorrect(name, password)) {
// use the credentials
// and authenticate against the third-party system
List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority(service.getPerson(name).getRole().role()));
return new UsernamePasswordAuthenticationToken(
name, password, grantedAuths);
} else {
return null;
}
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}}
使用自定义提供程序的类:
@Configuration
@EnableWebSecurity
@ComponentScan("view.controller")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider authenticationProvider;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/register/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login").permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
}}
豆子的创造:
@Configuration
class ApplicationConfig extends WebMvcConfigurerAdapter {
@Bean(destroyMethod = "close")
public Service service() {
return new Service("JPADatabase");
}
}
安全初始化:
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{ApplicationConfig.class, WebSecurityConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{DispatcherServletConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
错误信息:
警告:上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“webSecurityConfig”的bean时出错:通过字段“authenticationProvider”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建文件 [D:\Users\neuts\Documents\NetBeansProjects\project-ip-vincentneuts\spring-mvc-kopie\target\ 中定义的名称为“customAuthenticationProvider”的 bean 时出错r0621919\WEB-INF\classes\view\controller\CustomAuthenticationProvider.class]:bean 的实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [view.controller.CustomAuthenticationProvider]:构造函数抛出异常;嵌套异常是 java.lang.RuntimeException:无法编译的源代码 - 找不到符号 符号:自动连线类 位置:类 view.controller.CustomAuthenticationProvider 严重:上下文初始化失败 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“webSecurityConfig”的bean时出错:通过字段“authenticationProvider”表达的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建文件 [D:\Users\neuts\Documents\NetBeansProjects\project-ip-vincentneuts\spring-mvc-kopie\target\ 中定义的名称为“customAuthenticationProvider”的 bean 时出错r0621919\WEB-INF\classes\view\controller\CustomAuthenticationProvider.class]:bean 的实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [view.controller.CustomAuthenticationProvider]:构造函数抛出异常;嵌套异常是 java.lang.RuntimeException:无法编译的源代码 - 找不到符号 符号:自动连线类 位置:类view.controller.CustomAuthenticationProvider
编辑:它只在第一次运行时有效。 (重启我的 glassfish 服务器后)
【问题讨论】:
标签: java spring spring-security