如何在JSF中使用SocialAuth进行重定向

如何在JSF中使用SocialAuth进行重定向

本文介绍了如何在JSF中使用SocialAuth进行重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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返回了以下URL:

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();

这真的要花一些晚上才能弄清楚.任何想法都很赞赏,谢谢.

解决方案

除了您在URL中使用localhost之外,我没有看到任何代码级问题.这是 Wiki链接,其中描述了如何使用localhost运行应用程序. /p>

请告诉我这是否无效.

I'm trying to use SocialAuth, the idea is very simple, click in log in with facebook then redirect the user to my website signed in.The log in part I get it, which is below :

1) /index.xhtml

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

2) socialFacebook bean

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 returned the following URL:

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

4) It pass by my init method as BalusC suggest but always prints 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) And it prints nope :( too in my home_facebook page:

<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>

But, I'm a bit confuse how to get the result in my bean and do some verifications as if the user is registered or not for instance. I know, looking some code in Google, that I have to do this, but how can I redirect to my bean and do this and redirect the user to the proper page ?

    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();

This is really taking some nights to figure it out.Any idea is very appreaciated, thanks.

解决方案

I don't see any code level issue except you are using localhost in URL.Here is a wiki link which describes how to run application with localhost.

Please let me know if this does not work.

这篇关于如何在JSF中使用SocialAuth进行重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:22