JSF使用Facebook登录(SocialAuth)并保持会话活动 [英] JSF Login with Facebook (SocialAuth) and keeping Session alive

查看:274
本文介绍了JSF使用Facebook登录(SocialAuth)并保持会话活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上坚持了一段时间了,我已经重新提出了我的问题了几次。我会明确表示这一点,提供下面的实现:

I've been stuck on this issue for quite a while, and I've re-formulated my question a few times. I'll be explicit with this one, providing the implementation below:



index.xhtml ,简称。

...
<f:event listener="#{userBacking.pullUserInfo}" type="preRenderView" />

<h:commandLink action="#{userBacking.login()}"  value="Login"  rendered="#{!userBacking.isLoggedIn()}"/>
<h:commandLink action="#{userBacking.logout()}" value="Logout" rendered="#{userBacking.isLoggedIn()}" />

<h:outputText rendered="#{userBacking.isLoggedIn()}" value="#{userBacking.userProfile.fullName}" />
...



UserBacking.java

/**
 * 
 * @author ggrec
 *
 */
@ManagedBean
@SessionScoped
public class UserBacking implements Serializable
{

    // ==================== 2. Instance Fields ============================

    private static final long serialVersionUID = -2262690225818595135L;

    /**
     * This is static for now. In the future, Google (and others) may be implemented.
     */
    public static final String SOCIAL_PROVIDER_ID = "facebook"; //$NON-NLS-1$


    // ==================== 2. Instance Fields ============================

    private SocialAuthManager socialAuthManager;

    private Profile userProfile;


    // ==================== 6. Action Methods =============================

    public void login() throws Exception
    {
        final Properties props = System.getProperties();
        props.put("graph.facebook.com.consumer_key", FACEBOOK_APP_ID); //$NON-NLS-1$
        props.put("graph.facebook.com.consumer_secret", FACEBOOK_APP_SECRET); //$NON-NLS-1$
        props.put("graph.facebook.com.custom_permissions", "email"); //$NON-NLS-1$ //$NON-NLS-2$
        final SocialAuthConfig config = SocialAuthConfig.getDefault();
        config.load(props);

        socialAuthManager = new SocialAuthManager();
        socialAuthManager.setSocialAuthConfig(config);

        final String authenticationURL = socialAuthManager.getAuthenticationUrl(SOCIAL_PROVIDER_ID, successURL());

        ContextHelper.redirect(authenticationURL);
    }


    public void logout() throws Exception
    {
        socialAuthManager.disconnectProvider(SOCIAL_PROVIDER_ID);

        ContextHelper.invalidateSession();
    }

    /*
     * Should fill up the profile, if a redirect from Facebook appers. Uhhh....
     */
    public void pullUserInfo() throws Exception
    {
        if (userProfile == null && socialAuthManager != null)
        {
            final HttpServletRequest req = (HttpServletRequest) ContextHelper.ectx().getRequest();
            final Map<String, String> reqParam = SocialAuthUtil.getRequestParametersMap(req);

            final AuthProvider authProvider = socialAuthManager.connect(reqParam);

            userProfile = authProvider.getUserProfile();

            ContextHelper.redirect( ContextHelper.getContextPath() );
        }
    }


    // ==================== 7. Getters & Setters ======================

    public Profile getUserProfile()
    {
        return userProfile;
    }


    public boolean isLoggedIn()
    {
        return userProfile != null;
    }


    // ==================== 9. Convenience Methods ========================

    private static String successURL()
    {
        return IApplicationConstants.APP_PSEUDO_URL + ContextHelper.getContextPath();
    }

}



我的故事


  • 如果浏览器中不存在Facebook会话,可以正常工作。如果我已经登录,似乎出现了请求参数中的代码时, socialAuthManager 为NULL。

我使用 index.xhtml 进行登录和回拨。

I use index.xhtml for both login and callback.

每次视图渲染时, f:事件触发 pullUserInfo()希望方法触发时,请求参数中提供代码。我知道这是错误的。

The f:event fires pullUserInfo() each time the view renders, hoping that when the method fires, a code is provided in the request params. I am aware that this is all wrong.

这可以通过一个过滤器来完成(即当Facebook用代码)?

Can this be done with a filter (i.e. when Facebook calls back with a code)?

我不是最大的JSF专家,所以我可能会缺少一些基础知识。

I'm not the biggest JSF expert, so I might be missing some basic knowledge.



参考

如何使用SocialAuth与JSF进行重定向?

这个非常漂亮的流程图

推荐答案

答案很简单,同时愚蠢。

The answer is pretty straightforward, and stupid at the same time.

<一个href =https://stackoverflow.com/questions/2138245/session-is-lost-and-created-as-new-in-every-servlet-request>会话在每个servlet请求中丢失并创建为新的

我正在从 http:// devel-win8:1381 / app / login ,但是回调在 http:// dev-machine:1381 / app / callback (这是一个不同的实现,使用servlet,但它将与

I'm firing the login from http://devel-win8:1381/app/login, but the callback is found at http://dev-machine:1381/app/callback (this is a different implementation, with servlets, but it will work with the code in the question as well).

浏览器为裸主机名创建不同的会话。

The browser creates different session for 'naked' host names.

干杯。

这篇关于JSF使用Facebook登录(SocialAuth)并保持会话活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