简体   繁体   English

如果拦截器中的身份验证失败,Struts2将返回json

[英]Struts2 return json if authentication failed in interceptor

Please Note, I am not a Java developer so my question may have some heart breaking assumptions & invalid piece of code. 请注意,我不是Java开发人员,所以我的问题可能有一些令人心碎的假设和无效的代码段。

I have an interceptor which takes care of login authentication. 我有一个拦截器,负责登录身份验证。

<global-results>
   <result name="login">/admin/login.jsp</result>
</global-results>

As shown, it redirects to login.jsp when authentication fails ie the return value is "login" (in this case) 如图所示,当身份验证失败时,它将重定向到login.jsp,即返回值为“ login”(在这种情况下)

What I want to achieve is, I return some other value (say "loginAjax") and it should not send an HTTP 302, instead it should return back a JSON response. 我想要实现的是,我返回一些其他值(例如“ loginAjax”),并且它不应发送HTTP 302,而应返回JSON响应。

I don't know if that possible at all or not. 我不知道那是否可能。 I have this other piece in struts.xml which returns json when a particular value is returned from Action. 我在struts.xml中有另一部分,当从Action返回特定值时,该部分返回json。

<result name="success" type="json">
    <param name="includeProperties">
        return_id, return_message
    </param>
</result>

I don't know how should I tie both the pieces together and get it working for the same interceptor. 我不知道如何将这两部分捆绑在一起,并使其在同一个拦截器中工作。

A global result is not tied to one or more Interceptors or Actions: it is global to everyone, so it will be rendered when returned, no matter who is returning it. 全局结果与一个或多个“拦截器”或“动作”无关:它对每个人都是全局的,因此无论返回谁,它都会在返回时呈现。

You need to add a global (keeping it consistent with your other login result, that is global) result of type json, like this: 您需要添加json类型的全局(使其与其他登录结果一致,即全局),如下所示:

<global-results>
    <result name="login">/admin/login.jsp</result>
    <result name="loginAjax" type="json" />
</global-results>

Then, you can send a particular statusCode, or errorCode, like described here . 然后,您可以发送特定的statusCode或errorCode, 如此处所述

If you need to understand what kind of result you should return from within the Interceptor, you should proceed like described in this other answer , by checking the type of the result that it would be returned in case of success. 如果你需要了解你应该从拦截返回什么样的结果,你应该像描述继续在这个其他答案 ,通过检查,它会在成功的情况下返回的结果的类型。

PS: Oh, and consider hiring a Java dev to do Java stuff properly :) PS:哦,考虑雇用Java开发人员来正确地完成Java的工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM