gpt4 book ai didi

java - BCryptPasswordEncoder Spring Security 未定义

转载 作者:行者123 更新时间:2023-12-02 01:49:15 26 4
gpt4 key购买 nike

您好,我开发了一个 Spring Boot 应用程序(v1.5.18.BUILD-SNAPSHOT),但是当我尝试启动该应用程序时失败......

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of constructor in com.eficacia.security.WebSecurity required a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' in your configuration.

问题是我配置了 bean:

@Configuration
public class AppConfiguration {

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}

在我的 pom.xml 中:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

网络安全:

@Configuration
@EnableWebSecurity
@ComponentScan({"com.eficacia.security"})
public class WebSecurity extends WebSecurityConfigurerAdapter {

public static final String USER_REGISTRATION_URL = "/v1/user";

private UserDetailsService userDetailsService;

private BCryptPasswordEncoder bCryptPasswordEncoder;

public WebSecurity(UserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) {
this.userDetailsService = userDetailsService;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
.authorizeRequests().antMatchers(HttpMethod.POST, USER_REGISTRATION_URL).permitAll()
.anyRequest().authenticated()
.and().addFilter(new JWTAuthenticationFilter(authenticationManager(), getApplicationContext()))
.addFilter(new JWTAuthorizationFilter(authenticationManager(), getApplicationContext()))
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {

auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
}

@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
//configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200","http://opusclick.com","https://gateway2.tucompra.com.co"));
configuration.addAllowedOrigin("*");
configuration.addAllowedHeader("*");
configuration.setAllowedMethods(Arrays.asList("GET","POST","PUT","PATCH","DELETE"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}

}

网络配置器:

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

private final ApplicationContext applicationContext;
private final EntityManager entityManager;

@Autowired
public WebMvcConfiguration(ApplicationContext applicationContext, EntityManager entityManager) {
this.applicationContext = applicationContext;
this.entityManager = entityManager;
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
super.addArgumentResolvers(argumentResolvers);
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().applicationContext(this.applicationContext).build();
argumentResolvers.add(new DTOModelMapper(objectMapper, entityManager));
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
JacksonMapperConfiguration jacksonMapperConfiguration= new JacksonMapperConfiguration();
converters.add(jacksonMapperConfiguration.mappingJackson2HttpMessageConverter());
super.configureMessageConverters(converters);
}
}

我的问题很简单,这就是为什么如果确实存在该配置类,则找不到类'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder'的定义.

非常感谢!

最佳答案

看起来您的 Web 配置器类不在 com.eficacia.security 包或其子包之一中。

WebMvcConfiguration 移动到 @ComponentScan 包内的某个位置。

关于java - BCryptPasswordEncoder Spring Security 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53227149/

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