【问题标题】:error using Autowired in AuthenticationProvider在 AuthenticationProvider 中使用 Autowired 时出错
【发布时间】: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


    【解决方案1】:

    从异常java.lang.RuntimeException: Uncompilable source code来看,很明显这个错误是netbeans特有的。我猜 netbeans 正在使用两种不同的“类文件”数据源,这样它就可以解析构建中的所有符号,但运行时类路径和编辑器使用的是不同的东西。

    如果您使用的是 maven/gradle,请尝试使用它而不是您的 IDE 来构建和运行。或者尝试清理 netbeans 缓存。

    【讨论】:

    • 刚刚发现项目运行,但仅在我的 netbeans 会话的第一次运行时。
    • 更新。如果我只是重新启动我的 glassfish 服务器,它也可以工作。知道如何解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    相关资源
    最近更新 更多