JavaEE+;socialauth库,登录后存储什么
根据本教程,我正在使用Socialuth库: 在第3步结束后,我不知道在哪里/存储什么。我的意思是我不想强迫用户每次点击都登录。我试着从例子中找出这个问题,但我不能 以下是我所拥有的:JavaEE+;socialauth库,登录后存储什么,java,oauth,oauth-2.0,socialauth,Java,Oauth,Oauth 2.0,Socialauth,根据本教程,我正在使用Socialuth库: 在第3步结束后,我不知道在哪里/存储什么。我的意思是我不想强迫用户每次点击都登录。我试着从例子中找出这个问题,但我不能 以下是我所拥有的: @WebServlet("/success") public class AfterOAuth extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse re
@WebServlet("/success")
public class AfterOAuth extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// get the auth provider manager from session
SocialAuthManager manager = (SocialAuthManager) req.getSession().getAttribute("authManager");
// call connect method of manager which returns the provider object.
// Pass request parameter map while calling connect method.
Map<String, String> paramsMap = SocialAuthUtil.getRequestParametersMap(req);
AuthProvider provider = manager.connect(paramsMap);
// get profile
Profile p = provider.getUserProfile();
// you can obtain profile information
resp.getOutputStream().print(p.getFirstName());
// OK, everything is fine by now what should I store in my Session?
} catch (Exception e) {
throw new ServletException(e);
}
}
}
@WebServlet(“/success”)
公共类AfterOAuth扩展了HttpServlet{
@凌驾
受保护的void doGet(HttpServletRequest-req,HttpServletResponse-resp)抛出ServletException,IOException{
试一试{
//从会话获取身份验证提供程序管理器
SocialAuthManager=(SocialAuthManager)req.getSession().getAttribute(“authManager”);
//调用管理器的connect方法,该方法返回提供程序对象。
//调用connect方法时传递请求参数映射。
Map paramsMap=SocialAuthUtil.getRequestParametersMap(req);
AuthProvider=manager.connect(paramsMap);
//获取配置文件
Profile p=provider.getUserProfile();
//您可以获取配置文件信息
分别为getOutputStream().print(p.getFirstName());
//好的,现在一切都好了,我应该在会话中存储什么?
}捕获(例外e){
抛出新的ServletException(e);
}
}
}
好的,我找到了一个解决方案,只需使用提供的和重写的init()和servlet部分即可:
package com.test.oauth;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import config.KicEngineRootRessourceLoader;
import org.apache.log4j.Logger;
import org.brickred.socialauth.AuthProvider;
import org.brickred.socialauth.AuthProviderFactory;
import org.brickred.socialauth.Contact;
import org.brickred.socialauth.Profile;
import org.brickred.socialauth.SocialAuthConfig;
import org.brickred.socialauth.SocialAuthManager;
import org.brickred.socialauth.util.SocialAuthUtil;
/**
* Created by kic on 19.02.15.
*/
@Named("socialauth")
@SessionScoped
public class SocialAuth implements Serializable {
/**
* Serial version UID generated by Eclipse
*/
private static final long serialVersionUID = 1789108831048043099L;
private static final Logger log = Logger.getLogger( SocialAuth.class);
private String id;
private Profile profile;
private AuthProvider provider;
private String status;
private String viewUrl;
private SocialAuthManager manager;
private SocialAuthConfig config;
public void init() {
id = null;
provider = null;
config = new SocialAuthConfig().getDefault();
try {
Properties oauth = new Properties();
KicEngineRootRessourceLoader.loadProperties(oauth, "oauth_consumer");
config.load(oauth);
manager = new SocialAuthManager();
manager.setSocialAuthConfig(config);
} catch (Exception e) {
e.printStackTrace();
}
}
public SocialAuth() {
init();
}
public String getId() {
return id;
}
/**
* Sets the authentication provider. It is mandatory to do this before
* calling login
*
* @param id
* Can either have values facebook, foursquare, google, hotmail,
* linkedin, myspace, twitter, yahoo OR an OpenID URL
*/
public void setId(final String id) {
this.id = id;
}
/**
* Sets the view URL to which the user will be redirected after
* authentication
*
* @param viewUrl
* Relative URL of the view, for example "/openid.xhtml"
*/
public void setViewUrl(final String viewUrl) {
this.viewUrl = viewUrl;
}
/**
* Gets the relative URL of the view to which user will be redirected after
* authentication
*
* @return relative URL of the view
*/
public String getViewUrl() {
return viewUrl;
}
/**
* This is the most important action. It redirects the browser to an
* appropriate URL which will be used for authentication with the provider
* you set using setId()
*
* @throws Exception
*/
public void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//String url = manager.getAuthenticationUrl(req.getParameter("provider"), successUrl);
String returnToUrl = viewUrl;
String url = manager.getAuthenticationUrl(id, returnToUrl);
// Store in session
req.getSession().setAttribute("authManager", manager);
// redirect
log.info("Redirecting to:" + url);
resp.sendRedirect(url);
}
/**
* Verifies the user when the external provider redirects back to our
* application
*
* @throws Exception
*/
public void connect(HttpServletRequest request) throws Exception {
provider = manager.connect(SocialAuthUtil.getRequestParametersMap(request));
profile= provider.getUserProfile();
}
/**
* Reinitializes the bean
*/
public void logout() {
init();
}
/**
* Returns the Profile information for the user. Should be called only after
* loginImmediately()
*
* @return Profile of the user
*/
public Profile getProfile() {
return profile;
}
/**
* Status of the user to be updated on a provider like Facebook or Twitter.
* Remember this will not give us the current status of the user
*
* @return status message to be updated
*/
public String getStatus() {
return status;
}
/**
* Status of the user to be updated on a provider like Facebook or Twitter.
* To actually update the status, call updateStatus action.
*
* @param status
*/
public void setStatus(final String status) {
this.status = status;
}
/**
* Updates the status on the given provider. Exception will be thrown if the
* provider does not provide this facility
*/
public void updateStatus() throws Exception {
provider.updateStatus(status);
}
/**
* Gets the list of contacts available from the provider. This may be used
* to import contacts of any user in your web application from your chosen
* provider like Gmail, Yahoo or Hotmail
*
* @return list of contacts
*/
public List<Contact> getContactList() throws Exception {
return provider.getContactList();
}
/**
* Retrieves the user profile from the provider.
*
* @return Profile object containing the profile information.
* @throws Exception
*/
public Profile getUserProfile() throws Exception {
return provider.getUserProfile();
}
}
package com.test.oauth;
导入java.io.Serializable;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.List;
导入java.util.Properties;
导入javax.enterprise.context.SessionScoped;
导入javax.faces.context.ExternalContext;
导入javax.faces.context.FacesContext;
导入javax.inject.Named;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入config.kicengineRootResourceLoader;
导入org.apache.log4j.Logger;
导入org.brickred.socialauth.AuthProvider;
导入org.brickred.socialauth.AuthProviderFactory;
导入org.brickred.socialauth.Contact;
导入org.brickred.socialauth.Profile;
导入org.brickred.socialauth.SocialAuthConfig;
导入org.brickred.socialauth.SocialAuthManager;
导入org.brickred.socialauth.util.SocialAuthUtil;
/**
*由kic于2015年2月19日创建。
*/
@命名为(“socialauth”)
@会议范围
公共类SocialAuth实现可序列化{
/**
*Eclipse生成的串行版本UID
*/
私有静态最终长serialVersionUID=1789108831048043099L;
私有静态最终记录器log=Logger.getLogger(SocialAuth.class);
私有字符串id;
个人资料;
私人认证提供者;
私有字符串状态;
私有字符串viewUrl;
私人社会授权经理;
私有社会授权配置;
公共void init(){
id=null;
provider=null;
config=new SocialAuthConfig().getDefault();
试一试{
Properties oauth=新属性();
loadProperties(oauth,“oauth_消费者”);
config.load(oauth);
manager=新的SocialAuthManager();
manager.setSocialAuthConfig(配置);
}捕获(例外e){
e、 printStackTrace();
}
}
公共社会授权(){
init();
}
公共字符串getId(){
返回id;
}
/**
*设置身份验证提供程序。在此之前必须执行此操作
*呼叫登录
*
*@param-id
*可以有facebook、foursquare、google、hotmail、,
*linkedin、myspace、twitter、yahoo或OpenID URL
*/
公共无效集合id(最终字符串id){
this.id=id;
}
/**
*设置查看URL,用户将在之后重定向到该URL
*认证
*
*@param viewUrl
*视图的相对URL,例如“/openid.xhtml”
*/
public void setViewUrl(最终字符串viewUrl){
this.viewUrl=viewUrl;
}
/**
*获取视图的相对URL,用户将在之后重定向到该视图
*认证
*
*@return视图的相对URL
*/
公共字符串getViewUrl(){
返回viewUrl;
}
/**
*这是最重要的操作。它将浏览器重定向到
*将用于与提供商进行身份验证的适当URL
*您可以使用setId()设置
*
*@抛出异常
*/
公共无效登录(HttpServletRequest-req、HttpServletResponse-resp)引发异常{
//字符串url=manager.getAuthenticationUrl(req.getParameter(“提供者”),successUrl);
字符串returnToUrl=viewUrl;
字符串url=manager.getAuthenticationUrl(id,returnToUrl);
//存储会话
req.getSession().setAttribute(“authManager”,manager);
//重定向
log.info(“重定向到:“+url”);
分别发送重定向(url);
}
/**
*当外部提供程序重定向回我们的
*应用
*
*@抛出异常
*/
public void connect(HttpServletRequest请求)引发异常{
provider=manager.connect(SocialAuthUtil.getRequestParametersMap(请求));
profile=provider.getUserProfile();
}
/**
*重新初始化bean
*/
公开作废注销(){
init();
}
/**
*返回用户的配置文件信息。仅应在
*逻辑地
*
*@用户的返回配置文件
*/
公共配置文件getProfile(){
回报曲线;
}
/**
*要在Facebook或Twitter等提供商上更新的用户状态。
*请记住,这不会提供用户的当前状态
*
*@要更新的返回状态消息
*/
公共字符串getStatus()