JSD-9368 JWT单点登录
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.

89 lines
3.4 KiB

package com.eco.plugin.xxxx.ncgdjtsso.controller;
import com.eco.plugin.xxxx.ncgdjtsso.config.PluginSimpleConfig;
import com.eco.plugin.xxxx.ncgdjtsso.utils.FRUtils;
import com.eco.plugin.xxxx.ncgdjtsso.utils.JwtUtil;
import com.eco.plugin.xxxx.ncgdjtsso.utils.ResponseUtils;
import com.eco.plugin.xxxx.ncgdjtsso.utils.Utils;
import com.fr.decision.webservice.annotation.LoginStatusChecker;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;
@Controller
@LoginStatusChecker(required = false)
@FunctionRecorder
@Authorize(callSignKey = "com.eco.plugin.xxxx.ncgdjtsso")
public class ControllerSelf {
@GetMapping(value = "/ssologin")
@ResponseBody
public void ssologin(HttpServletRequest req,HttpServletResponse res){
if (PluginContexts.currentContext().isAvailable()) {
HttpSession session = req.getSession(true);
String token = req.getParameter("token");
if (Utils.isNotNullStr(token)) {
session.setAttribute("ssotoken", token);
}
//获取用户信息
Map result = validJwtFromRequest(req,res,token);
if(result == null){
ResponseUtils.responseText(res,"token校验失败");
return ;
}
String status = String.valueOf(result.get("status"));
//无权限
if ("9".equals(status))
{
ResponseUtils.responseText(res,"对不起,您无权限访问帆软系统!请联系管理员");
return;
}
//token为空或者验证失败
if (Utils.isNullStr(token) && !"0".equals(status)) {
//注销系统
session.invalidate();
try {
res.sendRedirect(PluginSimpleConfig.getInstance().getLogouturl());
} catch (IOException e) {
FRUtils.FRLogError("跳转异常");
}
return;
} else {
// token 验证成功后查询用户信息并设定登录成功状态。如果用户不存在给出页面提示。
// 以下是平台的认证登录,非基础平台的请按照自己系统认证逻辑处理。
String userName = (String) result.get(JwtUtil.USERNAME_KEY);
FRUtils.FRLogInfo("userName:"+userName);
String redirect = req.getParameter("redirect");
FRUtils.login(req,res,userName,redirect);
return;
}
}
ResponseUtils.responseText(res,"插件授权过期,请联系管理员!");
}
private Map validJwtFromRequest(HttpServletRequest req, HttpServletResponse res, String sessionToken) {
Map result = null;
try {
result = JwtUtil.getJwtTokenValidator().validateToken(sessionToken);
} catch (Exception e) {
FRUtils.FRLogError("验证token异常:"+e.getMessage());
}
return result;
}
}