我正在尝试做一个“使用 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;
}
}