Part 1 : Three main pillars in the Spring Security Authentication Mechanism..AbstractAuthenticationProcessingFilter
AbstractAuthenticationProcessingFilter is one of the three pillars
From the Docs
This filter will intercept the request and attempt to perform authentication from it if the request matches the setRequiresAuthenticationRequestMatcher (RequestMatcher).
If the custom url needs to be authenticated one needs to follow the below steps :
Step 1 :
Extend AbstractAuthenticationProcessingFilter and call its constructor passing the custom URL,
public CustomFilter () {
super(CUSTOM_URL);
}
Step 2 :
Override attemptAuthentication to implement custom logic, generally UsernamePasswordAuthentcationToken object is created using the credentials and custom processing is performed eventually authenticating the request using the supplied Authentication Manager.
“return this.getAuthenticationManager().authenticate(authenticatonRequest)”
Step 3 :
Write a RequestMatcher implementation,
example :
@Override
public boolean matches(HttpServletRequest request)
{
String headerParam = request.getHeader(SOME_HEADER_PARAM); if(headerParam != null) return true; return false;
}
Step 4 :
Write a custom AuthenticationProvider Implementation, and override authenticate()
example :
@Override
public Authentication authenticate (Authentication authentication) throws AuthenticationException
{ }
The above are some of the things you need to keep in mind while coding a Custom filter for Spring Security.
Comments
Post a Comment