一款簡單實用的請求過濾以及session逾時處理

來源:互聯網
上載者:User

代碼源於網際網路共用起初只是針對請求的攔截後來我給加上了session驗證

package com.ifan.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionMapping;
import org.json.simple.JSONObject;

/**
 * 許可權驗證過濾器,副業是亂碼糾
 */
public class VerificationFilter implements Filter{
    
    /**
     * HttpServletRequest對象
     */
    private HttpServletRequest thisRequest;
    /**
     * HttpServletResponse對象
     */
    private HttpServletResponse thisResponse;
    /**
     * 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;
    /**
     * init
     */
    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;
        }
    }
    /**
     * destroy
     */
    public void destroy() {
        this.encoding = null;
        this.filterConfig = null;
    }
    /**
     * doFilter
     */
    public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
        // Conditionally select and set the character encoding to be used
        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);
            if (encoding != null){
                request.setCharacterEncoding(encoding);
            }
        }
        thisRequest = (HttpServletRequest)request;
        thisResponse = (HttpServletResponse)response;
        String loginType=loginType=thisRequest.getParameter("loginType")==null?"":thisRequest.getParameter("loginType");
        if(loginType.equals("2")){

         //這是擷取action請求的
            String path = thisRequest.getRequestURI()+"?"+thisRequest.getQueryString();

     //這裡攔截是不是登入或者登出方法 除了這2個方法其餘的都要驗證
            if(!path.contains("user.php?users=loginByAcount") && !path.contains("user.php?users=logout")&& !path.contains("user.php?users=loginTimeOut")){
     //下面這段很簡單了 就是判斷如果session不存在了 那就是登入逾時   由於配合試用dwz架構 所以反悔的資料格式是json 大家可以改成返回一個頁面       

        HttpSession session=thisRequest.getSession(true);
                String admin=(String)session.getAttribute("uacount");
                if(admin==null||admin==""){
                    String relId=thisRequest.getParameter("relId");
                    try {
                          String resultStr="{\"statusCode\":\"301\",\"message\":\"當前會話已逾時,請重新登入!\",\"navTabId\":\" \",\"callbackType\":\" \",\"forwardUrl\":\" \"}";
                          thisResponse.setContentType("text/json;charset=utf-8");
                          thisResponse.getWriter().print(resultStr);      
                              return ;
                        } catch (IOException e) {
                            e.printStackTrace();                  
                   }
                }
            }
                         

        }
        chain.doFilter(thisRequest,thisResponse);
    }
    /**
     * return this.encoding
     * @param request
     * @return
     */
    protected String selectEncoding(ServletRequest request) {
        return (this.encoding);
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.