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.
134 lines
4.5 KiB
134 lines
4.5 KiB
2 years ago
|
package com.eco.plugin.xx.bssso2.controller;
|
||
|
|
||
|
import com.eco.plugin.xx.bssso2.config.PluginSimpleConfig;
|
||
|
import com.eco.plugin.xx.bssso2.utils.FRUtils;
|
||
|
import com.eco.plugin.xx.bssso2.utils.HttpUtils;
|
||
|
import com.eco.plugin.xx.bssso2.utils.ResponseUtils;
|
||
|
import com.eco.plugin.xx.bssso2.utils.Utils;
|
||
|
import com.fr.decision.webservice.annotation.LoginStatusChecker;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.plugin.context.PluginContexts;
|
||
|
import com.fr.stable.fun.Authorize;
|
||
|
import com.fr.third.springframework.stereotype.Controller;
|
||
|
import com.fr.third.springframework.web.bind.annotation.GetMapping;
|
||
|
import com.fr.third.springframework.web.bind.annotation.ResponseBody;
|
||
|
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.IOException;
|
||
|
import java.net.URLEncoder;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Locale;
|
||
|
import java.util.Map;
|
||
|
|
||
|
@Controller
|
||
|
@LoginStatusChecker(required = false)
|
||
|
@Authorize(callSignKey = "com.fr.plugin.xx.bssso2")
|
||
|
public class ControllerSelf {
|
||
|
|
||
|
/**
|
||
|
* 单点接口
|
||
|
* @param req
|
||
|
* @param res
|
||
|
*/
|
||
|
@GetMapping(value = "/ssologin")
|
||
|
@ResponseBody
|
||
|
public void ssologin(HttpServletRequest req,HttpServletResponse res) throws IOException {
|
||
|
if(!PluginContexts.currentContext().isAvailable()){
|
||
|
ResponseUtils.failedResponse(res, "插件授权过期,请联系管理员!");
|
||
|
return ;
|
||
|
}
|
||
|
|
||
|
String redirect = req.getParameter("redirect");
|
||
|
|
||
|
if(Utils.isNullStr(redirect)){
|
||
|
ResponseUtils.failedResponse(res,"redirect不能为空");
|
||
|
}
|
||
|
|
||
|
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
|
||
|
String authUrl = psc.getAuthUrl()+"?client_id="+psc.getClientId()+"&redirect_uri="+ psc.getCallback()+"&scope=all&response_type=code"+"&oauth_timestamp="+System.currentTimeMillis()+"&state="+URLEncoder.encode(redirect);
|
||
|
FRUtils.FRLogInfo("auth:"+authUrl);
|
||
|
|
||
|
res.sendRedirect(authUrl);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 单点回调接口
|
||
|
* @param req
|
||
|
* @param res
|
||
|
* @throws IOException
|
||
|
*/
|
||
|
@GetMapping(value = "/ssocallback")
|
||
|
@ResponseBody
|
||
|
public void ssocallback(HttpServletRequest req,HttpServletResponse res){
|
||
|
String url = FRUtils.getAllUrl(req);
|
||
|
FRUtils.FRLogInfo("ssocallback:"+url);
|
||
|
if(!PluginContexts.currentContext().isAvailable()){
|
||
|
ResponseUtils.failedResponse(res, "插件授权过期,请联系管理员!");
|
||
|
return ;
|
||
|
}
|
||
|
|
||
|
String code = req.getParameter("code");
|
||
|
|
||
|
if(Utils.isNullStr(code)){
|
||
|
ResponseUtils.failedResponse(res,"code不能为空!");
|
||
|
}
|
||
|
|
||
|
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
|
||
|
String token = getToken(code,psc);
|
||
|
|
||
|
if(Utils.isNullStr(token)){
|
||
|
ResponseUtils.failedResponse(res,"获取token失败!");
|
||
|
}
|
||
|
|
||
|
String username = getUsername(token,psc);
|
||
|
String redirect = url.split("state=")[1];
|
||
|
|
||
|
FRUtils.login(req,res,username,redirect);
|
||
|
}
|
||
|
|
||
|
private String getUsername(String token, PluginSimpleConfig psc) {
|
||
|
String url = psc.getUserUrl();
|
||
|
Map<String,String> param = new HashMap();
|
||
|
param.put("access_token",token);
|
||
|
FRUtils.FRLogInfo("param:"+param.toString());
|
||
|
|
||
|
String result = HttpUtils.HttpPostWWWForm(url,null,param);
|
||
|
FRUtils.FRLogInfo("result"+result);
|
||
|
if(Utils.isNullStr(result)){
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
JSONObject userinfo = new JSONObject(result);
|
||
|
FRUtils.FRLogInfo(userinfo.toString());
|
||
|
FRUtils.FRLogInfo(userinfo.getString("yumADAccount"));
|
||
|
FRUtils.FRLogInfo(userinfo.getString("yumADAccount").toLowerCase(Locale.ROOT));
|
||
|
|
||
|
|
||
|
return new JSONObject(result).getString("yumADAccount").toLowerCase(Locale.ROOT);
|
||
|
}
|
||
|
|
||
|
private String getToken(String code, PluginSimpleConfig psc) {
|
||
|
String url = psc.getTokenUrl();
|
||
|
Map<String,String> param = new HashMap();
|
||
|
param.put("code",code);
|
||
|
param.put("client_id",psc.getClientId());
|
||
|
param.put("client_secret",psc.getSecret());
|
||
|
param.put("redirect_uri",psc.getCallback());
|
||
|
param.put("oauth_timestamp",String.valueOf(System.currentTimeMillis()));
|
||
|
param.put("grant_type","authorization_code");
|
||
|
|
||
|
FRUtils.FRLogInfo("param:"+param.toString());
|
||
|
|
||
|
String result = HttpUtils.HttpPostWWWForm(url,null,param);
|
||
|
|
||
|
if(Utils.isNullStr(result)){
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
return new JSONObject(result).getString("access_token");
|
||
|
}
|
||
|
|
||
|
}
|