【发布时间】: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();
}
}
我的 UserDetailsServiceImpl:
@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,但那里的密码在数据库中是普通的,所以想使用散列的。
【问题讨论】:
-
这能回答你的问题吗? Encoded password does not look like BCrypt