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.

84 lines
3.2 KiB

package com.eco.plugin.xx.ynjtsso.controller;
import com.eco.plugin.xx.ynjtsso.config.PluginSimpleConfig;
import com.eco.plugin.xx.ynjtsso.utils.FRUtils;
import com.eco.plugin.xx.ynjtsso.utils.HttpUtils;
import com.eco.plugin.xx.ynjtsso.utils.ResponseUtils;
import com.eco.plugin.xx.ynjtsso.utils.Utils;
import com.fr.decision.webservice.annotation.LoginStatusChecker;
import com.fr.json.JSONObject;
import com.fr.plugin.context.PluginContexts;
import com.fr.plugin.transform.FunctionRecorder;
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.util.HashMap;
import java.util.Map;
@Controller
@LoginStatusChecker(required = false)
@FunctionRecorder
@Authorize(callSignKey = "com.eco.plugin.xx.ynjtsso")
public class ControllerSelf {
@GetMapping(value = "/ssologin")
@ResponseBody
public void ssologin(HttpServletRequest request,HttpServletResponse response){
//插件授权校验,可忽略
if(!PluginContexts.currentContext().isAvailable()){
ResponseUtils.failedResponse(response,"插件授权过期!");
return ;
}
//配置类
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
//获取token的参数名
String tokenstr = psc.getTokenstr();
//获取token
String token = request.getParameter(tokenstr);
//获取跳转链接,登陆成功后跳转用
String redirect = request.getParameter("redirect");
//访问接口,获取用户名
String username = getUsername(token,psc);
//帆软的登陆方法,已经封装好
//四个参数,1.request 2 response 此两项为固定的httpServilet参数,直接传入即可
//3.username 登陆用户名 4、跳转链接,传""时将不进行跳转,传其他值将自动跳转到相应页面
FRUtils.login(request,response,username,redirect);
}
//访问接口,获取用户名
private static String getUsername(String token,PluginSimpleConfig psc){
//根据文档拼接http请求链接
// String tokenurl = psc.getUserinfo()+"?access_token="+token;
String tokenurl = psc.getUserinfo();
FRUtils.FRLogInfo("token:"+token);
Map<String,String> header = new HashMap<String,String>();
header.put("Authorization","Bearer "+token);
//发送http请求获取返回值
//HttpUtils.httpGet已经封装好,不用管实现,三个参数 1.请求url 2、cookie 3、请求头 2和3没有直接传null即可
String result = HttpUtils.httpGet(tokenurl,null,header);
if(Utils.isNullStr(result)){
return "";
}
//将接口返回信息转换为json对象,返回信息格式见文档
JSONObject jsonResult = new JSONObject(result);
//获取用户名 loginName为本次单点的用户名
String username = jsonResult.getString("loginName");
return username;
}
}