私はspring-boot-starter-security
を使用しています。 WebSecurityConfigation
にDaoAuthenticationProvider
プロバイダを、BCryptPasswordEncoder
に認証を設定しました。また、UserDetailsService
の実装では、実際のハッシュに設定されたpassword
フィールドを持つUser
オブジェクトが返されます。Java Spring Securityのパスワードハッシュで認証できる理由
正常に動作しているようです。しかし私は、パスワードまたはハッシュのいずれかで正常に認証できることに気づいた。
たとえば、パスワード自体は、生成されたUUID 51a80a6a-8618-4583-98d2-d77d103a62c6
で、$2a$10$u4OSZf7B9yJvQ5UYNNpy7O4f3g0gfUMl2Xmm3h282W.3emSN3WqxO
にエンコードされています。
完全なWebセキュリティの設定:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DemoUserDetailsService userDetailsService;
@Autowired
private DaoAuthenticationProvider authenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
auth.userDetailsService(userDetailsService);
auth.inMemoryAuthentication().withUser("user").password("password").roles("SUPER", "BASIC");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").hasRole("BASIC").and().httpBasic();
http.csrf().disable();
}
}
@Service
public class DemoUserDetailsService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
UserDAO userDAO = userRepository.findByEmailAndActivated(email);
if (userDAO == null) {
throw new UsernameNotFoundException(String.format("Email %s not found", email));
}
return new User(email, userDAO.getPasswordHash(), getGrantedAuthorities(email));
}
private Collection<? extends GrantedAuthority> getGrantedAuthorities(String email) {
return asList(() -> "ROLE_BASIC");
}
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(userDetailsService);
authenticationProvider.setPasswordEncoder(passwordEncoder);
return authenticationProvider;
}
は、なぜ私は両方の文字列を使用して認証することができていますか?間違ったことをやっているのですか?私はドキュメントで何かを見つけることができませんでした。
オッズは何ですか。ただし、エンコードされた文字列をエンコードすることでチェックしてください。これは同じ出力になる可能性があります(私はそれを本当に疑っています)。結果のハッシュは一意ではないので、これは可能ですが、可能性は低いですが、 – AxelH
が設定を表示するため、私はbcryptでセキュリティを実装しています。 –
@shutdown -hあなたはどのような構成を正確に意味していますか? – Tom