Break Servlet Filter

I also have the SecurityAuthorisationFilter

following piece of code:

for(String anonymousRequestURI : anonymousRequestURIs) {
     if(httpRequest.getRequestURI().equals(anonymousRequestURI)) {
         //allow request
     }
 }
 String authToken = getTokenFromRequest(httpRequest);

      

If equals is true, I need to skip all subsequent filter operations and send the request to the controller. But if I write filterChain.doFilter(servletRequest, servletResponse)

, there will be no result. The filter continues to work to find the token. How can I get out of the filter?

+3


source to share


2 answers


As I understand from your comment, you want to "break" out of the current filter if the anonymous request URI condition is true. You can simply return;

after the call chain.doFilter()

:

for ( String uri : anonURIs ) {
    if ( ... ) {
        chain.doFilter( ... );
        return;
    }
}

String authToken = getTokenFromRequest( httpRequest );
...

      



Greetings,

+2


source


Ok, if it break

doesn't give you what you need, you can change your loop like this:



int i=0;
do {
      anonymousRequestURI = anonymousRequestURIs[i];
      i++;
while(!httpRequest.getRequestURI().equals(anonymousRequestURI) && i<anonymousRequestURIs.length);
String authToken = getTokenFromRequest(httpRequest);

      

+1


source







All Articles