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.
103 lines
3.7 KiB
103 lines
3.7 KiB
package com.fr.plugin.nfsq.sso; |
|
|
|
import com.fr.decision.authority.data.User; |
|
import com.fr.decision.fun.impl.BaseHttpHandler; |
|
import com.fr.decision.webservice.v10.login.LoginService; |
|
import com.fr.decision.webservice.v10.user.UserService; |
|
import com.fr.general.PropertiesUtils; |
|
import com.fr.json.JSONObject; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.record.analyzer.EnableMetrics; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
|
import com.fr.web.utils.WebUtils; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
/** |
|
* @author fr.open |
|
* @since 2020/08/28 |
|
*/ |
|
@EnableMetrics |
|
public class SsoHttpHandler extends BaseHttpHandler { |
|
|
|
private String apiUser = ""; |
|
|
|
public SsoHttpHandler() { |
|
apiUser = PropertiesUtils.getProperties("xplatform").getProperty("api.get-user"); |
|
} |
|
|
|
@Override |
|
public RequestMethod getMethod() { |
|
return RequestMethod.GET; |
|
} |
|
|
|
@Override |
|
public String getPath() { |
|
return "/getFineToken"; |
|
} |
|
|
|
@Override |
|
public boolean isPublic() { |
|
return true; |
|
} |
|
|
|
@Override |
|
public void handle(HttpServletRequest request, HttpServletResponse response) throws Exception { |
|
if (StringUtils.isBlank(apiUser)) { |
|
sendError(response, "apiUser config is null"); |
|
return; |
|
} |
|
String token = request.getParameter("access_token"); |
|
if (StringUtils.isBlank(token)) { |
|
sendError(response, "token is null"); |
|
return; |
|
} |
|
String userName = getUsername(token); |
|
if (StringUtils.isBlank(userName)) { |
|
sendError(response, "get user is null"); |
|
return; |
|
} |
|
User user = UserService.getInstance().getUserByUserName(userName); |
|
FineLoggerFactory.getLogger().info("get user:" + user); |
|
if (user == null) { |
|
sendError(response, "user not exist"); |
|
} |
|
String fineToken = LoginService.getInstance().login(request, response, userName); |
|
JSONObject jsonObject = new JSONObject("{\"codeDesc\":\"success\",\"success\":true,\"codeNum\":0}"); |
|
jsonObject.put("value", JSONObject.create().put("fine_oath_token", fineToken)); |
|
response.setContentType("application/json;charset=UTF-8"); |
|
WebUtils.printAsJSON(response, jsonObject); |
|
} |
|
|
|
private String getUsername(String accessToken) { |
|
String url = apiUser + "?access_token=" + accessToken; |
|
FineLoggerFactory.getLogger().info("Get user api address is [{}]", url); |
|
try { |
|
String res = HttpUtil.sendGet(url, null, null, null); |
|
FineLoggerFactory.getLogger().info("获取用户信息接口返回内容 ==> {}", res); |
|
JSONObject body = new JSONObject(res); |
|
if (body.getBoolean("success") && body.has("data")) { |
|
body = body.getJSONObject("data"); |
|
if (body.has("account")) { |
|
return body.getString("account"); |
|
} |
|
} |
|
throw new IllegalAccessException(); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error("获取用户名失败", e); |
|
throw new RuntimeException(e); |
|
} |
|
} |
|
|
|
protected void sendError(HttpServletResponse response, String errorCode) { |
|
JSONObject jsonObject = new JSONObject("{\"codeDesc\":\"" + errorCode + "\",\"success\":false,\"codeNum\":70}"); |
|
try { |
|
response.setContentType("application/json;charset=UTF-8"); |
|
WebUtils.printAsJSON(response, jsonObject); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error("输出响应错误失败", e); |
|
} |
|
} |
|
}
|
|
|