Filter详解

摘要:
=-1){returntrue;}if(uri.contains(“login.jsp”)|uri.contations(“login.do”)){returntrue;}如果(!

参考网页:http://blog.csdn.net/zhaozheng7758/article/details/6105749

设置拦截器,需要做两步操作:

1)在web.xml中配置Filter

<filter>
  <display-name>SsoFilter</display-name>
  <filter-name>SsoFilter</filter-name>   //Filter的名字
  <filter-class>com.cares.asis.common.SsoFilter</filter-class>  //Filter的实现类
  <init-param>
    <param-name>userNotExistUrl</param-name>   //Filter中配置初始化参数userNotExistUrl
    <param-value>/common/user_notexist.jsp</param-value>   //参数值
  </init-param>
  <init-param>
    <param-name>errorUrl</param-name>          //Filter中配置初始化参数errorUrl
    <param-value>/common/error.jsp</param-value>    //参数值
  </init-param>
</filter>

<filter-mapping>
  <filter-name>SsoFilter</filter-name>    //Filter的名字
  <url-pattern>/*</url-pattern>         //Filter负责拦截的URL,这里*是拦截所有的地址
</filter-mapping>

2)创建Filter处理类

public class SsoFilter implements Filter {
private String userNotExistUrl;
private String errorUrl;
private UserService userService;
private MenuService menuService;
private SystemCacheService systemCacheService;
private RoleService roleService;
private static final String CONFIG_PROPERTIES_PATH = "/baseconfig.properties";
private static final String IS_SSO = "is.sso";
private static PropertiesUtil prop = new PropertiesUtil(CONFIG_PROPERTIES_PATH);

//destroy()用于Filter 销毁前,完成某些资源的回收

@Override
public void destroy() {
// TODO Auto-generated method stub

}

//doFilter()实现过滤功能,对每个请求及响应增加的额外处理

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String uri = req.getRequestURI();
if (exclude(uri)) {
  chain.doFilter(request, response);      // Filter 只是链式处理,请求依然转发到目的地址
} else {

// 跳转到“拒绝进入系统”提示页
request.getRequestDispatcher(errorUrl).forward(req, res);  //Filter可以改变请求和响应的内容
}
}
}

//init()用于完成Filter 的初始化

@Override
public void init(FilterConfig arg0) throws ServletException {

  //在web.xml中设置的初始化参数,通过FilterConfig中的getInitParameter(“param-name”)方法可以得到

  userNotExistUrl = arg0.getInitParameter("userNotExistUrl");  

  errorUrl = arg0.getInitParameter("errorUrl");
 // userService = (UserService) SystemBeanFactory.getBean("userService");
 // menuService = (MenuService) SystemBeanFactory.getBean("menuService");
 // systemCacheService = (SystemCacheService) SystemBeanFactory.getBean("systemCacheService");
 // roleService = (RoleService) SystemBeanFactory.getBean("roleService");
}

private boolean exclude(String uri) {
if (uri.endsWith("js")) {
return true;
}
if (uri.endsWith("doc")) {
return true;
}
if (uri.endsWith("docx")) {
return true;
}
if (uri.endsWith("zip")) {
return true;
}
if (uri.endsWith("jsp") || uri.endsWith("html")) {
return true;
}
if (uri.endsWith("css")) {
return true;
}
if (uri.endsWith("png") || uri.endsWith("jpg")) {
return true;
}
if (uri.indexOf("cs") != -1) {
return true;
}
if (uri.contains("login.jsp") || uri.contains("login.do")) {
return true;
}
if (!Boolean.parseBoolean(prop.getProperty(IS_SSO))) {
return true;
}
return false;
}
}

免责声明:文章转载自《Filter详解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇阿里云ecs自定义监控项解决POWER BI权限控制问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

CAD转DXF怎么转换?教你三种转换方法

2.进入到CAD版本转换的界面中后,在选择“点击选择文件”,在跳转出的“打开”界面中打开需要转换的CAD图纸。...

IPI 通信(SMP)【转】

在MIPS架构下的IPI通信被关闭和中断后,IPIMIPS接口结构平台也将被发送_ smp_Ops{void;void;…}IPI通信是多个处理器之间的通信。send_ ipi_Single:一对一聊天send_ ipi_Mask:Mask posting,Mask表示Mask posting/*Octeon Tellanothercore of Lushi...

Jdk升级到11引起的问题:程序包javax.xml.bind.annotation不存在

您可以看到ELDict类中有一个引用:importjavax。xml。绑定注释XmlAttribute;虽然未使用,但它会导致mvn编译错误。在在线绑定中搜索“包javax.xml.bind.nannotation不存在”。结果是:包javax。xml。bind Annotation不存在-CSDN论坛2009年12月2日·无法编译使用jaxb的类,因为软件...

海康SDK编程指南(C#二次开发版本)

海康SDK编程指南目前使用的海康SDK包括IPC_SDK,Plat_SDK(平台),其中两套SDK都需单独调用海康播放库PlayCtrl.dll来解码视频流,返回视频信息和角度信息。本文仅对视频监控常用功能的使用进行说明,其它未实现功能请参看设备网络SDK使用手册和播放库编程指南V7.2。IPC_SDK编程指南(一)SDK的引用由于IPC_SDK没有SDK安...

硬中断与软中断的区别!

在多核系统上,一个中断通常只能中断一个CPU(也有一种特殊情况,即主机上有一个硬件通道。它可以在没有主CPU支持的情况下同时处理多个中断。软中断:1。软中断与硬中断非常相似。生成软中断的进程必须是当前正在运行的进程,因此它们不会中断CPU。...

Python-正则

,三:量词*重复0次或多次{0,}+重复一次或多次{1,}?重复0或1次{1,0}{n}重复n次{n}{n,}重复n次,或更多次{n,m}将n次重复到m次Escape:如果字符串中有特殊字符要匹配,请在常规字符和字符串前面添加r。如果特殊字符在字符组中,则它们是匹配的特殊字符,但为了记忆,匹配时会转义所有特殊字符。...