Я использую Spring Boot 2.2.5.RELEASE
. Я исследовал множество примеров стекового потока и других ресурсов, но мне ничего не помогло, и я не смог войти в систему, чтобы использовать h2-console
из-за предупреждения:
2020-03-31 18:22:57.545 WARN 29312 --- [nio-8080-exec-2] o.s.s.c.bcrypt.BCryptPasswordEncoder : Encoded password does not look like BCrypt
Мой класс конфигурации был таким:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfiguration
extends WebSecurityConfigurerAdapter {
private final UserInfoDetailsService userInfoDetailsService;
private BCryptPasswordEncoder passwordEncoder;
private DataSource dataSource;
public SpringSecurityConfiguration(UserInfoDetailsService userInfoDetailsService) {
this.userInfoDetailsService = userInfoDetailsService;
this.dataSource = new Jdbc3PoolingDataSource();
this.passwordEncoder = new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(userInfoDetailsService);
authenticationProvider.setPasswordEncoder(passwordEncoder);
return authenticationProvider;
}
@Override
protected void configure(
AuthenticationManagerBuilder authenticationManagerBuilder)
throws Exception {
authenticationManagerBuilder
.userDetailsService(userInfoDetailsService)
.passwordEncoder(passwordEncoder)
.and()
.authenticationProvider(authenticationProvider())
.jdbcAuthentication()
.dataSource(dataSource);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/api/user/**", "/h2-console/**").hasRole("ADMIN").anyRequest()
.authenticated()
.and()
.httpBasic()
.realmName("User Registration System")
.and()
.csrf()
.disable();
}
}
UserInfo
объект домена был похож на
@Entity
@Table(name = "users")
public class UserInfo {
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column(name = "username")
@NotEmpty
private String username;
@Column(name = "password")
@NotEmpty
private String password;
@Column(name = "enabled")
private boolean isEnabled;
@Column(name = "role")
private String role;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isEnabled() {
return isEnabled;
}
public void setEnabled(boolean isEnabled) {
this.isEnabled = isEnabled;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
И UserInfoDetailsService
класс был похож на:
@Service
public class UserInfoDetailsService implements UserDetailsService {
private final UserInfoJpaRepository userInfoJpaRepository;
public UserInfoDetailsService(UserInfoJpaRepository userInfoJpaRepository) {
this.userInfoJpaRepository = userInfoJpaRepository;
}
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
UserInfo user = userInfoJpaRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException(
"Opps! user not found with user-name: " + username);
}
return new User(
user.getUsername(), user.getPassword(),
Collections.singleton(getAuthorities(user)));
}
private SimpleGrantedAuthority getAuthorities(UserInfo user) {
return new SimpleGrantedAuthority("ROLE_" + user.getRole());
}
}
Более того, я создал несколько тестовых пользователей :
INSERT INTO users (id, username, password, enabled, role) VALUES (1, 'user', 'password', true, 'USER');
INSERT INTO users (id, username, password, enabled, role) VALUES (2, 'admin', 'password', true, 'ADMIN');
INSERT INTO users (id, username, password, enabled, role) VALUES (3, 'users2', 'password', true, 'USER');