@ConversationScope bean不保留引用值

Iva*_*tyk 1 ejb cdi twitter4j jsf-2 conversation-scope

我正在尝试实现Twitter登录过程.在登录过程中,用户需要被重定向到Twitter网站以输入他/她的凭证,然后他/她将被重定向到我的网站URL.在第一次重定向之前,应该在请求之间存储和保留RequestToken对象(Twitter4J库)的实例.

为此,我决定使用ConverstaionScoped bean,但遗憾的是,请求之间不保留引用的值.

这是我的JSF页面:

twitter.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >

    <f:event listener="#{twitterLoginPageController.login()}" type="preRenderView" />

</html>
Run Code Online (Sandbox Code Playgroud)

twitterRedirect.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >

    <f:event listener="#{twitterLoginPageController.onRedirect(param.oauth_token, param.oauth_verifier)}" type="preRenderView" />

</html>
Run Code Online (Sandbox Code Playgroud)

我的Twitter登录控制器:

@Named
@ConversationScoped
public class TwitterLoginPageController implements Serializable {


    @Inject
    private Conversation conversation;

    // These objects should be retained between requests
    Twitter twitter;
    RequestToken requestToken;

    public void login() {

        try {
            twitter = new TwitterFactory().getInstance();
            requestToken = twitter.getOAuthRequestToken("...");


            conversation.begin();
            conversation.setTimeout(30000);
            // Navigate to Twitter here
        } catch (TwitterException ex) {
             ...
        }

    }

    public void onRedirect(String oauthToken, String oauthVerifier) {      
        try {
            // twitter reference is null here
            twitter.getOAuthAccessToken(requestToken, oauthVerifier);
            ...
        } catch (TwitterException e) {
            ...
        } finally {
            conversation.end();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

在我看来,我正在密切关注使用@ConverstaionScope的例子,但是我没有得到预期的结果.如何在请求之间保留对象?

rdc*_*rng 5

我不熟悉Twitter auth或Twitter4J,但由于它是Twitter将用户重定向回你的应用程序,你必须通过twitter传递会话ID,以便twitter将用户重定向回包含该id的URL.

Java EE容器通过传递cid=...参数来维护会话状态,并且这种情况会自动发生在JSF导航中,但您必须另外处理它.因此,请确保在开始对话后获取对话ID并将其传递给Twitter,以便Twitter将用户重定向到twitterRedirect.xhtml?cid=....