Skip to content

Instantly share code, notes, and snippets.

@zEvg
Created March 21, 2014 14:22
Show Gist options
  • Save zEvg/9687276 to your computer and use it in GitHub Desktop.
Save zEvg/9687276 to your computer and use it in GitHub Desktop.
package com.velti.bb.platform.security;
import org.springframework.security.web.util.RequestMatcher;
import javax.servlet.http.HttpServletRequest;
/**
* This request matcher is used in jungle of Spring Security
* in order to decide what requests should be cached in case of user
* is visiting some page while he's not authenticated.
* @author [email protected]
*/
public class SmartRequestMatcher implements RequestMatcher {
public static final String X_REQUESTED_WITH_HEADER = "X-Requested-With";
public static final String XML_HTTP_REQUEST = "XMLHttpRequest";
@Override
public boolean matches(HttpServletRequest request) {
// match only get requests
// ignore ajax
// ignore static resources
return isGet(request) && isNotAjax(request) && isNotStaticResource(request) && isNotError(request);
}
private boolean isNotStaticResource(HttpServletRequest request) {
return !request.getRequestURI().matches("(.*)\\.(css|js|png|gif|jpeg|jpg|ico)");
}
private boolean isNotAjax(HttpServletRequest request) {
String header = request.getHeader(X_REQUESTED_WITH_HEADER);
return !(header != null && header.equalsIgnoreCase(XML_HTTP_REQUEST));
}
private boolean isGet(HttpServletRequest request) {
return "get".equalsIgnoreCase(request.getMethod());
}
private boolean isNotError(HttpServletRequest request) {
return !request.getRequestURI().matches("(.*)error(\\d*)\\.m");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment