0

我正在尝试做一个“使用 facebook 登录”按钮,它工作正常,但我想重定向到另一个页面,用户数据填写在表单中。

社交 Facebook 控制器

public void login(){
    try {
        //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/register.xhtml";

        // 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
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("authManager", manager);

        //after check out in facebook, redirect to the proper page
        logged();

        //redirect to the successful login page
        FacesContext.getCurrentInstance().getExternalContext().redirect(url);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

因此,当用户点击按钮时,log in with facebook我的 ' ' index.xhtml

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

它重定向到register.xhtml具有如下 URL 的页面:

http://localhost:8080/cc/pages/system/register.xhtml?code=AQC_3oCPjlyvZ51dpzxVdBNS1JfgwwZluBSduU7FG01esgVQT6Qxq8gWYRUsGz64aXDvXB1195m0CHZGmdvsmjLxtmbuUSSSqH7i49pcb6g9Begt4Yol1rqWFQGGGGGGGGGGGJ9mlWiEq4Aknlh1J2su2a9l0GzyLB21J4BgNgfBw3DUtwn-RkT00E7BsFpISiXKE7EVsT5NgxPBtOWIUY#_=_

现在的问题是,我想在我的 bean 中获取此代码并进行检查并填写表单register.xhtml 所以我在同一个 bean 中创建了这个方法:

private void logged(){
    FacesContext facesContext = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    SocialAuthManager manager = (SocialAuthManager) session.getAttribute("authManager");
    try {
        // get the auth provider manager from session
        if (manager != null){
            // call connect method of manager which returns the provider object. 
            // Pass request parameter map while calling connect method.
            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            Map<String, String> paramsMap = SocialAuthUtil.getRequestParametersMap(request);
            AuthProvider provider = manager.connect(paramsMap);

            // get profile
            Profile p = provider.getUserProfile();
            session.setAttribute("profile", p);
        }

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

但是我无法code从我的请求中获取参数。我怎样才能得到这个代码,然后检查并填写表格?

** 编辑

社交Facebook.java

package jpa.control;

// imports..

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

    @ManagedProperty("#{param.code}")
    private String code;

    public void login(){
        try {
            //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/register.xhtml";

            // 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
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
            session.setAttribute("authManager", manager);

            //after check out in facebook, redirect to the proper page
            logged();

            //redirect to the successful login page
            FacesContext.getCurrentInstance().getExternalContext().redirect(url);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private void logged(){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        SocialAuthManager manager = (SocialAuthManager) session.getAttribute("authManager");

        System.out.println("*******************************");
        System.out.println(code); // keeps return NULL everytime
        System.out.println("*******************************");

        try {
            // get the auth provider manager from session
            if (manager != null){
                // call connect method of manager which returns the provider object. 
                // Pass request parameter map while calling connect method.
                HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
                Map<String, String> paramsMap = SocialAuthUtil.getRequestParametersMap(request);
                AuthProvider provider = manager.connect(paramsMap);

                // get profile
                Profile p = provider.getUserProfile();
                session.setAttribute("profile", p);
            }

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


    // Get's and Set's
    public String getCode() {
        return code;
    }

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

1 回答 1

0

我解决了创建一个webfilter获取请求和响应的问题,然后我可以在我的 url 中填写用户日期,然后在我的表单页面中获取:

@WebFilter("/facebook/*")
public class LoginFilter implements Filter {

    @EJB UserEAO userEAO;

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {    
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession();
        FacesContext facesContext = FacesContext.getCurrentInstance();

        try {

            SocialAuthManager manager = (SocialAuthManager) session.getAttribute("authManager");
            Map<String, String> paramsMap = SocialAuthUtil.getRequestParametersMap(request);
            AuthProvider provider = manager.connect(paramsMap);

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

            if (userEAO.find(p.getEmail()) == null){
                response.sendRedirect
                (
                    request.getContextPath() + 
                    "/pages/system/register.xhtml?" +
                    "firstName=" + p.getFirstName()
                );
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

这是我的register页面:

<ui:composition template="/resources/jsf/include/default.xhtml">
    <ui:define name="title">
    </ui:define>

    <ui:define name="header">
    </ui:define>

    <ui:define name="content">

        <h:form id="register_form">
            <f:metadata>
                  <f:viewParam name="firstName" value="#{userc.userb.user.firstName}" />
               </f:metadata>

            Name: <h:message id="m_name" for="name" styleClass="red" />
            <h:inputText id="name" value="#{userc.userb.user.firstName}">
                <f:validateLength minimum="1" maximum="20" />
                <f:ajax event="blur" render="m_name" />
            </h:inputText>
于 2013-02-17T12:52:01.723 回答