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.
202 lines
6.5 KiB
202 lines
6.5 KiB
3 years ago
|
/*
|
||
|
* Copyright (C), 2018-2021
|
||
|
* Project: starter
|
||
|
* FileName: GlobalRequestFilterBridge
|
||
|
* Author: Louis
|
||
|
* Date: 2021/3/30 22:09
|
||
|
*/
|
||
|
package com.fr.plugin.xxxx.dingtalksyn.request;
|
||
|
|
||
|
import com.fanruan.api.decision.login.LoginKit;
|
||
|
import com.fanruan.api.log.LogKit;
|
||
|
import com.fanruan.api.net.NetworkKit;
|
||
|
import com.fanruan.api.net.http.HttpKit;
|
||
|
import com.fanruan.api.util.IOKit;
|
||
|
import com.fanruan.api.util.StringKit;
|
||
|
import com.fr.base.TemplateUtils;
|
||
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
|
||
|
import com.fr.decision.webservice.utils.DecisionServiceConstants;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.plugin.xxxx.dingtalksyn.config.DingSynConfig;
|
||
|
import com.fr.web.utils.WebUtils;
|
||
|
|
||
|
import javax.servlet.FilterChain;
|
||
|
import javax.servlet.FilterConfig;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
|
||
|
import static com.fr.plugin.xxxx.dingtalksyn.utils.DingAPI.GETTOKEN;
|
||
|
import static com.fr.plugin.xxxx.dingtalksyn.utils.DingAPI.GET_USER_INFO;
|
||
|
|
||
|
/**
|
||
|
* <Function Description><br>
|
||
|
* <GlobalRequestFilterBridge>
|
||
|
*
|
||
|
* @author fr.open
|
||
|
* @since 1.0.0
|
||
|
*/
|
||
|
public class GlobalRequestFilterBridge extends AbstractGlobalRequestFilterProvider {
|
||
|
public static final String TPL_PATH = "/com/fr/plugin/xxxx/dingtalksyn/web/codePage.html";
|
||
|
public static final String DINGTALK_OPEN_JS = "/com/fr/plugin/xxxx/dingtalksyn/web/dingtalk.open.js";
|
||
|
public static final String CODE = "code";
|
||
|
public static final String DING_TALK_LOGIN = "dt";
|
||
|
|
||
|
private DingSynConfig config;
|
||
|
|
||
|
/**
|
||
|
* 过滤器名称
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
@Override
|
||
|
public String filterName() {
|
||
|
return "DingTalkSynFilter";
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 过滤规则
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
@Override
|
||
|
public String[] urlPatterns() {
|
||
|
return new String[]{"/decision/*"};
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 过滤器初始化
|
||
|
*
|
||
|
* @param filterConfig
|
||
|
*/
|
||
|
@Override
|
||
|
public void init(FilterConfig filterConfig) {
|
||
|
this.config = DingSynConfig.getInstance();
|
||
|
super.init(filterConfig);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 过滤器处理
|
||
|
*
|
||
|
* @param request
|
||
|
* @param response
|
||
|
* @param filterChain
|
||
|
*/
|
||
|
@Override
|
||
|
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) {
|
||
|
try {
|
||
|
if (operation(request, response)) {
|
||
|
filterChain.doFilter(request, response);
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
LogKit.error(e.getMessage(), e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 用户验证登陆操作
|
||
|
*
|
||
|
* @param req
|
||
|
* @param res
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
private boolean operation(HttpServletRequest req, HttpServletResponse res) throws Exception {
|
||
|
// 判断单点功能是否开启
|
||
|
if (!this.config.getSsoEnable()) {
|
||
|
return true;
|
||
|
}
|
||
|
String pathInfo = (req.getPathInfo() != null) ? req.getPathInfo() : StringKit.EMPTY;
|
||
|
if (StringKit.equals(DINGTALK_OPEN_JS, pathInfo)) {
|
||
|
WebUtils.printAsString(res, IOKit.readResourceAsString(DINGTALK_OPEN_JS));
|
||
|
return false;
|
||
|
}
|
||
|
//DingTalk登陆参数
|
||
|
String dt = NetworkKit.getHTTPRequestParameter(req, DING_TALK_LOGIN);
|
||
|
if (StringKit.isBlank(dt)) {
|
||
|
return true;
|
||
|
}
|
||
|
String code = NetworkKit.getHTTPRequestParameter(req, CODE);
|
||
|
LogKit.info("dingtalksyn-GlobalRequestFilterBridge-operation-code:{}", code);
|
||
|
if (StringKit.isEmpty(code)) {
|
||
|
loginPage(req, res);
|
||
|
return false;
|
||
|
}
|
||
|
String accessToken = getAccessToken(code);
|
||
|
String username = getUsername(code, accessToken);
|
||
|
LogKit.info("dingtalksyn-GlobalRequestFilterBridge-operation-username:{}", username);
|
||
|
if (StringKit.isEmpty(username)) {
|
||
|
return true;
|
||
|
}
|
||
|
String tokenFR = LoginKit.login(req, res, username);
|
||
|
req.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, tokenFR);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 钉钉获取code页面
|
||
|
*
|
||
|
* @param req
|
||
|
* @param res
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
private void loginPage(HttpServletRequest req, HttpServletResponse res) throws Exception {
|
||
|
Map<String, String> parameterMap = new HashMap<>();
|
||
|
parameterMap.put("corpId", this.config.getCorpId());
|
||
|
parameterMap.put("openJs", WebUtils.createServletURL(req) + DINGTALK_OPEN_JS);
|
||
|
parameterMap.put("remoteServletURL", getRemoteServletURL(WebUtils.getOriginalURL(req)));
|
||
|
String codePage = TemplateUtils.renderTemplate(TPL_PATH, parameterMap);
|
||
|
WebUtils.printAsString(res, codePage);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 处理请求url加入code参数
|
||
|
*
|
||
|
* @param url
|
||
|
* @return
|
||
|
*/
|
||
|
private String getRemoteServletURL(String url) {
|
||
|
if (url.contains("?")) {
|
||
|
return url + "&" + CODE + "=";
|
||
|
}
|
||
|
return url + "?" + CODE + "=";
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取access_token
|
||
|
*
|
||
|
* @param code
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
private String getAccessToken(String code) throws Exception {
|
||
|
Map<String, String> tokenParams = new HashMap<>();
|
||
|
tokenParams.put("appkey", this.config.getAppKey());
|
||
|
tokenParams.put("appsecret", this.config.getAppSecret());
|
||
|
tokenParams.put("code", code);
|
||
|
LogKit.info("dingtalksyn-GlobalRequestFilterBridge-getAccessToken-params:{}", tokenParams);
|
||
|
String res = HttpKit.get(GETTOKEN, tokenParams);
|
||
|
LogKit.info("dingtalksyn-GlobalRequestFilterBridge-getAccessToken-res:{}", res);
|
||
|
if (StringKit.isEmpty(res)) {
|
||
|
return StringKit.EMPTY;
|
||
|
}
|
||
|
return new JSONObject(res).getString("access_token");
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 通过凭证获得username
|
||
|
*
|
||
|
* @param code
|
||
|
* @param accessToken
|
||
|
* @return
|
||
|
*/
|
||
|
private String getUsername(String code, String accessToken) throws Exception {
|
||
|
Map<String, String> userInfoParams = new HashMap<>();
|
||
|
userInfoParams.put("access_token", accessToken);
|
||
|
userInfoParams.put("code", code);
|
||
|
String userRes = HttpKit.get(GET_USER_INFO, userInfoParams);
|
||
|
LogKit.info("dingtalksyn-GlobalRequestFilterBridge-getUsername-userRes:{}", userRes);
|
||
|
return new JSONObject(userRes).getString("userid");
|
||
|
}
|
||
|
}
|