【问题标题】:Using bcrypt encoder with spring boot basic auth, springSecurityFilterChain throws NullPointerException将 bcrypt 编码器与 spring boot 基本身份验证一起使用,springSecurityFilterChain 抛出 NullPointerException
【发布时间】: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


    【解决方案1】:

    您只需要以下更改;

      protected void configure(AuthenticationManagerBuilder auth)
        throws Exception {
          auth.inMemoryAuthentication()
            .passwordEncoder(passwordEncoder())          
            .withUser(configService.getConfigurations().getUsername1())
            .password(configService.getConfigurations().getPassword1())
            .roles("USER");
      }
    

    并且在您的 XML 文件中使用哈希值中的密码。您可以使用如下所示的小代码 sn-p 获取哈希值。

    System.out.println(new BCryptPasswordEncoder().encode("yourpassword"));
    

    另一件事是你可以试试SCryptPasswordEncoder,这是我之前为 Spring Security 项目贡献的,它更安全。

    【讨论】:

    • 嗯,好吧,试过了,但出于某种原因,SpringSecurityFilterChain 现在抛出一个 nullPointExeption :S
    • 我已经编辑了我的问题以匹配你写的内容,以及新的例外@shazin
    猜你喜欢
    • 2019-04-25
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2017-07-31
    • 2021-03-26
    • 2018-06-21
    • 2018-01-22
    • 2018-11-10
    相关资源
    最近更新 更多