Here is the HttpServletRequestWrapper
Code:
public class AcegiHttpServletRequestWrapper extends HttpServletRequestWrapper {
public AcegiHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
}
public boolean isUserInRole(String role) {
return isGranted(role);
}
public String getRemoteUser() {
Authentication auth = getAuthentication();
if (auth == null)
return null;
return ((net.sf.acegisecurity.providers.dao.User) auth.getPrincipal()).getUsername();
}
private Authentication getAuthentication() {
if (ContextHolder.getContext() != null && ContextHolder.getContext() instanceof SecureContext) {
return ((SecureContext) ContextHolder.getContext()).getAuthentication();
}
return null;
}
private boolean isGranted(String role) {
Authentication auth = getAuthentication();
if (auth == null)
return false;
for (int i=0; i < auth.getAuthorities().length; i++) {
if (role.equals(auth.getAuthorities()[i].getAuthority()))
return true;
}
return false;
}
}
Here is the filter
Code:
public class AcegiHttpServletRequestFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
if (!(request instanceof AcegiHttpServletRequestWrapper)) {
request = new AcegiHttpServletRequestWrapper(request);
}
filterChain.doFilter(request, servletResponse);
}
public void destroy() {}
}
And here is the web.xml:
<filter>
<filter-name>Acegi Http Servlet Request Filter</filter-name>
<filter-class>packagename.AcegiHttpServletRequestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Acegi Http Servlet Request Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>