国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

登錄攔截功能

2019-11-15 00:41:53
字體:
來源:轉載
供稿:網友
登錄攔截功能

1 在web.xml中配置filter(要放在字符集過濾器之后,否則字符過濾會失效)

<!-- 登錄攔截 --> <filter>        <display-name>LoginFilter</display-name>        <filter-name>LoginFilter</filter-name>        <filter-class>com.xxx.common.LoginFilter</filter-class> </filter> <filter-mapping>        <filter-name>LoginFilter</filter-name>        <url-pattern>/*</url-pattern> </filter-mapping>

2.攔截器類(登錄,注冊,靜態文件.js.CSS等不進行過濾,放過去)

package com.xxx.common;import java.io.IOException;import java.io.Writer;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONObject;import org.sPRingframework.beans.factory.annotation.Autowired;import org.springframework.context.applicationContext;import org.springframework.stereotype.Component;import org.springframework.web.context.support.WebApplicationContextUtils;import com.xxx.entity.User;import com.xxx.service.LoginService;import com.xxx.util.CommonUtil;public class LoginFilter implements Filter {    private LoginService loginService;    public LoginService getLoginService() {        return loginService;    }    public void setLoginService(LoginService loginService) {        this.loginService = loginService;    }    public LoginFilter() {    }    /**     * 初始化     */    public void init(FilterConfig config) throws ServletException {        ServletContext context = config.getServletContext();        ApplicationContext ctx = WebApplicationContextUtils                .getWebApplicationContext(context);        loginService = (LoginService) ctx.getBean(LoginService.class);    }    public void destroy() {        loginService = null;    }    /**     * 登錄攔截     */    public void doFilter(ServletRequest req, ServletResponse res,            FilterChain chain) throws IOException, ServletException {        HttpServletRequest httpRequest = (HttpServletRequest) req;        HttpServletResponse httpResponse = (HttpServletResponse) res;        String path = CommonUtil.getRequestURL(httpRequest);                if (path.indexOf("/tologin") != -1 || path.indexOf("/login") != -1                || path.indexOf("/include") != -1) {            chain.doFilter(req, res);        } else {            User user = loginService.getCurrentUser();            if (user == null) {                boolean isAjaxRequest = isAjaxRequest(httpRequest);                if (isAjaxRequest) {                    httpResponse.setCharacterEncoding("UTF-8");                    Writer out = httpResponse.getWriter();                    JSONObject jsonObj = new JSONObject();                    jsonObj.put("success", false);                    jsonObj.put("code", "noLogin");                    jsonObj.put("message", "請您先登錄系統!");                    out.write(jsonObj.toString());                    out.flush();                    out.close();                } else {                    httpResponse.sendRedirect("/項目路徑/Login/tologin");                }            } else {                chain.doFilter(req, res);            }        }    }    /**     * 判斷是否為Ajax請求     *     * @param request     *            HttpServletRequest     * @return 是true, 否false     */    public static boolean isAjaxRequest(HttpServletRequest request) {        String requestType = request.getHeader("X-Requested-With");        return requestType != null && "xmlhttpRequest".equals(requestType);    }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平远县| 望奎县| 读书| 新宁县| 赤峰市| 类乌齐县| 明星| 高碑店市| 济南市| 新干县| 奉新县| 南岸区| 沈丘县| 嘉鱼县| 合川市| 新疆| 青海省| 会宁县| 横峰县| 崇明县| 达拉特旗| 化德县| 奎屯市| 甘南县| 西昌市| 庆元县| 酒泉市| 科技| 礼泉县| 东海县| 上虞市| 灌南县| 互助| 舞阳县| 新建县| 兰州市| 格尔木市| 霍山县| 舟山市| 太和县| 额济纳旗|