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. 马化腾看上了TA:读懂互联网医疗的进化与颠覆
  2. 使用IDA 进行远程调试
  3. python字符串反转方法_Python程序使用堆栈和反转方法反转字符串
  4. linux连接池等待时间,LINUX系统下解决time_wait 连接数过多问题
  5. 10.傅里叶变换——正弦之和、时间和频率_2
  6. 【linux】为什么 mmap 比系统调用快
  7. C#数据类型02--结构
  8. 反序列化,从XML中取数据
  9. 高等数学 下册 第九章 多元函数的概念 笔记
  10. PHPWAMP自启异常,服务器重启后Apache等服务不会自动重启的原因分析
  11. 英特尔第十代处理器为什么不支持win7_为什么7代CPU不支持WIN7,原因是什么
  12. ArcGis如何创建/连接企业级地理数据库
  13. 毕业设计之 --- 新闻分类系统
  14. php getdigest,http digest
  15. Kaggle数据竞赛入门-Titanic生存预测
  16. 老猿学5G专栏完结说明
  17. Jboot框架的使用
  18. 每周跑一下Python脚本,轻松生成工作日志模板
  19. Scratch所有积木
  20. 【轮播图】使用bootstrap轮播插件(Carousel)

热门文章

  1. 行者AI解析内容审核平台中的图像检测技术原理
  2. 泰尔解说Google云计算业务:关键在于垄断
  3. 实体类自动创建数据库表失败解决
  4. java过剩_中国的程序员数量是否已经饱和或者过剩?
  5. 机器学习:朴素贝叶斯算法与垃圾邮件过滤
  6. NetApp 人工智能解决方案
  7. 使用 格式工厂 对视频的大小进行压缩
  8. 新目标大学英语综合教程2_课后答案
  9. Outlook 2007 电子邮件数字签名和加密指南
  10. 无盘服务器的优缺点,网众无盘服务器建议