Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot + Spring Security permitAll() and addFilter() configuration does not have effect

  1. URL patter with /login should go through the LoginFilter where the user id and password is validated - working fine
  2. URL pattern with /users/register should not go through any of the filer but it is always passing through the JWTAuthentication filter - not working fine
  3. All other URL pattern should go through the JWTAuthentication filter for authorization - working fine

Below is my code for Security Configuration. Kindly help me with what I am missing in this code. How do I configure the filter such that JWT authentication happens for the URL pattern other than /login and /register

Spring-security-core:4.2.3, spring-boot:1.5.4

 protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers(HttpMethod.POST, "/login").permitAll()
            .antMatchers(HttpMethod.POST, "/users/register").permitAll()
            .anyRequest().authenticated()
            .and()
            // We filter the api/login requests
            .addFilterBefore(new LoginFilter("/login", authenticationManager()),
                    UsernamePasswordAuthenticationFilter.class)
            // And filter other requests to check the presence of JWT in header
            .addFilterBefore(new NoLoginAuthenticationFilter("/users/register"), UsernamePasswordAuthenticationFilter.class)
            .addFilterBefore(new JWTAuthenticationFilter("/**", authenticationManager()),
                    UsernamePasswordAuthenticationFilter.class);

}
like image 569
Selva Avatar asked Mar 29 '18 17:03

Selva


People also ask

How do I disable spring boot security configuration?

To disable Security Auto-Configuration and add our own configuration, we need to exclude the SecurityAutoConfiguration class from auto-configuration. If you have spring-boot-actuator included in your dependecies then you need to exclude ManagementWebSecurityAutoConfiguration class from auto-configuration.

How do I fix WebSecurityConfigurerAdapter?

Declare a bean of type AuthenticationProvider:authenticationProvider(authenticationProvider()); That's how to remove the warning “The type WebSecurityConfigurerAdapter is deprecated” in Spring-based application with Spring Security.


1 Answers

What you want is to ignore certain URLs. For this override the configure method that takes WebSecurity object and ignore the pattern.

Try adding below method override to your config class.

@Override
public void configure(WebSecurity web) throws Exception {
    web
    .ignoring()
    .antMatchers("/users/register/**");
}
like image 50
Mahesh_Loya Avatar answered Nov 15 '22 07:11

Mahesh_Loya