2013-02-17 97 views
0

我正在嘗試做一個'用facebook登錄'按鈕,這工作正常,但我想重定向到另一個頁面,用戶數據填寫在表單中。重定向到JSF中填充表單的另一個頁面?

SocialFacebook控制器

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#_=_ 

現在的事情,我會l IKE得到這個代碼在我的豆,做檢查和填寫表格中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我請求。 如何獲取此代碼,然後執行檢查並填寫表單?

** 編輯

SocialFacebook.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; 
    } 
} 
+0

我不確定是否瞭解您的具體問題。我沒有看到你試圖訪問任何地方的請求參數。如果你的bean是一個請求作用域,你嘗試過''externalContext.getRequestParameterMap()。get(「code」)'甚至'@ManagedProperty(「#{param.code}」)'嗎? – BalusC 2013-02-17 03:16:07

+0

@BalusC你好我的朋友。其實我不需要直接訪問代碼參數,因爲我只是初始化HttpServletRequest對象,並在這裏使用'Map paramsMap = SocialAuthUtil.getRequestParametersMap(request);',但是這不斷給出一個異常說這是無法讀取'code'參數。是的,我的bean是一個請求的範圍。我嘗試了很多可能性,但目前爲止沒有任何工作。 – 2013-02-17 03:47:48

+0

@BalusC,我發佈我的整個豆在我的文章。 我需要的是與Facebook登錄並重定向到我的網頁'寄存器',並填寫表格中的用戶數據。 – 2013-02-17 03:53:46

回答

0

我解決我的問題建立一個webfilter它得到的請求,然後responde我可以填補用戶日期我的網址,並得到再在我的表格頁上:

@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> 
+0

我不知道是否是最好的解決方案,如果有人有另一個想法,請免費分享。 – 2013-02-17 12:52:47

相關問題