错误:需要一个找不到的“org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder”类型的bean
Posted
技术标签:
【中文标题】错误:需要一个找不到的“org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder”类型的bean【英文标题】:Error: Required a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' that could not be found 【发布时间】:2021-07-23 12:41:28 【问题描述】:我想通过创建一个简单的登录屏幕在我的项目中使用 Spring Boot Security,但是在运行 y 应用程序时出现这些错误
说明: com.panchmeru_studio.controller.UserController 中构造函数的参数 1 需要一个找不到的 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' 类型的 bean。
操作:考虑在您的配置中定义一个 org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' 类型的 bean。 这是我的代码。
用户控制器
package com.panchmeru_studio.controller;
import com.panchmeru_studio.entities.ApplicationUser;
import com.panchmeru_studio.repository.ApplicationUserRepository;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UserController
private ApplicationUserRepository applicationUserRepository;
private BCryptPasswordEncoder bCryptPasswordEncoder;
public UserController(ApplicationUserRepository applicationUserRepository,
BCryptPasswordEncoder bCryptPasswordEncoder)
this.applicationUserRepository = applicationUserRepository;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
@PostMapping("/record")
public void signUp(@RequestBody ApplicationUser applicationUser)
applicationUser.setPassword(bCryptPasswordEncoder.encode(applicationUser.getPassword()));
applicationUserRepository.save(applicationUser);
SecurityConfiguration.java
package com.panchmeru_studio.security;
import com.panchmeru_studio.filter.AuthenticationFilter;
import com.panchmeru_studio.filter.AuthorizationFilter;
import com.panchmeru_studio.service.ApplicationUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import static com.panchmeru_studio.constants.SecurityConstants.SIGN_UP_URL;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
private ApplicationUserDetailsService userDetailsService;
private BCryptPasswordEncoder bCryptPasswordEncoder;
public SecurityConfiguration(ApplicationUserDetailsService 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, SIGN_UP_URL).permitAll()
.anyRequest().authenticated()
.and()
.addFilter(new AuthenticationFilter(authenticationManager()))
.addFilter(new AuthorizationFilter(authenticationManager()))
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
@Bean
CorsConfigurationSource corsConfigurationSource()
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception
return super.authenticationManagerBean();
【问题讨论】:
【参考方案1】:在您的安全配置类中,您不需要此行:
private BCryptPasswordEncoder bCryptPasswordEncoder;
用这个替换它。下面的方法是向 Spring 容器提供 Password Encoder bean 以增强安全性。
@Bean
public PasswordEncoder passwordEncoder()
return new BCryptPasswordEncoder();
在您的控制器中,现在您可以将其自动连接为:
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
【讨论】:
您错过的是 Spring 的基础知识,与 Spring Security 没有特别的关系。也许,参考以下内容可以帮助您理解:***.com/questions/19414734/… 第二个困惑是密码编码器,作为概述,您只需知道在配置安全性时需要为容器提供两个 bean-UserDetailsService 和 PasswordEncoder。有关更多信息,请查看 Spring Security 文档。 但是我在几个地方使用了变量 bCryptPasswordEncoder,删除了私有 BCryptPasswordEncoder bCryptPasswordEncoder;可能会导致几个错误 不不,不是那样的。看,一旦你将 bean 提供给 spring 容器,你就不需要在任何地方声明变量和创建对象了。 Spring 在那里引入了依赖注入。它创建一个单例对象并将其保存在容器中。现在,只要您需要此对象,只需对您需要的对象类型执行 @Autowired 即可。该实例将直接从容器中可用。而且,您不需要在任何地方使用 new()。 嘿,@VJain,我在这里发布了一个与 Spring Security 非常基础相关的问答:***.com/questions/67329660/… 这可能会帮助您解决实施和困惑。【参考方案2】:错误提示,您没有任何带有密码编码器的 bean。
在您的配置类中添加该 bean 并从 SecurityConfiguration
的构造函数中删除 BCryptPasswordEncoder
:
@Bean
public PasswordEncoder encoder()
return new BCryptPasswordEncoder();
【讨论】:
以上是关于错误:需要一个找不到的“org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder”类型的bean的主要内容,如果未能解决你的问题,请参考以下文章
无法找到'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder'类型的bean
错误:需要一个找不到的“org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder”类型的bean
错误:找不到模块 'webpack/lib/rules/DescriptionDataMatcherRulePlugin' 需要堆栈: