gpt4 book ai didi

java - 无法使用 Spring Security 实现 CustomAuthenticationProvider : BeanCreationException while autowiring

转载 作者:行者123 更新时间:2023-12-02 08:54:16 38 4
gpt4 key购买 nike

我正在 Spring Security 中实现 CustomAuthenticationProvider。我创建了实现 AuthenticationProvider 的 CustomAuthenticationProvider 类。但是,当我在 SecurityConfiguration 类中定义此 CustomAuthenticationProvider 并 Autowiring 它时,应用程序会引发以下错误:

2020-03-08 19:27:42 [main] ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.highrise.isimwebapp.config.customauth.CustomAuthenticationProvider com.highrise.isimwebapp.config.SecurityConfiguration.authProvider; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.highrise.isimwebapp.config.customauth.CustomAuthenticationProvider] 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)}

我已在 CustomAuthenticationProvider 类中包含 @Component 注释。此外,定义此类的包也包含在 @ComponentScan 中。上下文没有选取 CustomAuthenticationProvider bean。其他定义的类成功地被 @ComponentScan 中定义的上下文拾取,并且它们的 Autowiring 对象上没有收到此类错误。我这边可能出了什么问题?任何有关如何解决此问题的帮助将不胜感激。

CustomAuthenticationProvider.java

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// TODO Auto-generated method stub
String name = authentication.getName();
String password = authentication.getCredentials().toString();
if(name.equalsIgnoreCase("testuser1") && password.equalsIgnoreCase("demo"))
return new UsernamePasswordAuthenticationToken(name, password);
else
throw new BadCredentialsException("Authentication failed");
}

@Override
public boolean supports(Class<?> authentication) {
// TODO Auto-generated method stub
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}

}

安全配置.java

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
private CustomAuthenticationProvider authProvider;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authProvider);
}

@Override
protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.antMatchers("/icon/**").permitAll()
.anyRequest().authenticated()
.and().formLogin().loginPage("/login").usernameParameter("username").passwordParameter("password").permitAll().defaultSuccessUrl("/persons/listall")
.and().csrf().disable();

}
}

SpringWebConfig.java

@EnableWebMvc
@Configuration
@ComponentScan({ "com.highrise.isimwebapp.config", "com.highrise.isimwebapp.config.customauth", "com.highrise.isimwebapp.config.servlet3", "com.highrise.isimwebapp.web", "com.highrise.isimwebapp.service", "com.highrise.isimwebapp.dao",
"com.highrise.isimwebapp.exception", "com.highrise.isimwebapp.validator" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}

@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource rb = new ResourceBundleMessageSource();
rb.setBasenames(new String[] { "messages/messages", "messages/validation" });
return rb;
}

}

最佳答案

问题可能出在包扫描的顺序上,我可以建议您两种方法:

  1. @ComponentScan("com.highrise.isimwebapp.config.customauth") 移至 SecurityConfiguration 类。
  2. CustomAuthenticationProvider 类中删除 @Component 注解,并在 SecurityConfiguration 中声明 @Bean,某些内容像下面这样:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthProvider());
}

@Override
protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.antMatchers("/icon/**").permitAll()
.anyRequest().authenticated()
.and().formLogin().loginPage("/login").usernameParameter("username").passwordParameter("password").permitAll().defaultSuccessUrl("/persons/listall")
.and().csrf().disable();

}

@Bean
public CustomAuthenticationProvider customAuthProvider() {
return new CustomAuthenticationProvider();
}
}

关于java - 无法使用 Spring Security 实现 CustomAuthenticationProvider : BeanCreationException while autowiring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60589062/

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