Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break servlet-filter work

I have SecurityAuthorisationFilter and the following part of the 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 next filter operations and send request to controller. But, if I write filterChain.doFilter(servletRequest, servletResponse), there is no result. The filter continue operations to find a token. How can I break out of the filter?

like image 394
Squeez Avatar asked Jun 27 '26 05:06

Squeez


2 Answers

As I understand from your comment, you wish to "break out" of the current filter if the condition about anonymous request URI is true. You could simply return; after calling chain.doFilter():

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

String authToken = getTokenFromRequest( httpRequest );
...

Cheers,

like image 57
Anders R. Bystrup Avatar answered Jun 28 '26 19:06

Anders R. Bystrup


Well if breakdoesn'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);
like image 24
cнŝdk Avatar answered Jun 28 '26 19:06

cнŝdk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!