Answer the question
In order to leave comments, you need to log in
Do I understand spring security correctly?
Hello,
trying to understand spring security. All examples are somehow inappropriate for my task or I don’t understand something. There are a lot of things and all the solutions are too different.
I have requests that have two cookies set - account and token. Account is essentially a username, and token is actually a custom token that needs to be periodically checked that it is valid.
I have created an AppPrincipal class that implements the UserDetails interface.
In the custom filter, I retrieve the above cookies and create an AppPrincipal object.
But I don't understand how to use it. As far as I understand, I need to put the principal into the SecurityContextHolder somehow.
If I understood everything correctly, I need to call
SecurityContextHolder.getContext().setAuthentication()
@RequestMapping(value = "/stats/{account}", method = RequestMethod.GET)
public Mono<List<PersonalStatsRecord>> getPersonalStatsAccount(@PathVariable String account) {
return Mono.just(sessionRepository.getStatsForAccount(account));
}
Answer the question
In order to leave comments, you need to log in
You need to create a configuration for your security.
This is a Bean that inherits from WebSecurityConfigurerAdapter
In it, override the configure method. And there indicate which endpoints should go under an authorized user, and which ones you want to allow without authorization.
For example something like this:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/stats/**").permitAll()
.antMatchers(new String[]{
"/register",
"/admin/**"}).hasRole("ADMIN")
.antMatchers("/**").authenticated().and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/authenticateTheUser")
.failureUrl("/login?error=true")
.defaultSuccessUrl("/")
.and()
.logout()
.logoutSuccessUrl("/login")
.permitAll();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question