I am having trouble getting authenticated on a rest API I just put together using Spring Boot and maven. I just cannot get authenticated at all.
Here is my current security configuration :
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/person/getAll").permitAll()
.anyRequest().authenticated();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
and here is that "/person/getAll" endpoint...
@RequestMapping("/getAll")
List<Person> getAllPeople()
{
log.info("Getting all users in system.");
return repository.findAll();
}
Now, what I have here, is an endpoint that actually works - but it is unauthenticated (Because I have it listed in the perminAll section of the security configuration) -
But now I want to make this so I have to have the "ROLE_USER" role - in order to get back my list of people.
I've tried removing it from the permit all, and adding @PreAuthorise("hasRole('ROLE_USER')") above the method - but that just results in a 403 response - which I wasn't really expecting at all. I figured that would allow me to run either of the following curl commands and see data :
curl --user user:password localhost:8080/person/getAll
or
curl user:password@localhost:8080/person/getAll
For everyone's sanity - when I use the code as listed in this question currently (I get data back - even if the password is wrong, haha)
Long term, my goal is actually to remove this in memory stuff and do the usual queries to get credentials, but I just want to make sure my stuff works for now.