【fix sonar】CasClientController文件

This commit is contained in:
梁琦涛
2023-03-08 16:11:49 +08:00
parent 1175ba0d57
commit 3f87c872dc
@@ -1,17 +1,10 @@
package com.jero.modules.cas.controller; package com.jero.modules.cas.controller;
import java.util.List; import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.crypto.SecureUtil;
import org.apache.commons.lang.StringUtils;
import com.jero.common.api.vo.Result; import com.jero.common.api.vo.Result;
import com.jero.common.constant.CacheConstant;
import com.jero.common.constant.CommonConstant; import com.jero.common.constant.CommonConstant;
import com.jero.common.exception.JeroBootException;
import com.jero.common.system.util.JwtUtil; import com.jero.common.system.util.JwtUtil;
import com.jero.common.system.vo.LoginUser;
import com.jero.common.util.RedisUtil; import com.jero.common.util.RedisUtil;
import com.jero.modules.cas.util.CASServiceUtil; import com.jero.modules.cas.util.CASServiceUtil;
import com.jero.modules.cas.util.XmlUtils; import com.jero.modules.cas.util.XmlUtils;
@@ -19,7 +12,8 @@ import com.jero.modules.system.entity.SysDepart;
import com.jero.modules.system.entity.SysUser; import com.jero.modules.system.entity.SysUser;
import com.jero.modules.system.service.ISysDepartService; import com.jero.modules.system.service.ISysDepartService;
import com.jero.modules.system.service.ISysUserService; import com.jero.modules.system.service.ISysUserService;
import org.springframework.beans.BeanUtils; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
@@ -28,9 +22,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j; import java.util.List;
/** /**
* <p> * <p>
@@ -60,9 +54,9 @@ public class CasClientController {
public Object validateLogin(@RequestParam(name="ticket") String ticket, public Object validateLogin(@RequestParam(name="ticket") String ticket,
@RequestParam(name="service") String service, @RequestParam(name="service") String service,
HttpServletRequest request, HttpServletRequest request,
HttpServletResponse response) throws Exception { HttpServletResponse response) {
String MULTI_DEPART = "multi_depart"; String multiDepart = "multi_depart";
Result<JSONObject> result = new Result<JSONObject>(); Result<JSONObject> result = new Result<>();
log.info("Rest api login."); log.info("Rest api login.");
try { try {
String validateUrl = prefixUrl+"/p3/serviceValidate"; String validateUrl = prefixUrl+"/p3/serviceValidate";
@@ -70,11 +64,11 @@ public class CasClientController {
log.info("res."+res); log.info("res."+res);
final String error = XmlUtils.getTextForElement(res, "authenticationFailure"); final String error = XmlUtils.getTextForElement(res, "authenticationFailure");
if(StringUtils.isNotEmpty(error)) { if(StringUtils.isNotEmpty(error)) {
throw new Exception(error); throw new JeroBootException(error);
} }
final String principal = XmlUtils.getTextForElement(res, "user"); final String principal = XmlUtils.getTextForElement(res, "user");
if (StringUtils.isEmpty(principal)) { if (StringUtils.isEmpty(principal)) {
throw new Exception("No principal was found in the response from the CAS server."); throw new JeroBootException("No principal was found in the response from the CAS server.");
} }
log.info("-------token----username---"+principal); log.info("-------token----username---"+principal);
//1. 校验用户是否有效 //1. 校验用户是否有效
@@ -92,13 +86,13 @@ public class CasClientController {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId()); List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
obj.put("departs", departs); obj.put("departs", departs);
if (departs == null || departs.size() == 0) { if (departs == null || departs.isEmpty()) {
obj.put(MULTI_DEPART, 0); obj.put(multiDepart, 0);
} else if (departs.size() == 1) { } else if (departs.size() == 1) {
sysUserService.updateUserDepart(principal, departs.get(0).getOrgCode()); sysUserService.updateUserDepart(principal, departs.get(0).getOrgCode());
obj.put(MULTI_DEPART, 1); obj.put(multiDepart, 1);
} else { } else {
obj.put(MULTI_DEPART, 2); obj.put(multiDepart, 2);
} }
obj.put("token", token); obj.put("token", token);
obj.put("userInfo", sysUser); obj.put("userInfo", sysUser);