首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JSF中处理会话,同时使用socialauth API进行登录

在JSF中处理会话,同时使用socialauth API进行登录
EN

Stack Overflow用户
提问于 2011-12-02 17:21:39
回答 1查看 1.4K关注 0票数 0

我一直在使用JSF中的社交认证API,为了获得facebook的登录,我不知道是否正确地创建了一个会话。我已经在网上搜索了一些东西,正在做这件事。现在我发现了一些错误。

根据社交身份验证,我们首先需要调用一个函数,该函数有助于从Facebook获取身份验证URL,在该函数中,我需要创建一个会话,并使用用户登录到facebook时来自facebook的参数为其设置属性。

所以我的第一个视图页面将只包含一个调用相应函数的命令按钮。

代码语言:javascript
复制
            <h:form><h:commandButton value="submit" action="#{socialNetworkAuthentication.facebookAuthentication}"></h:commandButton></h:form>

然后调用的函数...

代码语言:javascript
复制
                 public String facebookAuthentication(){

    try{


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

 String propUrl="oauth_consumer.properties";


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

          //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://chennaivolunteers.com/ChennaiVolunteers/faces/cv/employee-profile.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
          HttpServletRequest request=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpSession ses = request.getSession(true);

          ses.setAttribute("authManager", manager);
          System.out.println(url);
          FacesContext.getCurrentInstance().responseComplete();
       FacesContext.getCurrentInstance().getExternalContext().redirect(url);

        }

最后,我重定向到facebook给出的相应网址,在用户登录facebook后,它会自动移动到上面代码中提到的succesURL。

在我的successURL中,我只有一个outputtext。

代码语言:javascript
复制
                          <tr><td class="profile-head"><h:outputText id="name" value="#{employeeProfile.profileName}" /></td></tr>
            <tr><td class="content-black">
            <div class="padding-2"><h:outputText id="name" value="#{employeeProfile.profileName}" />

在支持bean中,我创建了一个会话来获取我之前设置的属性。

代码语言:javascript
复制
                  public class EmployeeProfile {

public String profileName;

public String getProfileName() throws Exception  {

HttpServletRequest request=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    HttpSession ses = request.getSession(true);
    SocialAuthManager m = (SocialAuthManager)ses.getAttribute("authManager");        
    AuthProvider provider = m.connect(SocialAuthUtil.getRequestParametersMap(request));
    Profile p = provider.getUserProfile();
      String userName=p.getFirstName();
      System.out.println(userName);

    return userName;

}

public void setProfileName(String profileName) {
    this.profileName = profileName;
}

当我在控制台中打印用户名时,它确实发生了,但它不是这个支持bean的视图页面,已经赶上了两个异常,如下所示。

代码语言:javascript
复制
             1.  javax.faces.FacesException: Could not retrieve value of component with path : {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /cv/employee-profile.xhtml][Class: javax.faces.component.html.HtmlOutputText,Id: name]}
           Caused by: javax.el.ELException: /cv/employee-profile.xhtml at line 133 and column 105 value="#{employeeProfile.profileName}": Error reading 'profileName' on type socialServlet.EmployeeProfile       


            2.javax.faces.FacesException: This is not the same SocailAuthManager object that was used for login.Please check if you have called getAuthenticationUrl() method before calling connect()
              Caused by: This is not the same SocailAuthManager object that was used for login.Please check if you have called getAuthenticationUrl() method before calling connect()

最后一个是社交身份验证API的内置异常,但我不认为这有任何问题,因为当我用servlet尝试它时,一切正常,我认为我在JSF会话中犯了一些错误。但是我不知道我哪里错了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-03 14:28:45

我的问题现在解决了..我犯的错误是,我再次调用connect()函数。现在我已经在构造器中完成了所有的工作。它工作得很好。

代码语言:javascript
复制
                public class EmployeeProfile {


public EmployeeProfile() throws Exception {
    // TODO Auto-generated constructor stub




        ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest)ectx.getRequest();
        HttpSession session = request.getSession(true);
         SocialAuthManager m = (SocialAuthManager)session.getAttribute("authManager"); 
         AuthProvider provider = m.connect(SocialAuthUtil.getRequestParametersMap(request));
         Profile p = provider.getUserProfile();
             String userName=p.getFirstName();    
              System.out.println(userName);
              setProfileName(userName);
              setBirthDate(p.getDob());
              setImageUrl(p.getProfileImageURL());
              p.getGender();


}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8354162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档