【发布时间】:2019-04-25 13:11:32
【问题描述】:
我是第一次配置 Spring Security,但当我收到此错误时,Spring 似乎无法看到我的客户端的原始密码。
o.s.s.c.bcrypt.BCryptPasswordEncoder : Empty encoded password
这似乎是一个明显的问题,但请允许我,经过多次尝试,我还是想不通。 我的 SecurityConfig 类是...
@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
CustomUserDetailsService userDetailsService;
@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
}
}
这是我的 UserServiceDetails 服务。
公共类 CustomUserDetailsService 实现 UserDetailsService {
@Autowired
private UserRepository repo;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Optional<Owner> optionalUser = repo.findByUsername(username);
optionalUser
.orElseThrow(() -> new UsernameNotFoundException("Username not
found"));
return optionalUser
.map(CustomUserDetails::new).get();
}
}
我也配置了以下 bean
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public BCryptPasswordEncoder passwordEncoder() {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
return bCryptPasswordEncoder;
}
} 这是我的用户服务。
public class CustomUserDetails extends Owner implements UserDetails {
public CustomUserDetails(final Owner owner) {
super();
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return getRoles().stream()
.map(role -> new SimpleGrantedAuthority("ROLE_"+getRoles()))
.collect(Collectors.toList());
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
我肯定错过了什么,但我似乎无法弄清楚。从 HttpRequest 中,我知道密码正在发布到系统,正如我登录的那样。
【问题讨论】:
-
您可以发布您的 UserDetailService 吗?
-
你持久化密码的时候有没有使用BCEncoder对密码进行编码?
-
@AokoQin,我用BCrypt来持久化密码,在数据库中得到了6个字符的哈希密码。
-
@IntegralMaster,6?db中的密码有长度限制吗?应该是60。
-
@slimane 我已经发布了,你是说 UserService 吗?我也刚刚编辑并发布了它。
标签: java spring spring-security passwords bcrypt