gpt4 book ai didi

java - 在 AuthenticationProvider 中使用 Autowired 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 11:30:51 26 4
gpt4 key购买 nike

我尝试在我的authenticationProvider 中使用 Autowiring 的对象,但出现错误。如果在类中不使用服务对象,没有问题,但当然我需要服务对象。

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);
}}

Bean 的创建:

@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[]{"/"};
}

}

错误消息:

Warning: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'authenticationProvider'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customAuthenticationProvider' defined in file [D:\Users\neuts\Documents\NetBeansProjects\project-ip-vincentneuts\spring-mvc-kopie\target\r0621919\WEB-INF\classes\view\controller\CustomAuthenticationProvider.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [view.controller.CustomAuthenticationProvider]: Constructor threw exception; nested exception is java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Autowired location: class view.controller.CustomAuthenticationProvider Severe: Context initialization failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'authenticationProvider'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customAuthenticationProvider' defined in file [D:\Users\neuts\Documents\NetBeansProjects\project-ip-vincentneuts\spring-mvc-kopie\target\r0621919\WEB-INF\classes\view\controller\CustomAuthenticationProvider.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [view.controller.CustomAuthenticationProvider]: Constructor threw exception; nested exception is java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Autowired location: class view.controller.CustomAuthenticationProvider

编辑:它仅在第一次运行时有效。 (重新启动我的 glassfish 服务器后)

最佳答案

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

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

关于java - 在 AuthenticationProvider 中使用 Autowired 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43820167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com