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.
 
 

108 lines
3.4 KiB

package com.eco.plugin.gfkdsso.controller;
import com.eco.plugin.gfkdsso.config.PluginSimpleConfig;
import com.eco.plugin.gfkdsso.utils.FRUtils;
import com.eco.plugin.gfkdsso.utils.HttpUtils;
import com.eco.plugin.gfkdsso.utils.ResponseUtils;
import com.eco.plugin.gfkdsso.utils.Utils;
import com.fr.decision.webservice.annotation.LoginStatusChecker;
import com.fr.json.JSONObject;
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;
@Controller
@LoginStatusChecker(required = false)
public class ControllerSelf {
@GetMapping(value = "/ssologin")
@ResponseBody
public void ssologin(HttpServletRequest req,HttpServletResponse res){
String token = req.getParameter("admin_token");
String redirect = req.getParameter("redirect");
if(Utils.isNullStr(token) || Utils.isNullStr(redirect)){
ResponseUtils.failedResponse(res,"请求非法");
return ;
}
PluginSimpleConfig pluginSimpleConfig = PluginSimpleConfig.getInstance();
String username = null;
try {
username = getUsername(token,pluginSimpleConfig);
} catch (Exception e) {
e.printStackTrace();
ResponseUtils.failedResponse(res,"获取用户信息异常!");
return ;
}
FRUtils.login(req,res,username,redirect,token);
}
@GetMapping(value = "/ssologout")
@ResponseBody
public void ssologout(HttpServletRequest req,HttpServletResponse res){
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
logout(req,psc);
try {
res.sendRedirect(psc.getLogoutUrl());
} catch (IOException e) {
e.printStackTrace();
ResponseUtils.failedResponse(res,"跳转登出地址异常!");
}
}
/**
* 登出
* @param req
* @param psc
*/
public void logout(HttpServletRequest req, PluginSimpleConfig psc) {
String token = String.valueOf(req.getSession(true).getAttribute("admin_token"));
if(Utils.isNullStr(token)){
return ;
}
String result = requestinfo(token,psc,psc.getLogoutserviceid());
}
/**
* 获取用户信息
* @param token
* @param pluginSimpleConfig
* @return
*/
private String getUsername(String token, PluginSimpleConfig pluginSimpleConfig) throws Exception{
String result = requestinfo(token,pluginSimpleConfig,pluginSimpleConfig.getUserserviceid());
return new JSONObject(result).getJSONObject("Data").getJSONObject("user").getString("account");
}
/**
* 发起请求
* @param token
* @param pluginSimpleConfig
* @param
* @return
*/
private String requestinfo(String token, PluginSimpleConfig pluginSimpleConfig, String serviceid) {
String url = pluginSimpleConfig.getSsoUrl();
JSONObject formParam = new JSONObject();
formParam.put("cookie",token);
JSONObject param = new JSONObject();
param.put("serviceId",serviceid);
param.put("method","GET");
param.put("dataType","form");
param.put("formParams",formParam);
return HttpUtils.HttpPostJson(url,param.toString(),null);
}
}