Atitit.struts排除url 的设计and 原理 自定义filter 排除特定url
1.1. 原理流程
读取struts配置xml文件内容
得到多个regexpress规则,匹配规则与uri
<constant name="struts.action.excludePattern" value="/com.attilax/core/approot_js.jsp,.*.jsp,/api.jsp,/com.attilax/.*,/static/.*,/dwr/.*,/dwr/test/.*,/spr/.*,/eqMntRun.jsp,/vod/eqMntRun.jsp,/comm/.*"></constant>
作者:: 绰号:老哇的爪子 ( 全名::Attilax Akbar Al Rapanui 阿提拉克斯 阿克巴 阿尔 拉帕努伊 ) 汉字名:艾龙, EMAIL:1466519819@qq.com
转载请注明来源: http://www.cnblogs.com/attilax/
2. Invoke
/wxb_site/src/com/csmy/my/center/filter/RequestFilter.java
if(CTUtils.isEmpty(userInfo) && CTUtils.isEmpty(customerInfo) && isnotExtUrl(request)){//如果全部为空
if ((uri.indexOf("login") == -1) && !uri.equals(path + "/")) {
System.out.println("客户端未登录,请登录...");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
if (uri.indexOf(".jsp") >= 0) {
System.out.println("截取jsp请求");
response.sendRedirect(path + "/login.htm");
return;
}
}
}
private boolean isnotExtUrl(HttpServletRequest request) {
String f = pathx.classPath() + "/struts.xml";
String pats =StrutsX. get_excludePattern(f);
return !new StrutsX().isUrlExcluded(request, pats);
}
3. StrutsX
/**
* @author attilax 老哇的爪子
@since o08 j_52_3$
*/
package com.attilax.util;
import com.attilax.core;
import com.attilax.exception.ExUtil;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import static com.attilax.core.*;
import java.util.*;
import java.util.regex.Pattern;
import java.net.*;
import java.io.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.Dispatcher;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
/**
* @author attilax 老哇的爪子
* @since o08 j_52_3$
*/
public class StrutsX {
public static void main(String[] args) {
String f = pathx.classPath() + "/struts.xml";
String pats = get_excludePattern(f);
System.out.println(pats);
}
public static String get_excludePattern(String f) {
String t = filex.read(f);
org.dom4j.Document document;
try {
document = DocumentHelper.parseText(t);
Node root = document.getRootElement();
// root.selectNodes(arg0)
List<Element> nodes = ((Element) root).elements("constant");
for (Element e : nodes) {
if (e.attributeValue("name").equals(
"struts.action.excludePattern")) {
return e.attributeValue("value");
}
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
ExUtil.throwEx(e1);
}
return "";
}
public boolean isUrlExcluded(HttpServletRequest request, String patts) {
List<Pattern> excludedPatterns=buildExcludedPatternsList(patts);
if (excludedPatterns != null) {
String uri = (request.getRequestURI());
for (Pattern pattern : excludedPatterns) {
if (pattern.matcher(uri).matches()) {
return true;
}
}
}
return false;
}
public boolean isUrlExcluded(HttpServletRequest request,
List<Pattern> excludedPatterns) {
if (excludedPatterns != null) {
String uri = (request.getRequestURI());
for (Pattern pattern : excludedPatterns) {
if (pattern.matcher(uri).matches()) {
return true;
}
}
}
return false;
}
//public List<Pattern> buildExcludedPatternsList(Dispatcher dispatcher) {
//return buildExcludedPatternsList(dispatcher.getContainer().getInstance(
//String.class, StrutsConstants.STRUTS_ACTION_EXCLUDE_PATTERN));
//}
private List<Pattern> buildExcludedPatternsList(String patterns) {
if (null != patterns && patterns.trim().length() != 0) {
List<Pattern> list = new ArrayList<Pattern>();
String[] tokens = patterns.split(",");
for (String token : tokens) {
list.add(Pattern.compile(token.trim()));
}
return Collections.unmodifiableList(list);
} else {
return null;
}
}
/**
* @author attilax 老哇的爪子
* @since o08 j_52_a
*
* @return
*/
public static HttpServletRequest getReq() {
// attilax 老哇的爪子 j_52_a o08
return ServletActionContext.getRequest();
}
// attilax 老哇的爪子 j_52_3 o08
}
// attilax 老哇的爪子
paip.读取WEB.XML中的参数值总结 - attilax的专栏 - 博客频道 - CSDN.NET.htm
看struts2源码感之一:struts.action.excludePattern属性_「电脑玩物」中文网我们只是「电脑玩物」 -.htm