You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
231 lines
8.5 KiB
231 lines
8.5 KiB
3 years ago
|
package com.fr.plugin.handers;
|
||
|
|
||
|
import com.fanruan.api.log.LogKit;
|
||
|
import com.fanruan.api.net.http.HttpKit;
|
||
|
import com.fr.decision.authority.data.User;
|
||
|
import com.fr.decision.fun.impl.BaseHttpHandler;
|
||
|
import com.fr.decision.webservice.utils.DecisionStatusService;
|
||
|
import com.fr.decision.webservice.v10.login.LoginService;
|
||
|
import com.fr.decision.webservice.v10.user.UserService;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.plugin.PLFilter;
|
||
|
import com.fr.plugin.ZtgtConfig;
|
||
|
import com.fr.plugin.transform.ExecuteFunctionRecord;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.store.StateHubService;
|
||
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
|
||
|
import com.fr.web.utils.WebUtils;
|
||
|
|
||
|
import javax.servlet.http.Cookie;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.IOException;
|
||
|
import java.net.URLEncoder;
|
||
|
import java.nio.charset.StandardCharsets;
|
||
|
import java.util.Enumeration;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
|
||
|
public class LoginCallBackHandler extends BaseHttpHandler {
|
||
|
@Override
|
||
|
public RequestMethod getMethod() {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String getPath() {
|
||
|
return "/loginCallback";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean isPublic() {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
@ExecuteFunctionRecord
|
||
|
public void handle(HttpServletRequest req, HttpServletResponse httpServletResponse) throws Exception {
|
||
|
String rfToken = req.getParameter("code");
|
||
|
if (StringUtils.isNotBlank(rfToken)) {
|
||
|
String username = getUsername(rfToken);
|
||
|
User user = UserService.getInstance().getUserByUserName(username);
|
||
|
if (user != null) {
|
||
|
login(req, httpServletResponse, username);
|
||
|
String loginCallBack1 = getCookieByKey(req, "loginCallBack");
|
||
|
if (StringUtils.isNotBlank(loginCallBack1)) {
|
||
|
deleteCookieByName( req, httpServletResponse,"loginCallBack");
|
||
|
sendRedirect(httpServletResponse, loginCallBack1);
|
||
|
return;
|
||
|
}
|
||
|
ZtgtConfig plConfig = ZtgtConfig.getInstance();
|
||
|
sendRedirect(httpServletResponse, plConfig.getFrUrl());
|
||
|
return;
|
||
|
} else {
|
||
|
WebUtils.printAsString(httpServletResponse, username + "用户不存在");
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
sendRedirect(httpServletResponse, getCodeUrl(req));
|
||
|
}
|
||
|
/**
|
||
|
* 根据key获取cookie
|
||
|
* @param req
|
||
|
* @return
|
||
|
*/
|
||
|
public static String getCookieByKey(HttpServletRequest req,String key){
|
||
|
Cookie[] cookies = req.getCookies();
|
||
|
String cookie = "";
|
||
|
|
||
|
if(cookies == null || cookies.length <=0){
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
for(int i = 0; i < cookies.length; i++) {
|
||
|
Cookie item = cookies[i];
|
||
|
if (item.getName().equalsIgnoreCase(key)) {
|
||
|
cookie = item.getValue();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
LogKit.info("cookie:"+cookie);
|
||
|
|
||
|
return cookie;
|
||
|
}
|
||
|
private String deleteCookieByName(HttpServletRequest request, HttpServletResponse response,String name ) {
|
||
|
Cookie[] cookies = request.getCookies();
|
||
|
if (null == cookies) {
|
||
|
FineLoggerFactory.getLogger().debug("没有cookie");
|
||
|
} else {
|
||
|
for (Cookie cookie : cookies) {
|
||
|
if (cookie.getName().equals(name)) {
|
||
|
String cookieValue = cookie.getValue();
|
||
|
//设置值为null
|
||
|
cookie.setValue(null);
|
||
|
//立即销毁cookie
|
||
|
cookie.setMaxAge(0);
|
||
|
cookie.setPath("/");
|
||
|
FineLoggerFactory.getLogger().debug("被删除的cookie名字为:{}", cookie.getName(), cookieValue);
|
||
|
response.addCookie(cookie);
|
||
|
return cookieValue;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return "";
|
||
|
}
|
||
|
private String getCodeUrl(HttpServletRequest req) throws Exception {
|
||
|
ZtgtConfig plConfig = ZtgtConfig.getInstance();
|
||
|
String originalURL = URLEncoder.encode(plConfig.getFrUrl() , StandardCharsets.UTF_8.name());
|
||
|
String baseUrl = plConfig.getValAddr();
|
||
|
String clientId = plConfig.getAppId();
|
||
|
String redirect_uri = URLEncoder.encode(plConfig.getFrUrl()+"/url/loginCallback", "UTF-8");
|
||
|
return baseUrl + "/authorize?response_type=code&client_id=" + clientId + "&state=xplatform&redirect_uri=" + redirect_uri + "&oauth_timestamp=" + System.currentTimeMillis() + "&target_uri=" + originalURL;
|
||
|
}
|
||
|
|
||
|
private class Token {
|
||
|
private String content = StringUtils.EMPTY;
|
||
|
private long expires_in = 0L;
|
||
|
private long timestamp = 0L;
|
||
|
|
||
|
public Token(String content, long expires_in) {
|
||
|
Token.this.content = content;
|
||
|
Token.this.expires_in = expires_in;
|
||
|
Token.this.timestamp = System.currentTimeMillis();
|
||
|
}
|
||
|
|
||
|
public String getContent() {
|
||
|
return content;
|
||
|
}
|
||
|
|
||
|
public long getExpiresIn() {
|
||
|
return expires_in;
|
||
|
}
|
||
|
|
||
|
public boolean isTimeout() {
|
||
|
//考虑到网络传输,有效期我们只算90%
|
||
|
return System.currentTimeMillis() - timestamp > expires_in * 900;
|
||
|
}
|
||
|
|
||
|
public boolean needRefresh() {
|
||
|
//在70%到90%有效期期间进行刷新
|
||
|
return System.currentTimeMillis() - timestamp > expires_in * 700;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String toString() {
|
||
|
return "Token{" +
|
||
|
"content='" + content + '\'' +
|
||
|
", expires_in=" + expires_in +
|
||
|
", timestamp=" + timestamp +
|
||
|
'}';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void sendRedirect(HttpServletResponse res, String url) throws IOException {
|
||
|
Map<String, String> params = new HashMap<>();
|
||
|
params.put("callBack", url);
|
||
|
WebUtils.writeOutTemplate("com/fr/plugin/redirect.html", res, params);
|
||
|
}
|
||
|
|
||
|
private void login(HttpServletRequest req, HttpServletResponse res, String username) {
|
||
|
String token = null;
|
||
|
try {
|
||
|
token = LoginService.getInstance().login(req, res, username);
|
||
|
req.setAttribute("fine_auth_token", token);
|
||
|
FineLoggerFactory.getLogger().error("{} login success from ticket", username);
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage(), e);
|
||
|
FineLoggerFactory.getLogger().error("login failed");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private String getUsername(String code) {
|
||
|
Token token = getAccessToken(code);
|
||
|
ZtgtConfig plConfig = ZtgtConfig.getInstance();
|
||
|
String baseUrl = plConfig.getValAddr();
|
||
|
String clientId = plConfig.getAppId();
|
||
|
String clientSecret = plConfig.getClientSecret();
|
||
|
String url = baseUrl + "/profile?access_token=" + token.getContent() + "&client_id=" + clientId + "&client_secret=" + clientSecret + "&oauth_timestamp=" + System.currentTimeMillis();
|
||
|
try {
|
||
|
String s = HttpKit.get(url);
|
||
|
LogKit.info("token {} get user info from {} profile {}",token,url, s);
|
||
|
JSONObject jsonObject = new JSONObject(s);
|
||
|
return jsonObject.getString("accountId");
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
return StringUtils.EMPTY;
|
||
|
}
|
||
|
|
||
|
private synchronized Token getAccessToken(String code) {
|
||
|
try {
|
||
|
token = getNewToken(code);
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
return token;
|
||
|
}
|
||
|
|
||
|
private static Token token = null;
|
||
|
|
||
|
|
||
|
private Token getNewToken(String code) throws Exception {
|
||
|
ZtgtConfig plConfig = ZtgtConfig.getInstance();
|
||
|
String baseUrl = plConfig.getValAddr();
|
||
|
String clientId = plConfig.getAppId();
|
||
|
String clientSecret = plConfig.getClientSecret();
|
||
|
String redirect_uri = URLEncoder.encode(plConfig.getFrUrl(), "UTF-8");
|
||
|
String url = baseUrl + "/accessToken?client_id=" + clientId + "&client_secret=" + clientSecret + "&code=" + code + "&redirect_uri=" + redirect_uri;
|
||
|
String res = HttpKit.get(url, new HashMap<>());
|
||
|
LogKit.info("get token from url: {} resp:{}", url, res);
|
||
|
if (StringUtils.isEmpty(res)) {
|
||
|
return null;
|
||
|
}
|
||
|
return parseToken(res);
|
||
|
}
|
||
|
|
||
|
private Token parseToken(String res) throws Exception {
|
||
|
JSONObject entries = new JSONObject(res);
|
||
|
String access_token = entries.getString("access_token");
|
||
|
return new Token(access_token, -1);
|
||
|
}
|
||
|
}
|