【问题标题】:How use SocialAuth with JSF to redirect?如何使用带有 JSF 的 SocialAuth 进行重定向?
【发布时间】:2013-02-28 00:45:50
【问题描述】:

我正在尝试使用 SocialAuth,想法很简单,用 facebook 点击log in 然后将用户重定向到我登录的网站。 log in 部分我明白了,如下:

1) /index.xhtml

<h:form id="login-facebook">
    <h:commandButton id="login" action="#{socialFacebook.login}" value="Login"/>
</h:form>

2)socialFacebook

package controller;


@ManagedBean(name="socialFacebook")
@RequestScoped
public class SocialFacebook implements Serializable{
    private static final long serialVersionUID = -4787254243136316495L;

    private String code;

    @PostConstruct
    public void init(){
        try {
            HttpServletRequest request=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            SocialAuthManager manager = (SocialAuthManager)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("authManager");
            Map<String, String> paramsMap = SocialAuthUtil.getRequestParametersMap(request);
            AuthProvider provider = manager.connect(paramsMap);

            // get profile
            Profile p = provider.getUserProfile();
            System.out.println(p.getFullName());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void login(){
        try {
            HttpServletRequest request=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();

            //Create an instance of SocialAuthConfig object
            SocialAuthConfig config  = SocialAuthConfig.getDefault();

            //load configuration. By default load the configuration from oauth_consumer.properties. 
            //You can also pass input stream, properties object or properties file name.
            config.load(); 

            //Create an instance of SocialAuthManager and set config
            SocialAuthManager manager = new SocialAuthManager();
            manager.setSocialAuthConfig(config);

            //URL of YOUR application which will be called after authentication
            //String successUrl = "http://localhost:8080/cc/pages/system/login_facebook.xhtml" + ";jsessionid=" + req.getSession().getId();
            String successUrl = "http://localhost:8080/cc/pages/system/index.xhtml" + ";jsessionid=" + request.getSession().getId();

            // get Provider URL to which you should redirect for authentication.
            // id can have values "facebook", "twitter", "yahoo" etc. or the OpenID URL
            String url = manager.getAuthenticationUrl("facebook", successUrl);

            // Store in session
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("authManager", manager);                
            //redirect to the successful login page
            FacesContext.getCurrentInstance().responseComplete();
            FacesContext.getCurrentInstance().getExternalContext().redirect(url);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    }

3) Facebook 返回以下网址:

http://localhost:8080/cc/pages/system/home_facebook.xhtml;jsessionid=e143aa975fa3f313c677fbcb03e3?code=AQAmJXdQX0B__zJHXnRyPfgaG1CfNUEEEEEEEEEEEEEEEZJLEpsT5s1spd3KtWGWI2HYaIOZKLkrn8axKs4iKwJVQJwJQB_WSs2iWkp2DDDDDDDDDDDDtdRPLPG7psp6r2PYmn7CTm2QNNha7f1QlgmoZtBsIEF0SSSSSSSSSSSSSSSSSSSSSSS8RutAU8dqI2KDE57f#_=_

4) 它按照 BalusC 的建议通过我的 init 方法,但始终打印 nope :(

@ManagedBean(name="redirectFacebook")
@RequestScoped
public class RedirectFacebook implements Serializable{
    private static final long serialVersionUID = -566276017320074630L;

    private String code;
    private Profile profile;


    @PostConstruct
    public void init(){
        try {
            HttpServletRequest request=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpSession session = (HttpSession) request.getAttribute("jsessionid");

            if (request.getAttribute("code") != null)
                System.out.println("code");
            else
                System.out.println("nope :(");

            if (session != null){
                SocialAuthManager manager = (SocialAuthManager)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("authManager");
                Map<String, String> paramsMap = SocialAuthUtil.getRequestParametersMap(request);
                AuthProvider provider = manager.connect(paramsMap);

                // get profile
                profile = provider.getUserProfile();
                System.out.println(profile.getFullName());
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

5) 它还在我的 home_facebook 页面上打印 nope :(

<h:form id="redirect-facebook-form">
    <f:metadata>
        <f:viewParam name="code" value="#{redirectFacebook.code}" />
    </f:metadata>
    <h:panelGroup rendered="#{not empty redirectFacebook.profile}"> 
        Hello, you're successfully associated as #{socialFacebook.profile.firstName} on Facebook
    </h:panelGroup>
    <h:panelGroup rendered="#{empty redirectFacebook.profile}"> 
        Nope :(
    </h:panelGroup>
</h:form>

但是,我有点困惑如何在我的 bean 中获取结果并进行一些验证,例如用户是否已注册。我知道,在 Google 中查找一些代码,我必须这样做,但是我怎样才能重定向到我的 bean 并执行此操作并将用户重定向到正确的页面?

    SocialAuthManager manager = (SocialAuthManager)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("authManager");
    Map<String, String> paramsMap = SocialAuthUtil.getRequestParametersMap(request);
    AuthProvider provider = manager.connect(paramsMap);

    // get profile
    Profile p;
    p = provider.getUserProfile();

这确实需要一些晚上才能弄清楚。 任何想法都非常感谢,谢谢。

【问题讨论】:

  • 最直接的方法是在与回调页面home.xhtml 关联的请求/视图范围 bean 的(后)构造函数中完成这项工作。你试过这个吗?
  • 是的,我做到了,request 我总是NULL。如何将回调页面与 bean 关联?
  • 只需在页面某处引用 bean,例如&lt;h:panelGroup rendered="#{not empty socialAuthBean.profile}"&gt;Hello, you're successfully associated as #{socialAuthBean.profile.username} on Facebook&lt;/h:panelGroup&gt; 等等。或者,通过&lt;f:event type="preRenderView" listener="#{socialAuthBean.init}" /&gt;.
  • @BalusC,我更新了我的帖子和 bean,按照您的建议,这似乎是正确的做法,但不断收到此错误 SEVERE: org.brickred.socialauth.exception.SocialAuthException: Verification code is null
  • code 是请求参数,而不是请求属性。另外,jsessionid 可以也不应该以这种方式获得,容器透明地处理它。使用request.getParameter()externalContext.getRequestParameterMap()获取请求参数。

标签: facebook jsf servlets jsf-2 socialauth


【解决方案1】:

除了您在 URL 中使用 localhost 之外,我没有看到任何代码级别问题。 这是一个wiki link,它描述了如何使用localhost 运行应用程序。

如果这不起作用,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-30
    • 2012-12-12
    • 2012-01-11
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    相关资源
    最近更新 更多