WEB.XML
authFilter
jp.cofesta.framework.web.filter.AuthorityFilter
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
struts2
org.apache.struts2.dispatcher.FilterDispatcher
<filter-mapping>
<filter-name>authFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
过滤器 实现类
public class AuthorityFilter extends OncePerRequestFilter {
private String error = "/logout.action";
/**
- error action setter method
- @param error */ public void setError(String error) { this.error = error; }
/**
- filter
- @param request
- @param response
- @param filterChain */ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException { String requestURI = request.getRequestURI(); //String shortRequestURI = requestURI.replace(request.getContextPath(),""); //ServletActionContext sac = new ServletActionContext(null,null); ServletActionContext.setRequest(request); User user = SecurityUtil.getLoginUser(); if (requestURI.endsWith(error)) { filterChain.doFilter(request, response); }else if (user == null && !requestURI.endsWith(Constants.LOGINACTION) && !requestURI.endsWith(Constants.WELCOMEACTION)) { String forwardPath = request.getContextPath() + error; (new HttpServletResponseWrapper(response)) .sendRedirect(forwardPath); } else { filterChain.doFilter(request, response); } } }
错误信息如下:
2010-8-25 13:34:56 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
at org.apache.struts2.ServletActionContext.setRequest(ServletActionContext.java:103)
at cc.yilian.invoicing.common.filter.AuthorityFilter.doFilterInternal(AuthorityFilter.java:51)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)