私はユーザー登録とログインを暗号化するためにBCryptPasswordEncoderを使用しています。登録部が正常に動作しSpringbootのセキュリティ:ログイン時にパスワードが一致しない
、それはのようなパスワードを使用してデータベースに新しいユーザーを置く:私は、ログインしてくるとき
'$2a$10$aUk/26idLhSaNmhNRTRejd03FnxxLxv6X0Uo0P4PcA4mbyy.
、ユーザ名が一致に入り、私は正常にリポジトリからユーザーを見つけます。
ユーザー名またはパスワードが間違っていると言われています。この暗号化をプログラムから削除すると、正常に動作します。だから本質的に私は暗号化されたパスワードを比較するとき何か間違っています。ここで
は私のUserDetailsService実装ロジックです:
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
System.out.println(username);
User user = userRepository.findByUsername(username);
System.out.println(user.getPassword());
if (user.getUsername().isEmpty()) {
throw new UsernameNotFoundException(
"No user found with username: "+ username);
}
boolean enabled = true;
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
return new org.springframework.security.core.userdetails.User
(user.getUsername(),
user.getPassword().toLowerCase(), enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked,
getAuthorities(Arrays.asList("ROLE_USER")));
}
private static List<GrantedAuthority> getAuthorities (List<String> roles) {
List<GrantedAuthority> authorities = new ArrayList<>();
for (String role : roles) {
authorities.add(new SimpleGrantedAuthority(role));
}
return authorities;
私もWebセキュリティファイル内のBeanを設定します。私は間違って
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder(){
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
何をしているのですか?ありがとう。
あなたのコードでは、提出されたパスワードと保存されたハッシュを比較していますか?最初にハッシュしたときと同じように、同じ塩をBCryptに渡していることを確認しましたか? – user8675309
なぜこれをやっているのですか? 'user.getPassword()。toLowerCase()'? – ESala