私はSpring Securityとパスワードの暗号化に問題があります。MsSQLです。 RESTアプリではSpring 4
,Hibernate
、Spring Data JPA
を使用しています。私は、正しい資格情報を使用してログインしようとしたときしかし、私は唯一のSpring Securityは独自の暗号化を認識しません
WARN 4780 --- [io-8080-exec-61] o.s.s.c.bcrypt.BCryptPasswordEncoder
:Encoded password does not look like BCrypt
を取得し、Bcrypt
とパスワードの暗号化を実装しようとしています。そして、明らかにアクセスは拒否されます。私が知っていることをしようとしたかどう
:DBにおけるパスワードのbcryptの暗号化された文字列
- パスワードは、正しく保存されている
- 追加AuthenticationManagerBuilderへの
auth.jdbcAuthentication().dataSource(dataSource)
は何も変更しませんでした。 - DBにパスワードを要求すると、格納されているものが返されます。暗号化されたパスワードを暗号化します。
私はすべてをエンコードするためにPasswordEncoderの同じインスタンスを使用しているので、すべてのことはちょっと奇妙です。そしてそれはそれ自身の暗号化を認識しません。私が持っているもの:
構成:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private RESTAuthenticationEntryPoint authenticationEntryPoint;
@Autowired
private RESTAuthenticationFailureHandler authenticationFailureHandler;
@Autowired
private RESTAuthenticationSuccessHandler authenticationSuccessHandler;
@Autowired
private UserDetailsService userAuthService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/home", "/").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/addGame").hasRole("USER")
.and()
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.formLogin()
.successHandler(authenticationSuccessHandler)
.failureHandler(authenticationFailureHandler);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userAuthService);
authProvider.setPasswordEncoder(encoder());
return authProvider;
}
@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}
UserDetailsService:
@Service
public class UserAuthService implements UserDetailsService{
@Autowired
UserDatabaseService userDatabaseService;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserDto user = userDatabaseService.getUserByUsername(username);
if (user == null){
throw new UsernameNotFoundException(username);
} else{
return new MyUserPrincipal(user);
}
}
}
UserDatabaseService(春データで実装):
@Service
public class UserDatabaseService {
@Autowired
UserDatabaseRepository userDatabaseRepository;
@Autowired
UserToUserDtoConverter userToUserDtoConverter;
@Autowired
UserDtoToUserEntityConverter userDtoToUserEntityConverter;
@Autowired
PasswordEncoder passwordEncoder;
public UserDto getUserByUsername(String username){
return userToUserDtoConverter.convert(userDatabaseRepository.findByUsername(username));
}
public boolean saveUser(UserDto user){
user.setPassword(passwordEncoder.encode(user.getPassword()));
if (userDatabaseRepository.save(userDtoToUserEntityConverter.convert(user)) != null){
return true;
} else{
return false;
}
}
}
正直に言うと、私はいただきました!間違って本当に知りません。 http://www.baeldung.com/spring-security-authentication-with-a-database http://www.baeldung.com/spring-security-registration-password-encoding-bcrypt
すべてのヘルプははるかに高く評価されます。私は、これらの2つのチュートリアルを追ってきました。
EDIT:エンティティにDTOクラスを変換するために使用されるコンバータ(およびその逆)
@Service
public class UserDtoToUserEntityConverter {
public UserEntity convert(UserDto user){
return new UserEntity(user.getFirstName(), user.getLastName(), user.getUsername(), user.getPassword() , user.getEmail());
}
public Collection<UserEntity> convertAll(Collection<UserDto> fElements){
Collection<UserEntity> convertedElement =
fElements.stream()
.map(element -> convert(element))
.collect(Collectors.toList());
return convertedElement;
}
}
@Service
public class UserToUserDtoConverter implements UserDtoConverter {
@Override
public UserDto convert(UserEntity from) {
return new BaseUserDto(from.getFirstName(), from.getLastName(),
from.getUsername(), from.getPassword(),
from.getEmail());
}
}
MyUserPrincipal:
public class MyUserPrincipal implements UserDetails{
private UserDto user;
public MyUserPrincipal(UserDto user) {
this.user = user;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public String getUsername() {
return user.getUsername();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
@dur元の質問を編集しました。もしあなたが見たら、本当に感謝しています。それらの変換者は多くのあなたをしません。 – Stompy
私はそれを試してみました - それは正しくエンコードされたパスワードを返します。しかし、エンコーディングだけでなく、何も間違っています - それがなければ私もログインできません。SpringのデータはHibernateか何かを憎んでいますか? – Stompy
MyUserPrincipalは、Spring Securityの「UserDetails」インタフェースを実装するクラスです。私は質問にコードを追加した、それはまた、本当に多くを行うことはありません... – Stompy