SpringSecurity - koncept działania + BCrypt

0

Cześć,
Staram sie obecnie zaimplementować logowanie do określonej części aplikacji z użyciem spring security.
Struktura moich tabel to: users(PK id) 1 .. n users_roles(FK userid, roleid) n .. 1 roles (PK id)

Dwie metody w SecurityConfig:

@Override
	protected void configure(HttpSecurity http) throws Exception {	
	  http.authorizeRequests()
			.antMatchers("/user/register/**").access("hasRole('ROLE_USER')")
			.and()
			    .formLogin().loginPage("/user/login").failureUrl("/user/login?error")
			    .usernameParameter("username").passwordParameter("password")		
			.and()
			    .logout().logoutSuccessUrl("/user/login?logout");

	  http.csrf().disable();
		}

Powyższa metoda jest dość jasna.

Natomiast nie za bardzo rozumiem jak miałaby sie odbywać identyfikacja po haśle i sprawdzenie czy człowiek występuje w danej roli:

@Autowired
	public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
 
        final String findUserQuery = 
        		"SELECT username, password "
        		+ "FROM users "
        		+ "WHERE username = ?";

        final String findRolesQuery = 
        		"SELECT roles.rolename " 
        		+ "FROM users " 
        		+ "LEFT JOIN users_roles ON users.id=users_roles.userid " 
        		+ "LEFT JOIN roles ON roles.id=users_roles.roleid " 
        		+ "WHERE users.username = ?";
         
        auth.jdbcAuthentication().dataSource(dataSource.dataSource())
                .usersByUsernameQuery(findUserQuery)
                .authoritiesByUsernameQuery(findRolesQuery);
    }

Powyższe metody nie pozwalają na poprawne zalogowanie, stąd mam 2 pytania:
1)
**findUserQuery **znajduje usera z pola username przekazanego z formularza
**findRolesQuery **znajduje wszystkie role danego usera
Dobrze to pojmuje?
2)
Gdzie jest przekazanie/porównanie hasła? Dodatkowo hasło mam zahaszowane BCryptem i chciałbym tez to tutaj zastosować, ale nie bardzo widzę gdzie to umieścić - przechwycenie hasła z formularza, zahaszowanie i wtedy dopiero porównanie z tym w bazie...

1

Przecież Spring Security może to zrobić za ciebie.

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public DaoAuthenticationProvider authProvider() {
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService);
        authProvider.setPasswordEncoder(passwordEncoder());
        return authProvider;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
                .antMatchers("/signup").permitAll()
                .antMatchers("/static/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .headers()
                .frameOptions().disable()
                .and()
            .logout()
                .permitAll();
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authProvider());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1