I have looked up articles about using Spring Security, but they do not quite answer my need. They usually deal with in-memory users and plaintext passwords.
What I want to achieve:
Clients authenticate using Basic Authentication.
Secured Controller methods look like this:
@RequestMapping(value = "/test")
public ResponseEntity test(@AuthenticationPrincipal MyUser user){
// Logic
}
I want the controller method to get a MyUser object in its parameters if the given user is authenticated or null if not.
I am storing hashed password and salt of a user in a database, and I have a service which authenticates given username-password pair and returns a User object or null if authentication succeeded or failed, respectively.
All that needs to happen now is to intercept every request, look at the basic authentication header, pass the info to my custom authentication service and then pass its result into the controller method. Unfortunately I haven't been able to do that.
I read on using custom UserDetailsService
, but that only seems to handle fetching user data from database, and not any authentication. I cannot figure out how to plug all the components together.
Can you point me in the right direction how to get this rather simple workflow working?
EDIT
I am not using XML config and the only security related config I have is:
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**").csrf().disable();
http.authorizeRequests()
.antMatchers("/user").permitAll()
.antMatchers("/user/**").permitAll()
.anyRequest().authenticated();
}
}