在过滤器中判断用户登录(session过期跳转到登陆页面)
今天一直想重写DispatchAction的excute方法来判断用户session是否过期,但是由于项目中所有的Action从一开始都是extends DispatchAction,全盘替换总怕出什么问题,所以想换一种方式,考虑在进入具体action前先走过滤器,那么是否可以在过滤器中试一下呢?上网一搜果然可以,先将代码备份一下作将来参考用:
public class SetCharacterEncodingFilter implements Filter {
?// ----------------------------------------------------- Instance Variables
?/**
? * The default character encoding to set for requests that pass through this
? * filter.
? */
?protected String encoding = null;
?/**
? * The filter configuration object we are associated with. If this value is
? * null, this filter instance is not currently configured.
? */
?protected FilterConfig filterConfig = null;
?/**
? * Should a character encoding specified by the client be ignored?
? */
?protected boolean ignore = true;
?// --------------------- Public Methods
?/**
? * Take this filter out of service.
? */
?public void destroy() {
??this.encoding = null;
??this.filterConfig = null;
?}
?private final static String[] ignore_url={"/index.do"};//进入各子平台时不用坐登录check
?/**
? * Select and set (if specified) the character encoding to be used to
? * interpret request parameters for this request.
? *
? * @param request
? *??????????? The servlet request we are processing
? * @param result
? *??????????? The servlet response we are creating
? * @param chain
? *??????????? The filter chain we are processing
? *
? * @exception IOException
? *??????????????? if an input/output error occurs
? * @exception ServletException
? *??????????????? if a servlet error occurs
? */
?public void doFilter(ServletRequest request, ServletResponse response,
???FilterChain chain) throws IOException, ServletException {
??System.out.println("&&&&&&&&&&&&&&&&doFilter&&&&&&&&&&&&&&");
??// Conditionally select and set the character encoding to be used
??
??boolean haveFind = true;
??if (ignore || (request.getCharacterEncoding() == null)) {
???String encoding = selectEncoding(request);
???if (encoding != null)
???{
????request.setCharacterEncoding(encoding);
????response.setCharacterEncoding(encoding);
???}
??}
??RequestDispatcher dispatcher = request.getRequestDispatcher("logout.jsp");//这里设置如果没有登陆将要转发到的页面??
??? HttpServletRequest req = (HttpServletRequest) request;??
??? HttpServletResponse res = (HttpServletResponse) response;??
??? HttpSession session = req.getSession(true);??
???
??? if (req.getRequestURI().indexOf(".jsp") >= 0 || req.getRequestURI().indexOf(".do") >= 0)??
???????????? haveFind = false;
??? //System.out.println(haveFind);
??? for (int i = 0; i < ignore_url.length; i++) {??
???????????? if (req.getRequestURI().indexOf(ignore_url[i]) >= 0) {??
???????????????? haveFind = true;??
???????????????? break;??
???????????? }?
??? }
??? //System.out.println(req.getRequestURI()+haveFind);
???
??? // 从session里取的用户名信息??
??? String username = (String) session.getAttribute("Hthm_RealName");//这里获取session,为了检查session里有没有保存用户信息,没有的话回转发到登陆页面??
???
??? // 判断如果没有取到用户信息,就跳转到登陆页面??
??? if (!haveFind&&(username == null || "".equals(username)))??
??? {??
???? // 跳转到登陆页面??
???? dispatcher.forward(request,response);??
???? System.out.println("用户没有登陆,不允许操作");??
???????
???? res.setHeader("Cache-Control","no-store");?????
???? res.setDateHeader("Expires",0);??
???? res.setHeader("Pragma","no-cache");??
??? }??
??? else?
??? {??
???? // 已经登陆,继续此次请求??
???? chain.doFilter(request,response);??
//???? System.out.println("用户已经登陆,允许操作");??
??? }??
?
????
??// Pass control on to the next filter
??//chain.doFilter(request, response);
??
???? Map map = request.getParameterMap();
???? Set set = map.entrySet();
???? if(map!= null)
???? {
????? for(Iterator it = set.iterator();it.hasNext();)
????? {
?????? Map.Entry entry = (Entry) it.next();
?????? if(entry.getValue() instanceof String[])
?????? {
??????? String[] values = (String[]) entry.getValue();
?????? //TransHtmlTag是spring中类文件.
??????? for(int i = 0 ; i < values.length ; i++)
???????? values[i] = TransHtmlTag.TransHtmlTag(values[i]);
???????? entry.setValue(values);
?????? }
????? }
???? }
?}
?/**
? * Place this filter into service.
? *
? * @param filterConfig
? *??????????? The filter configuration object
? */
?public void init(FilterConfig filterConfig) throws ServletException {
??this.filterConfig = filterConfig;
??this.encoding = filterConfig.getInitParameter("encoding");
??String value = filterConfig.getInitParameter("ignore");
??if (value == null)
???this.ignore = true;
??else if (value.equalsIgnoreCase("true"))
???this.ignore = true;
??else if (value.equalsIgnoreCase("yes"))
???this.ignore = true;
??else
???this.ignore = false;
?}
?// ------------------------------------------------------ Protected Methods
?/**
? * Select an appropriate character encoding to be used, based on the
? * characteristics of the current request and/or filter initialization
? * parameters. If no character encoding should be set, return
? * <code>null</code>.
? * <p>
? * The default implementation unconditionally returns the value configured
? * by the <strong>encoding</strong> initialization parameter for this
? * filter.
? *
? * @param request
? *??????????? The servlet request we are processing
? */
?protected String selectEncoding(ServletRequest request) {
??return (this.encoding);
?}
}// EOC