权限过滤器Filter

xiaoxiao2021-03-01  16

package com.baitw.struts.utils;import java.io.IOException;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.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;/** * * 权限过滤器 * * */public class AuthorityFilter implements Filter { private FilterConfig config; //过滤器核心方法 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub //获取Filter配置参数 String encoding=config.getInitParameter("encoding"); String loginPage=config.getInitParameter("loginPage"); String proLogin=config.getInitParameter("proLogin"); //设置request编码 request.setCharacterEncoding(encoding); HttpServletRequest requ=(HttpServletRequest) request; HttpSession session=requ.getSession(true); //获取客户请求页面 String requestPath=requ.getServletPath(); /* //获取cookie Cookie[] cookies=requ.getCookies(); Cookie c=cookies[0]; String val=c.getValue(); */ /** * 如果session范围内的user为null,即表明没有登录 * 且用户请求的既不是登录页面,也不是处理登录页面 * */ if(session.getAttribute("username")==null &&!requestPath.endsWith(loginPage) &&!requestPath.endsWith(proLogin) ){ System.out.println("权限拦截器的消息:"+"终止"); request.getRequestDispatcher(loginPage) .forward(request, response); }else{ System.out.println("权限拦截器的消息:"+"放行"); chain.doFilter(request, response); } } public void init(FilterConfig config) throws ServletException { // TODO Auto-generated method stub this.config=config; } public void destroy() { // TODO Auto-generated method stub this.config=null; }} <!-- 权限拦截器 --> <filter> <filter-name>authority</filter-name> <filter-class>com.baitw.struts.utils.AuthorityFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> <init-param> <param-name>loginPage</param-name> <param-value>/view/login.jsp</param-value> </init-param> <init-param> <param-name>proLogin</param-name> <param-value>/view/admin/main.jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>authority</filter-name> <url-pattern>/admin/*</url-pattern> </filter-mapping>
转载请注明原文地址: https://www.6miu.com/read-3650059.html

最新回复(0)