Atitit.struts排除url 的設計and 原理 自定義filter 排除特定url
1.1. 原理流程1
2. Invoke1
3. StrutsX2
讀取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:[email protected]
轉載請注明來源: http://www.cnblogs.com/attilax/
/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);
}
/**
* @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