package demo.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import com.renren.api.client.RenrenApiClient;
import com.renren.api.client.RenrenApiConfig;
import com.renren.api.client.param.impl.AccessToken;
import com.renren.api.client.utils.HttpURLUtils;
import demo.dao.RenrenUserMappingDAO;
import demo.dao.UserDAO;
import demo.model.User;
/**
* 人人连接的登录界面,将跳转到人人网账号登陆界面,
* @author Administrator
*
*/
public class RenrenLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RenrenLoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException {
// TODO Auto-generated method stub
String code = request.getParameter("code");
if (code == null || code.length() == 0) {
//缺乏有效参数,跳转到登录页去
response.sendRedirect("login");
return;
}
//到人人网的OAuth 2.0的token endpoint用code换取access token
String rrOAuthTokenEndpoint = "https://graph.renren.com/oauth/token";
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("client_id", RenrenApiConfig.renrenApiKey);
parameters.put("client_secret", RenrenApiConfig.renrenApiSecret);
parameters.put("redirect_uri", "http://127.0.0.1/demo4web/rr_login");//这个redirect_uri要和之前传给authorization endpoint的值一样
parameters.put("grant_type", "authorization_code");
parameters.put("code", code);
String tokenResult = HttpURLUtils.doPost(rrOAuthTokenEndpoint, parameters);
JSONObject tokenJson = (JSONObject) JSONValue.parse(tokenResult);
if (tokenJson != null) {
String accessToken = (String) tokenJson.get("access_token");
Long expiresIn = (Long) tokenJson.get("expires_in");//距离过期时的时间段(秒数)
String scope = (String) tokenJson.get("scope"); //获得的权限
String refresh_token = (String) tokenJson.get("refresh_token");//refreshToken
//将返回的信息存入session
request.getSession().setAttribute("access_token", accessToken);
request.getSession().setAttribute("expiresIn", expiresIn);
request.getSession().setAttribute("scope", scope);
request.getSession().setAttribute("refresh_token", refresh_token);
//将appid存入session
request.getSession().setAttribute("appId", RenrenApiConfig.renrenAppID);
//用refreshtoken刷新accesstoken(如果用当前时间的秒数-expiresIn>获取token的时间的秒数,则accesstoken过期,用refreshtoken刷新accesstoken)则需要刷新token
long currentTime = System.currentTimeMillis() / 1000;
long expiresTime = currentTime - expiresIn;//即将过期的时间点(秒数)
request.getSession().setAttribute("expiresTime", expiresTime);
//调用SDK获得用户信息
RenrenApiClient apiClient = RenrenApiClient.getInstance();
int rrUid = apiClient.getUserService().getLoggedInUser(new AccessToken(accessToken));
JSONArray userInfo = apiClient.getUserService().getInfo(String.valueOf(rrUid),
"name,headurl,email", new AccessToken(accessToken));
if (userInfo != null && userInfo.size() > 0) {
JSONObject currentUser = (JSONObject) userInfo.get(0);
if (currentUser != null) {
String name = (String) currentUser.get("name");
String headurl = (String) currentUser.get("headurl");
//判断帐号关联表里有没有现成的关联
String username = RenrenUserMappingDAO.getInstance().getUsername(rrUid);
User user;
if (username == null) {
//在帐号关联表里没有记录,用户是第一次来;为这个用户创建一个User对象
User newUser = new User();
newUser.setName(name);
newUser.setHeadurl(headurl);
//是人人网用户
newUser.setFlag(true);
//自动拼装一个username并随即生成一个password;实际实现时,这里应该保证
//拼装出来的username不与其它帐号冲突
username = "renren-" + rrUid;
String password = UUID.randomUUID().toString();
newUser.setUsername(username);
newUser.setPassword(password);
//保存到用户表
UserDAO.getInstance().addUser(newUser);
//保存到帐号关联表
RenrenUserMappingDAO.getInstance().addMapping(rrUid, username);
user = newUser;
} else {
//用户不是第一次来了,已经在帐号关联表里有了
user = UserDAO.getInstance().getUser(username);
}
//将用户身份信息保存在会话里
request.getSession().setAttribute("user", user);
//已登录,跳转到个人主页
response.sendRedirect("profile");
return;
}
}
}
response.sendRedirect("login");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException {
// TODO Auto-generated method stub
}
}