web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><display-name>scm</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!-- 编码过滤器注册 --><filter><filter-name>encode</filter-name><filter-class>com.scm.filter.EncodeFilter</filter-class><!-- 配置初始化信息 --><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param></filter><!-- 登录过滤器注册 --><filter><filter-name>login</filter-name><filter-class>com.scm.filter.LoginFilter</filter-class></filter><!-- 权限过滤器注册配置 --><filter><filter-name>auth</filter-name><filter-class>com.scm.filter.AuthFilter</filter-class></filter><!-- 编码过滤器 --><filter-mapping><filter-name>encode</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 登录过滤器:过滤具体部门 --><filter-mapping><filter-name>login</filter-name><url-pattern>/system/*</url-pattern></filter-mapping><filter-mapping><filter-name>login</filter-name><url-pattern>/purchase/*</url-pattern></filter-mapping><filter-mapping><filter-name>login</filter-name><url-pattern>/warehouse/*</url-pattern></filter-mapping><filter-mapping><filter-name>login</filter-name><url-pattern>/finance/*</url-pattern></filter-mapping><filter-mapping><filter-name>login</filter-name><url-pattern>/sale/*</url-pattern></filter-mapping><filter-mapping><filter-name>login</filter-name><url-pattern>/system/*</url-pattern></filter-mapping><!-- 权限过滤器:过滤具体部门 --><filter-mapping><filter-name>auth</filter-name><url-pattern>/system/*</url-pattern></filter-mapping><filter-mapping><filter-name>auth</filter-name><url-pattern>/purchase/*</url-pattern></filter-mapping><filter-mapping><filter-name>auth</filter-name><url-pattern>/warehouse/*</url-pattern></filter-mapping><filter-mapping><filter-name>auth</filter-name><url-pattern>/finance/*</url-pattern></filter-mapping><filter-mapping><filter-name>auth</filter-name><url-pattern>/sale/*</url-pattern></filter-mapping><filter-mapping><filter-name>auth</filter-name><url-pattern>/system/*</url-pattern></filter-mapping>
</web-app>

EncodeFilter

package com.scm.filter;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.annotation.WebFilter;/*** Servlet Filter implementation class EncodeFilter*/
@WebFilter("/EncodeFilter")
public class EncodeFilter implements Filter {String encode;/*** Default constructor. */public EncodeFilter() {// TODO Auto-generated constructor stub}/*** @see Filter#destroy()*/public void destroy() {// TODO Auto-generated method stub}/*** @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)*/public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {request.setCharacterEncoding(encode);response.setCharacterEncoding(encode);chain.doFilter(request, response);}/*** @see Filter#init(FilterConfig)*/public void init(FilterConfig fConfig) throws ServletException {Filter.super.init(fConfig);encode=fConfig.getInitParameter("encoding");}}

LoginFilter

package com.scm.filter;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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;/*** Servlet Filter implementation class LoginFilter*/
@WebFilter("/LoginFilter")
public class LoginFilter implements Filter {/*** Default constructor. */public LoginFilter() {// TODO Auto-generated constructor stub}/*** @see Filter#destroy()*/public void destroy() {// TODO Auto-generated method stub}/*** @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)*/public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {HttpServletRequest req=(HttpServletRequest)request;Object obj = req.getSession().getAttribute("user");if(obj==null) {//未登录response.setContentType("text/html;charset=utf-8");response.getWriter().println("<script language=\"javascript\">alert(\"您还没有登录,请先登录!\");"+ "if(window.opener==null){window.top.location.href=\"../login.jsp\";}"+ "else{window.opener.top.location.href=\"../login.jsp\";window.close();}</script>"); response.getWriter().flush();response.getWriter().close();}else {//登录chain.doFilter(request, response);}}/*** @see Filter#init(FilterConfig)*/public void init(FilterConfig fConfig) throws ServletException {// TODO Auto-generated method stub}
}

AuthFilter

package com.scm.filter;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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;import com.scm.model.User;
import com.scm.service.AuthService;/*** Servlet Filter implementation class AuthFilter*/
@WebFilter("/AuthFilter")
public class AuthFilter implements Filter {/*** Default constructor. */public AuthFilter() {// TODO Auto-generated constructor stub}/*** @see Filter#destroy()*/public void destroy() {// TODO Auto-generated method stub}/*** @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)*/public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {HttpServletRequest req=(HttpServletRequest) request;String path=req.getServletPath();User user=(User) req.getSession().getAttribute("user");boolean flag=new AuthService().authCheck(path, user.getModelUri());if(flag) {chain.doFilter(request, response);}else {response.getWriter().println("<script language=\"javascript\">alert(\"您还没有权限!\");</script>");response.getWriter().flush();response.getWriter().close();}}/*** @see Filter#init(FilterConfig)*/public void init(FilterConfig fConfig) throws ServletException {// TODO Auto-generated method stub}
}
package com.scm.service;import java.util.ArrayList;public class AuthService {/*** 根据servlet路径和user modeluri判断其是否具有权限* @param path* @param modelUri* @return*/public boolean authCheck(String path,ArrayList<String> modelUri) {for(String s:modelUri) {if(path.startsWith(s)) {return true;}}return false;}
}

Filter_编码过滤,登录过滤,权限过滤相关推荐

