【发布时间】:2018-08-22 18:46:33
【问题描述】:
我想使用 bcrypt 密码编码器,据我了解,它会自动对密码进行散列和加盐。
我的代码看起来像这样:
@Configuration
@EnableWebSecurity
public class BasicAuthConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder(){return new BCryptPasswordEncoder();}
@Autowired
private ConfigService configService;
// Authentication : User --> Roles
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication().passwordEncoder(passwordEncoder())
.withUser(configService.getUsers().getUsername())
.password(configService.getUsers().getPassword())
.roles("USER");
}
// Authorization : Role -> Access
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic()
.and().authorizeRequests()
.antMatchers("/actuator/**")
.permitAll()
.antMatchers("/tokenservice/**")
.hasRole("USER")
.antMatchers("/")
.permitAll()
.and().csrf()
.disable()
.headers()
.frameOptions()
.and().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
它像这样工作,没有编码 {noop}。但是当我这样做时,我收到以下错误:(一行sry,向右滚动)
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext:99 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NullPointerException
configService.getConfigurations.getUsername 和密码是从 xml 文件中读取的
**编辑 好的,所以我已经验证了 2 个用户存在,我认为问题出在我试图给他们打电话的方式上。它们存在于配置列表中。 configuration.getUsers() 返回两个用户。那么我该如何调用 .withUser() 中的任何用户呢?
类似 configService.getConfigurations() //返回配置 .getUsers() //返回用户列表 .getsomethinghere??
【问题讨论】:
标签: spring spring-boot spring-security basic-authentication bcrypt