Java - Spring - 基本身份验证 - 如何散列application.properties中定义的密码

时间:2018-03-21 11:34:59

标签: java spring security authentication

我实现了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中定义的用户名和密码登录,那么使用哈希密码是什么意思?我想更像是有一个哈希键列表,并将输入密码与列表进行比较,如果成功则登录。

2 个答案:

答案 0 :(得分: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。不知道它是如何工作的,因为你没有共享代码,并且不确定它是否会与内存认证相关。但是,无论如何,我认为值得一试,这是你的方案中的正确方法。 希望它有所帮助。

答案 1 :(得分:0)

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