使用过滤器可防止用户直接在浏览器中输入URL访问。
过滤器的内容如下:
public void preProcess(WebContext context) {
this.initParam();
String URI = context.getRequest().getRequestURI();
//获取父URL--用户直接输入URL,这个父URL是不存在的
String parentURL = context.getRequest().getHeader("REFERER");
boolean illegalURLFlag = false; //非法URL标志
Object userSession = context.getRequest().getSession().getAttribute(this.getSessionKey());
if(null == userSession){//未登录,不使用该filter
return;
}
if(null == parentURL || "".equals(parentURL)){//无父URL,非法访问
illegalURLFlag = true;
}
// 非法访问URL
if (illegalURLFlag){
try {
//强制用户重新登录
context.getRequest().getSession().removeAttribute(this.getSessionKey());
context.getResponse().sendRedirect(context.getRequest().getContextPath());
log.info("common.filter.illegalURLfilter.preprocess.noright", URI);
} catch (Exception e) {
log.error(e, "common.filter.illegalURLfilter.preprocess.redirectErrorPage", new Object[]{URI, this.getMainPage()});
}
}
}
过滤器的配置文件如下:
<!-- 防止用户直接访问URL --> <tiny-filter id="illegalURLFilter" class="illegalURLFilter"> <filter-mapping url-pattern=".*" ></filter-mapping> <!-- 登录后,非法URL访问跳转到此页面--> <init-param name="mainPage" value="/index.jsp"></init-param> <!-- 用户会话信息KEY --> <init-param name="sessionKey" value="commDtoSession"></init-param> </tiny-filter>
直接访问URL效果图如下: