Не получается реализовать аутентификацию пользователя Spring Boot
Создаю класс от WebSecurityConfigurerAdapter и создаю метод:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserService userService;
@Autowired
DataSource dataSource;
private static final String USER_SQL = "SELECT" +
" t_user.username," +
" t_user.password," +
" true" +
" FROM" +
" t_user" +
" WHERE" +
" t_user.username = ?";
private static final String ROLE_SQL = "SELECT t_user.username, t_role.name" +
" FROM t_user" +
" INNER JOIN t_role ON t_role.id = (SELECT t_user.id FROM t_user WHERE t_user.username = 'root')" +
" WHERE" +
" t_user.username = ?";
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeRequests()
.antMatchers("/admin").hasRole("ADMIN")
.antMatchers("/news").hasAnyAuthority()
.anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login")
.permitAll();
}
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery(USER_SQL)
.authoritiesByUsernameQuery(ROLE_SQL)
.passwordEncoder(bCryptPasswordEncoder());
}
}
Вот SQL запросы:
private static final String USER_SQL = "SELECT" +
" t_user.username," +
" t_user.password," +
" true" +
" FROM" +
" t_user" +
" WHERE" +
" t_user.username = ?";
private static final String USER_SQL = "SELECT" +
" t_user.username," +
" t_user.password," +
" true" +
" FROM" +
" t_user" +
" WHERE" +
" t_user.username = ?";
Таблицы бд имеют вид:
@Entity
@Table(name = "t_user")
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Transient
private String passwordConfirm;
@ManyToMany(fetch = FetchType.EAGER)
private Set<Role> roles;
// Get, set, constructor
@Entity
@Table(name = "t_role")
public class Role implements GrantedAuthority {
@Id
private Long id;
@Column(name = "name")
private String name;
@Transient
@ManyToMany(mappedBy = "roles")
private Set<User> users;
public Role() {
}
Суть проблемы в том что не получается пройти аутентификацию, запросы вроде как написаны правильно но что не так не понятно. Подскажите пожалуйста