【问题标题】:Java - Spring - Basic authentication - How to hash the password defined in application.propertiesJava - Spring - 基本身份验证 - 如何散列 application.properties 中定义的密码
【发布时间】:2018-08-30 12:16:27
【问题描述】:

我正在为 Spring Boot 应用程序实现基本身份验证,并在 application.properties 类中定义我的凭据,但我想对密码进行哈希编码,然后检查哈希值是否与 application.properties 中密码的哈希值相同然后我可以登录。如果可以在配置方法中完成所有逻辑,那就太好了。

application.properties:

基本身份验证

user.name=test
user.password={noop}example

SecurityConfig 类:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

    private AuthenticationProvider authenticationProvider;

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

        http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic()
                .and().sessionManagement().and().authenticationProvider(authenticationProvider)
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    }

更新代码

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

    @Value("${security.user.password}")
    private String password;
    @Value("${security.user.name}")
    private String username;

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

        http.csrf().disable().authorizeRequests().anyRequest().authenticated()
                .and().logout().and().httpBasic().and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);


    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().
                passwordEncoder(passwordEncoder()).withUser(username).password(password);
    }

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

    @Bean
    public String generateHashedPassword(String password) {
        return BCrypt.hashpw(password, BCrypt.gensalt(10));
    }
}

更新 2

目前它的工作方式是当我启动应用程序时,我访问 localhost:8080 然后出现一个登录弹出窗口,我输入用户名和密码(在 application.properties 中定义)

如果我输入了正确的用户名和密码,我就会登录,但如果我设法使用 application.properties 中定义的用户名和密码登录,那么对密码进行哈希处理有什么意义?我想更像是拥有一个散列键列表并将输入密码与列表进行比较,如果成功则登录。

【问题讨论】:

标签: java spring security authentication


【解决方案1】:

由于您想在属性文件中定义您的凭据,我想您可以利用内存身份验证。请尝试以下操作:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

    private AuthenticationProvider authenticationProvider;

    @Value("${user.name}") 
    private String userName;

    @Value("${user.password}") 
    private String userHashedPassword; // hashed password

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

    http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic()
            .and().sessionManagement().and().authenticationProvider(authenticationProvider)
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth
           .inMemoryAuthentication()
           .passwordEncoder(passwordEncoder())
           .withUser(userName)
           .password(userHashedPassword);
    }

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

请注意,在这种情况下,您的密码应首先使用BCryptPasswordEncoder 加密,然后您应将其放入属性文件(您可以使用其encoder.encode("password") 方法)。或者,您可以根据需要使用PasswordEncoder 的任何其他实现。我还注意到您正在使用一些自定义 autenticationProvider。由于您没有共享代码,因此不确定它是如何工作的,并且不确定它是否会与内存认证一起使用。但是,无论如何,我认为值得一试,这是在你的场景中的正确方法。 希望对您有所帮助。

【讨论】:

  • 您好,谢谢您的回答!我将更新我的代码并发布它。我添加了一种加密密码的方法,但如何在 application.properties 中再次保存?
  • @Genesis,等等,你为什么要在更新的代码中对你的密码进行哈希处理?也许我误解了你的问题。你想在你的属性文件中存储散列密码,对吧?
  • 但每当我尝试运行应用程序时,我仍然会收到该错误
  • @Genesis,看起来您使用了错误的导入(错误的包)。请使用org.springframework.security.crypto.password.PasswordEncoder
  • @Genesis,至于将散列密码保存到应用程序属性中...嗯,这是个好问题 :) 您可以编写非常简单的 java 应用程序来加密您的密码 - like this one。然后您可以将结果放入您的 application.properties。这可能有点 hacky,但这就是我会做的 :)
【解决方案2】:

我认为您需要像this 问题中那样实现自己的 AuthenticationProvider。在authenticate() 方法中,您可以对检索到的密码进行哈希处理,并检查它是否与您的 application.properties 中的密码匹配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-22
    • 2021-03-26
    • 2021-11-22
    • 2011-12-01
    • 2011-02-13
    • 2013-04-06
    • 2016-04-05
    • 2013-11-10
    相关资源
    最近更新 更多