Dharita Chokshi
Dharita Chokshi

Reputation: 1263

Add a filter to validate path variable for every HTTP request in spring security

I'm working on Server application build in spring-boot based on micro service architecture which is already having Spring Security to handle form based authentication. Now the requirement is that each of the incoming Restful requests will have a country code and area code. I need to validate if the codes passed in path variable are same as local codes or not.

I am not able to figure out how and where to trigger or hook on to Spring Security to add the filter so that each of the requests is validated before it comes to rest controller and in case the codes are not valid, the filter itself sends 400 status code as the response.

I thought there should be something in Spring Security that can be extended or customized to do this?

Upvotes: 0

Views: 1346

Answers (1)

Mehraj Malik
Mehraj Malik

Reputation: 15874

You need to create your custom filter by extending the GenericFilterBean override the doFilter method.

Then in your custom implementation of WebSecurityConfigurerAdapter add the above custom filter in HttpSecurity.

@Configuration
public class CustomWebSecurityConfigurerAdapter
  extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.addFilterAfter(
          new MyCountryCodeFilter(), BasicAuthenticationFilter.class);
    }
}

Upvotes: 1

Related Questions