【问题标题】:spring security Encoded password does not look like BCryptspring security 编码密码看起来不像 BCrypt
【发布时间】:2021-03-20 01:28:52
【问题描述】:

我正在使用 Spring Security 构建 MVC

我的安全配置:

@Configuration
@Slf4j
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;

@Qualifier("userDetailsServiceImpl")
@Autowired
UserDetailsService userDetailsService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    auth
            .userDetailsService(userDetailsService)
            .passwordEncoder(getEncoder());

}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/**").hasAnyRole("admin")
            .and()
            .formLogin()
            .loginPage("/login").permitAll();
    log.debug("http object", http);
}

@Bean
public BCryptPasswordEncoder getEncoder() {
    return new BCryptPasswordEncoder();
}
}

我的 UserDetailsS​​erviceImpl:

@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserRepository userRepository;

@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Optional<UsersEntity> optionalUser = userRepository.findByUserName(username);
    if (optionalUser.isPresent()) {
        UsersEntity user = optionalUser.get();
        List<String> roleList = new ArrayList<String>();
        for(RoleEntity roleEntity : user.getRoleList()){
            roleList.add(roleEntity.getRole());
        }
        Boolean disabled = ((user.getDisabledFlag() == 0) ? false : true);

        UserDetails userToBuild = User.builder()
                .username(user.getUserName())
                .password(user.getPassword())
                .disabled(false)
                .accountExpired(false)
                .credentialsExpired(false)
                .accountLocked(false)
                .roles(roleList.toArray(new String[0]))
                .build();
        return userToBuild;
    } else {
        throw new UsernameNotFoundException("Username not found");
    }
}
}

由于某种原因,我收到错误编码密码看起来不像 BCrypt

我确信这是一个很容易解决的错误,但我不知道我做错了什么。 DB 中的密码是 BCrypt。在不同的应用程序(PHP)中使用没有问题。现在我正在学习 Spring Boot 并使用相同的数据库做另一个应用程序。我关注https://www.yawintutor.com/spring-boot-security-database-authentication-using-userdetailsservice-example,但那里的密码在数据库中是普通的,所以想使用散列的。

【问题讨论】:

标签: spring-security bcrypt


【解决方案1】:

我在本地测试,所以我使用在线生成器进行散列。我检查另一个,这个工作。与一些在线生成器相比,它看起来像 spring 使用了一些不同的算法。

【讨论】:

    猜你喜欢
    • 2020-05-26
    • 2019-03-25
    • 1970-01-01
    • 2021-12-12
    • 2018-10-09
    • 2017-04-03
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多