  1. es html标签,Elasticsearch如何使用同义词搜索富文本html标签过滤以及分权限过滤搜索结果...

    如何建立恰当的索引结点 { "mappings": { "data": { "properties": { "answer_id& ...

  2. 【自然框架】稳定版的Demo——看点二:权限,权限过滤与验证。

    建议先看看 上一篇:[自然框架]稳定版beta1--源码下载,Demo说明 下载地址:还是老地方,自然框架的源代码.Demo.数据库.配置信息管理程序下载(2010.01.25更新) 在线演示:htt ...

  3. 通用数据级别权限的框架设计与实现(3)-数据列表的权限过滤

    查看上篇文章通用数据级别权限的框架设计与实现(2)-数据权限的准备工作,我们开始数据列表的权限过滤. 原理:我们在做过滤列表时,根据用户权限自动注入到相关SQL中,实现相关过滤,如果拥有全部权限,则不 ...

  4. MVC用filter做权限过滤

    MVC用filter做权限过滤: 1:创建filter类:类需要继承一个接口,并且重写4个方法:如下: namespace MvcApplication1.Code {     public clas ...

  5. Django rest framework之限流Throttling、内置过滤功能及第三方过滤功能及分页Pagination

    文章目录 1.限流Throttling 1.1.自定义频率类 1.1.1.编写频率类 1.1.2.全局使用 1.1.3.局部使用 1.2.内置频率类 1.2.1.根据用户ip限制 1.2.2.限制匿名 ...

  6. mysql 危险字符_PHP过滤指定字符串,过滤危险字符

    安全过滤函数,用于过滤危险字符 function safe_replace($string) { $string = str_replace(' ','',$string); $string = st ...

  7. Windows驱动_文件系统微小过滤驱动之三微小过滤驱动的操作

    30岁左右的程序员,现在除了奋斗以外,要开始考虑下自己的身体了,到了这个年纪,不能像之前20岁左的年轻人一样不顾一切去拼搏.现在的自己,应该更讲究效率.所以选择公司也很重要.同样,运动开始变得必需了, ...

  8. 垃圾邮件过滤 php,垃圾邮件过滤功能

    · 系统内置无需第三方系统配合 · 启用发信(SMTP)认证,关闭匿名转发 · 启用发本域邮件也需发信(SMTP)认证 · IP 黑名单过滤 · IP 白名单 · IP 灰名单,来自于名单中IP的SM ...

  9. jq选择器||基本选择器 层级选择器 属性选择器 过滤选择器 表单过滤选择器

    基本选择器 层级选择器 属性选择器 过滤选择器 表单过滤选择器 1. 基本选择器        1. 标签选择器(元素选择器)                 * 语法: $("html标签 ...

最新文章

  1. Oracle数据库安装响应文件,1.2.6 步骤6:配置应答文件,然后运行安装程序(2)...
  2. memcached与spring集成
  3. python分布式进程(windows下)
  4. mysql hash分区 数目_mysql8 参考手册-HASH分区
  5. SAP License:SAP顾问是如何炼成的——SAP到底是什么?
  6. mysql 手机归属地_盒子 - 手机归属地 MySql 数据
  7. 点击area不出现黑框_黑框淋浴房,黑得高级,黑得漂亮!
  8. 如何判断自己的Windows系统是否为盗版系统?
  9. 程序员必备14款生产力工具~
  10. quartus仿真11:74138译码
  11. 国外免费数据集下载网址
  12. cntv.cn今日“开锅” USB3.0初露端倪(每日关注2009.12.28)
  13. 汉语拼音文件搜索项目
  14. 计算机中alu子系统功能,计算机组成原理第3章1CPU子系统概述与ALU.ppt
  15. pvid与access的关系_关于Trunk、Hybrid、Access、Tag、Untag、Pvid的关系
  16. 数字信号处理:视频-15-FFT计算线性卷积
  17. 科技型中小企业的申报流程及材料?
  18. 霍夫圆检测原理+实战
  19. 存储过程 编译错误:PLS-00103: Encountered the symbol TABLE when expecting one of the following:
  20. 踩到了不可见字符\u200B的坑(0长度的字符)

热门文章

  1. 商家酒店模块之java商城 开源java电子商务Spring Cloud+Spring Boot+mybatis+MQ+VR全景+b2b2c
  2. php 校园oa办公系统xammp
  3. linux配置supervisor
  4. H.264嵌入式视频监控系统项目指导
  5. html5 隐藏摄像头,js控制摄像头拍照 请问html5怎么关闭摄像头?
  6. 6.1.4 6.1.5完美越狱
  7. 晨曦记账本的功能有哪些
  8. 黑马—private关键字-封装
  9. python中年大叔学编程_中年大叔学编程-Python环境安装
  10. 用python写一个北京市的个税计算器