@Configuration
@EnableWebSecurity
//@EnableOAuth2Sso
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
@Autowired
private JwtRequestFilter jwtRequestFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
// dont authenticate this particular request
.authorizeRequests()
.antMatchers(
"/api/login",
"/user/create-new-user",
"/user/get-verification",
"/user/pwd-reset",
"/user/pwd-reset/verification",
"/api/swagger-ui.html")
.permitAll()
// .antMatchers("/**").permitAll().hasRole("ADMIN")
.anyRequest()
.fullyAuthenticated()
.and()
.exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
// .and()
// .logout()
// .logoutRequestMatcher(new AntPathRequestMatcher("/api/logout")).logoutSuccessUrl("/https://www.baeldung.com/spring-security-logout")
// .invalidateHttpSession(true).deleteCookies("JSESSIONID");
// Add a filter to validate the tokens with every request
http
.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
}
или используйте этот способ
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/home").access("hasRole('USER')")
.antMatchers("/admin/**").hasRole("ADMIN")
.and()
// some more method calls
.formLogin();
}