2013年12月15日星期日

struts2 interceptor Login parity does not properly handle json request

 This post last edited by zhangchu_63 on 2012-01-04 17:18:36
Recent projects to be used in the Flexigrid, part of the operation using AJAX submit .
system is configured with a login and session fail validation struts2 global interceptors , when ajax request is sent to the background, the interceptor can intercept , but the judge is not logged or session time-out when they can not jump to a specific page. Such permission
words simply can not do anything .
expressed do not know if I have no problem.

<package name="strutsCheckLogin" extends="json-default">
<interceptors>
<interceptor name="noLogin"
class="com.zc.zcproject.commonInterceptor.AuthorityInterceptor" />
<interceptor-stack name="appStack">
<interceptor-ref name="defaultStack" />
</interceptor-stack>
<interceptor-stack
name="defaultPaginationInterceptorStack">
<interceptor-ref name="noLogin" />
<interceptor-ref name="appStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref
name="defaultPaginationInterceptorStack" />
<global-results>
<result name="json" >
${pageContext.request.contextPath}/page/main_login.jsp
</result>
<result name="login" >
${pageContext.request.contextPath}/page/main_login.jsp
</result>
<result name="noframe">
${pageContext.request.contextPath}/page/error/error.jsp
</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="noframe"
exception="com.zc.zcproject.exception.AppException">
</exception-mapping>
</global-exception-mappings>
</package>





public String intercept(ActionInvocation ai) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ActionContext ctx = ai.getInvocationContext();
Map session = ctx.getSession();
Object ACCOUNT = (Object) session.get(Constants.USER);
if (ACCOUNT != null) {
return ai.invoke();
}

_logger.info("intercept user is not login");
if(isAjaxRequest(request)){
return "json";
}else{
System.out.println("login");
return "login";
}
}

private boolean isAjaxRequest(HttpServletRequest request) {
String header = request.getHeader("X-Requested-With");
if (header != null && "XMLHttpRequest".equals(header))
return true;
else
return false;
}



you help out .
------ Solution ---------------------------------------- ----
have encountered such a problem before me . But LZ to understand that submission AJAX is not directly jump through the backstage ACTION . Case 2 , 1: JS submit AJAX request, if ACTION returns a page that JS is not correctly parse the data returned ; 2: direct access to a background URL address ( eg directly in the browser address bar , or point a super- connections, etc. ) , if the background returns JSON or XML format string , the browser will prompt you to download and save . These two cases are not want to see.
solution is case 1: ACTION returns JSON or XML format string , JS return data after receiving pop-up message or controlled by the JS jump to a page. case 2: ACTION returned directly to a page or landing page without permission .

------ Solution ------------------------------------ --------
modify the code :
public String intercept (ActionInvocation ai) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest ();
HttpServletResponse response = ServletActionContext.getResponse ();
ActionContext ctx = ai.getInvocationContext ();
Map session = ctx.getSession ();
Object ACCOUNT = (Object) session.get (Constants.USER);
if (ACCOUNT! = null) {
return ai.invoke ();
}

_logger.info ("intercept user is not login");
if (isAjaxRequest (request)) {
ValueStack stack = invocation.getStack ();
Map returnResult = new HashMap ();
/ / add an object to the root of MAP, KEY value ( returnResult )
same value / / in the root
stack.set ("returnResult", returnResult);
returnResult.put ("rtn", "01");
returnResult.put ("msg", " no authority" ) ;
return "input";
} else {
System.out.println ("login");
return "login";
}
}

private boolean isAjaxRequest (HttpServletRequest request) {
String header = request.getHeader ("X-Requested-With");
if (header! = null && "XMLHttpRequest". equals (header))
return true;
else
return false;
}

struts.xml added profiles

login.jsp

returnResult


scheduled to be added this configuration a succession json-default in the package .
------ For reference only ----------------- ----------------------
not jump $ {pageContext.request.contextPath} / page / main_login.jsp
What is the meaning of this page inside
result you return to main_login.jsp resolve or jump directly to main_login.jsp page
------ For reference only ---------------- -----------------------
I'm going to jump directly to main_login.jsp page
------ For reference only - -------------------------------------
top up ah , someone know
------ For reference only ---------------------------------------
try to remove the $ {pageContext.request.contextPath}
------ For reference only ------------------------ ---------------
I have encountered , solving "
------ For reference only ------------- --------------------------
I'm doing a similar function : remote account login prompt , and provides links to re-login , just started yet I do not know specifically how to use json , a strong focus of the issue , the House resolved if hope to share
------ For reference only ------------------- --------------------
own re-package the ajax request to send a unified message processing returns
------ For reference only - -------------------------------------
I also new to ExtJs, if the relevant authority problems may want to communicate about email zhangchu63@sohu.com
------ For reference only ------------------------ ---------------
how to solve the Arab-Israeli . . . . . . . .
------ For reference only -------------------------------------- -
I also encountered a similar way .
------ For reference only -------------------------------------- -
1.response.sendError ( custom error ID)
2. set global Ajax error handling , error ID for the custom error ID, ​​jump landing page
------ For reference only ------------ ---------------------------
6 floor of the mighty , I realized , according to the sixth floor .

没有评论:

发表评论