Struts2: INfinite recursion detected
来源:互联网 发布:广东省电子网络发票 编辑:程序博客网 时间:2024/05/24 05:11
今天遇到一问题纠缠很久
环境:
1.写了一个关于身份认证以及授权的拦截器
2.如果没有通过认证和授权那么将根据客户端的类型返回信息,我开发的系统有两种客户端[浏览器和手机客户端,手机客户端的数据服务器采用Json格式数据进行响应]。
3.拦截器认证授权失败返回一个全局结果nologin,代码如下:
身份认证和授权配置:
<interceptors>
<interceptor name="authenticationInterceptor" class="com.defshare.crm.web.interceptor.AuthenticationInterceptor" />
<!-- 自定义拦截器需要和缺省拦截器栈整合,否则可能丢失struts2的一些既定功能,比如参数赋值-->
<interceptor-stack name="authenInterceptStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="authenticationInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!--Action的缺省拦截器[身份认证和授权] -->
<default-interceptor-ref name="authenticationInterceptor" />
拦截器:
@Override
public String intercept(ActionInvocation invocation) throws Exception {
//获得当前请求的uri
String uri = ServletActionContext.getRequest().getRequestURI();
uri = uri.substring(ServletActionContext.getRequest().getContextPath().length());
if(log.isInfoEnabled())
log.info("\t当前请求uri:"+uri);
//判断当前uri是否在放行列表存在,存在则放行[最好将此放入配置]
if (permissonURIS.contains(uri.toUpperCase()))
return invocation.invoke();
//初始化需要认证和授权的Uri集合
if (checkURIS.size()==0){
Object action = invocation.getAction();
if (action instanceof BaseAction){
BaseAction baction = (BaseAction)action;
List<String> smlist = baction.getModules();
checkURIS.addAll(smlist);
}
}
//判断uri是否不在检查列表,如果不在检查列表也放行
if (!checkURIS.contains(uri.toUpperCase()))
return invocation.invoke();
//程序能执行到这里说明当前uri属于检查列表,那么必须通过任何和授权才该uri才可以被访问
//不允许放行的uri必须登录才可以访问/【认证】
if (invocation.getInvocationContext().getSession().get(IAction.LOGINED_USER)==null){//如果没有登录直接返回登录
((Map<String,Object>)invocation.getInvocationContext().get("request")).put(IAction.LOGIN_MSG,"您还没有登录");
return IAction.NO_LOGIN;
}
Emp emp = (Emp)invocation.getInvocationContext().getSession().get(IAction.LOGINED_USER);
//已经登录确保用户对象放入线程局部变量/后面业务层和持久层可以使用
UserUtil.setLoginedUser(emp);
//登录判断权限是否满足
//没有角色或者没有权限都必须重新登录/【授权】
if(emp.getRole()==null || emp.getRole().getSysModules()==null && emp.getRole().getSysModules().size()==0){
((Map<String,Object>)invocation.getInvocationContext().get("request")).put(IAction.LOGIN_MSG,"没有分配任何角色或者权限,您不能登录");
return IAction.NO_LOGIN;
}
//特殊处理:只要是总经理角色一律放行,而不管系统权限如何分配【总经理特权/权限设置将对总经理无效】
if (emp.getRole().getName().equals("总经理")){
if(log.isInfoEnabled())
log.info("\t当前角色是总经理,忽略权限配置,直接放行");
return invocation.invoke();
}
//其它非总经理角色必须验证权限是否满足才可以放行
//有权限判断是否满足
List<String> userpermission = (List<String>)invocation.getInvocationContext().getSession().get(IAction.USER_PERSSION);
if (userpermission.contains(uri.toUpperCase()))
return invocation.invoke();
((Map<String,Object>)invocation.getInvocationContext().get("request")).put(IAction.LOGIN_MSG,"权限不足,您不能登录");
return IAction.NO_LOGIN;
}
nologin全局结果配置如下:
<!-- 全局结果-->
<global-results>
<!--
<result name="crmError">/WEB-INF/view/actionError.jsp</result>
-->
<result name="login">/Login.jsp</result>
<result name="nologin" type="chain">noLoginAction</result>
</global-results>
<!-- 全局异常处理
<global-exception-mappings>
<exception-mapping result="crmError" exception="java.lang.Exception" />
</global-exception-mappings>
-->
<!-- NoLoginAction -->
<action name="noLoginAction" class="noLoginAction">
<result name="login">/Login.jsp</result>
<result name="jsonresp" type="json">
<param name="ignoreHierarchy">false</param>
<param name="root">up</param>
</result>
</action>
4.关于noLoginAction我的代码如下【根据uri特征分别进行处理:/jsp/开头的uri返回login结果,否则返回jsonresp】:
package com.defshare.crm.web.action;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.defshare.crm.uprotocol.UProtocol.ERspStatus;
import com.defshare.crm.web.action.IAction.VScope;
import com.opensymphony.xwork2.ActionContext;
@Controller("noLoginAction")
@Scope("prototype")
public class NoLoginAction extends BaseJSONAction {
public NoLoginAction() {
// TODO Auto-generated constructor stub
//System.out.println("执行NoLoginAction...........");
}
@Override
public String execute() throws Exception {
//获得当前请求的uri
String uri = ServletActionContext.getRequest().getRequestURI();
uri = uri.substring(ServletActionContext.getRequest().getContextPath().length());
if (uri.toLowerCase().startsWith("/jsp/")){
return super.LOGIN;
} else if (uri.toLowerCase().startsWith("/")){
super.getUp().setStatus(ERspStatus.NotLogOn);
super.getUp().setMsg("您还没有登录");
return super.JSON_RESP;
} else {
return super.LOGIN;
}
}
/**
*
*/
private static final long serialVersionUID = 1L;
}
结果配置如下:
<action name="noLoginAction" class="noLoginAction">
<result name="login">/Login.jsp</result>
<result name="jsonresp" type="json">
<param name="ignoreHierarchy">false</param>
<param name="root">up</param>
</result>
</action>
部署运行项目输入一测试地址:http://localhost:8080/CRMWeb/jsp/regEmp.action,异常如下:
Infinite recursion detected: [/jsp/regEmp!addEmp, /jsp/noLoginAction, /jsp/noLoginAction] - [unknown location]
com.opensymphony.xwork2.ActionChainResult.execute(ActionChainResult.java:203)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:362)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
com.opensymphony.xwork2.ActionChainResult.execute(ActionChainResult.java:222)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:362)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
意思是检测到无穷递归!!!
这是使用interceptor(拦截器) 跳转到另一个action 后出现的问题,当我们的请求试图到达NoLoginAction的时候再次跳到自己写的认证授权拦截器上,然后开始从Action链的开始从头进行认证授权验证,通过在NoLoginAction的构造方法中和拦截器中设置断点我们会发现当请求进入NoLoginAction执行完构造方法后还没有来得及执行NoLoginActioN的execute方法前程序再次进入拦截器中,拦截器中取得uri地址仍然是“regEmp.action”,如此往复形成递归。
解决方法:
<!-- NoLoginAction -->
<action name="noLoginAction" class="noLoginAction">
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="login">/Login.jsp</result>
<result name="jsonresp" type="json">
<param name="ignoreHierarchy">false</param>
<param name="root">up</param>
</result>
</action>
将拦截器跳转到的NoLoginAction使用默认的拦截器defaultStack,而不再使用缺省的认证授权拦截器。
再次输入相同地址,效果如下:
输入一个非/jsp/xx.action特征的url[手机请求url]:
问题解决!
- Struts2: INfinite recursion detected
- struts2:Infinite recursion detected
- INfinite recursion detected .
- json infinite recursion stackoverflowerror
- 关于SSH中异常处理的冲突引起的无限循环-Infinite recursion detected: [……]-问题处理
- Infinite recursion (StackOverflowError)解决方案@JsonBackReference
- Could not write content: Infinite recursion (StackOverflowError)
- 关于 Infinite recursion (StackOverflowError)报错
- spring mvc json循环引用JsonMappingException: Infinite recursion
- 返回JSON出现Infinite recursion无限循环错误的解决
- Could not write JSON: Infinite recursion (StackOverflowError) (through reference chain:
- 对象序列化成json数据时,Infinite recursion (StackOverflowError)
- org.codehaus.jackson.map.JsonMappingException: Infinite recursion的解决
- Could not write JSON: Infinite recursion (StackOverflowError) (through reference chain:
- Jackson序列化对象时无限递归错误Infinite recursion的解决办法
- 微服务间调用导致的Could not write content: Infinite recursion (StackOverflowError)问题
- Recursion
- Recursion
- 黑马程序员学习日记----多线程(二)
- Jack's Notes11——打地鼠(forin遍历、type获取节点对象的类型)
- Android listview动态添加删除测试
- PMBOK(第4版)--第11章 项目风险管理(概述、输入、工具与技术、输出、数据流向图)
- 如何使用IRC
- Struts2: INfinite recursion detected
- 触摸屏驱动 2440
- 摘录2
- 使用matlab绘制数学函数
- 用LaTeX制作幻灯片(slide)
- PL/SQL DBA 创建用户 表空间 授权
- Jack's Notes12——全选、全不选、反选(循环遍历给按钮动态注册事件)
- jQuery仿手风琴效果另一种实现方式,非插件.原创
- qt 4.5.3 中文支持