您的位置:首页 > 编程语言 > Java开发

Struts2 过滤器无法正常过滤.action请求

2016-05-20 17:11 519 查看

1. 问题描述

过滤器启用正常,而且使用也正常,调试时它也确实进入到断点中,并可以正常过滤.jsp请求,但过滤器唯独获取不到.action的请求。通过打印request.getRequestURI请求路劲,可以输出拦截到的各种请求。但发现过滤器有时会进入多次,重复多次,但仍却未打印输出.action类型请求。(注:后得知,会多次进入过滤器类,是因为我在url-pattern设置的是/*,也就是会过滤所有的请求,包括加载的img,css,js等)

当时测试时输出的信息:



拦截下的什么都出来了,唯独未出现.action

2.解决方案

一开始一直纳闷着默默的调试着,一直不解,而后才百度找原因。

在web.xml配置文件中将 自定义的过滤器放在默认Struts2过滤器之前即可,就可以完成对.action的过滤拦截,一个容易忽略的小问题引发一下午的郁闷,实在不应该。



完成了拦截的操作,



3.贴上自己过滤器的代码供参考

虽然我也是用的前辈留下来的过滤器模板,但是会用就行啦,现在拿出来分享分享。
(1).web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>wrtPlatformVt</display-name>

<!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>

<error-page>
<error-code>404</error-code>
<location>/wrt/other/notfound.jsp</location>
</error-page>

<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/wrt/other/error.jsp</location>
</error-page>

<!-- 自定义的过滤器 -->
<filter>
<filter-name>authorityFilter</filter-name>
<filter-class>cn.thinknet.filter.AuthorityFilter</filter-class>
<init-param>
<param-name>allowAuthorityURL</param-name><!-- 不需要过滤的地址 -->
<param-value>login.jsp,register.jsp,getbackPwdOne.jsp,login.action,sendMail.action,register.action,getbackPwdTwo.jsp,getbackPwdThree.jsp,getbackTwo.action,getbackThree.action,index.jsp</param-value>
</init-param>
<init-param>
<param-name>authorityURL</param-name><!-- 只对指定过滤参数后缀进行过滤 -->
<param-value>.action,.jsp,.do</param-value>
</init-param>
<init-param>
<param-name>redirectPath</param-name><!-- 未通过跳转到登录界面 -->
<param-value>/wrtPlatformVt/wrt/login.jsp</param-value>
</init-param>
<init-param>
<param-name>disableFilter</param-name><!-- Y:过滤无效 -->
<param-value>N</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>authorityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置struts2过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

<!-- 欢迎页 -->
<welcome-file-list>
<welcome-file>/wrt/login.jsp</welcome-file>
</welcome-file-list>
</web-app>


(2).过滤器AuthorityFilter.java

package cn.thinknet.filter;

import java.io.IOException;
import java.io.PrintWriter;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import cn.thinknet.utils.others.AKKeysUtil;

/**
* 过滤器
*
*
*
*/
public class AuthorityFilter extends HttpServlet implements Filter
{
/**
*
*/
private static final long serialVersionUID = 4504557649329493897L;

public String[] allowAuthorityURLs;

public String[] authorityURLs;

public FilterConfig config;

/**
* 过滤不能访问的地址
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException
{
// 未登录需要跳转的地址
String redirectPath = config
.getInitParameter(AKKeysUtil.WEB_CONTEXT_REDIRECT_PATH);

// 过滤是否启用
boolean isEnable = true; // 过滤器可用
String disableStr = config
.getInitParameter(AKKeysUtil.WEB_CONTEXT_DISABLE_FILTER);
if (StringUtils.isNotEmpty(disableStr))
{
isEnable = disableStr.equals("N");
}
HttpServletRequest req = (HttpServletRequest) request;

// 判断过滤器是否启用
if (!isEnable)
{
filterChain.doFilter(request, response);
return;
}

// 需要过滤的后缀
String authorityURL = config
.getInitParameter(AKKeysUtil.WEB_CONTEXT_AUTHORITY_URL);
if (StringUtils.isNotEmpty(authorityURL))
{
authorityURLs = authorityURL.split(",");
}

// 判断当前的请求地址中是否存在需要过滤的后缀
if (authorityURL(req))
{
// 不需要过滤的地址
String allowAuthorityURL = config
.getInitParameter(AKKeysUtil.WEB_CONTEXT_ALLOW_AUTHORITY_URL);
if (StringUtils.isNotEmpty(allowAuthorityURL))
{
allowAuthorityURLs = allowAuthorityURL.split(",");
}

// 过滤不拦截的url
if (allowAuthorityURL(req))
{
filterChain.doFilter(request, response);
return;
} else
{
// 判断当前用户是否登录,没有登录直接跳转到登录页面
if (!relogin(redirectPath, response, req))
{
return;
}
}

// 最后对action与jsp进行权限校验
// if (authorityRequestAddress(req))
// {
// 【暂时不实现纵向越权控制】
filterChain.doFilter(request, response);
// }
// else
// {
// 没有权限时
// noAuthority();
// }
} else
{
// 例如js,image,css等文件不列入权限控制范围内
filterChain.doFilter(request, response);
}
}

@Override
public void init(FilterConfig filterConfig) throws ServletException
{
config = filterConfig;
// WebApplicationContext ctx = WebApplicationContextUtils
// .getWebApplicationContext(this.getServletContext());

// menuService = (MenuService) ctx.getBean("menuService");
}

/**
* 在未登陆的情况下允许访问的URL
*
* @return Boolean
*/
private boolean allowAuthorityURL(HttpServletRequest request)
{
boolean isAllow = false;
// 获得当前访问的地址
String current_url = request.getRequestURI();

if (ArrayUtils.isNotEmpty(allowAuthorityURLs))
{
for (String allowUrl : allowAuthorityURLs)
{
if (StringUtils.containsIgnoreCase(current_url, allowUrl))
{
isAllow = true;
break;
}
}
}
return isAllow;
}

/**
* 需要过滤的后缀
*
* @return Boolean
*/
private boolean authorityURL(HttpServletRequest request)
{
boolean isFilter = false;
if (ArrayUtils.isNotEmpty(authorityURLs))
{
for (String suffix : authorityURLs)
{
if (request.getRequestURI().indexOf(suffix) != -1)
{
isFilter = true;
break;
}
}
}
return isFilter;
}

/**
* 判断员工回话是否失效
*
* @param redirectPath
*            需要跳转的页面
* @param response
*            请求响应
*
* @param request
*            请求
*
* @throws IOException
*
* @return boolean 假:代表重新登录,真:代表session存在
*/
private boolean relogin(String redirectPath, ServletResponse response,
HttpServletRequest request) throws IOException
{
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
// 判断该用户是否存在session中,如果有直接进入当前action
if (null == request.getSession(true).getAttribute(
AKKeysUtil.USER_EMPLOY_SESSION_KEY))
{
// 跳转到登录界面
out.print("<script language='javascript'>alert('身份验证失效,请重新登录!');window.parent.location.href='"
+ redirectPath + "';</script>");
return false;
}

// 如果用户禁用掉cookie,则跳转到登录界面,提示用户启用cookie
Cookie[] cookies = request.getCookies();
if (null == cookies)
{
// 1.可能用户清除过cookie 2.可能是由于用户禁用了cookie 此时都会跳转到登录界面
// 跳转到登录界面
out.print("<script language='javascript'>alert('Cookie被清理或是已禁用,请尝试重新登录!');window.parent.location.href='"
+ redirectPath + "';</script>");
return false;
}
return true;
}
}


此代码复制粘贴即可使用,当然别忘记了要加入的jar包,负责是不行的喔 ,至于对以下这种不理解的,可以查找一个web.xml各标签用法即可
<init-param>
<param-name>redirectPath</param-name><!-- 未通过跳转到登录界面 -->
<param-value>/wrtPlatformVt/wrt/login.jsp</param-value>
</init-param>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: