| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package cn.com.goldenwater.dcproj.controller.sso;
- import cn.com.goldenwater.core.web.BaseController;
- import cn.com.goldenwater.core.web.BaseResponse;
- import cn.com.goldenwater.dcproj.model.BisInspAllRlationPers;
- import cn.com.goldenwater.dcproj.service.CdCasService;
- import cn.com.goldenwater.dcproj.utils.HttpClientUtils;
- import cn.com.goldenwater.dcproj.utils.StringUtils;
- import cn.com.goldenwater.id.util.UuidUtil;
- import com.alibaba.fastjson.JSONObject;
- import io.swagger.annotations.Api;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.util.DigestUtils;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import javax.servlet.http.HttpServletRequest;
- import java.io.UnsupportedEncodingException;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.regex.Pattern;
- /**
- * CdCasController
- * 成都市水务局 单点登录
- *
- * @author lxf
- * @version 1.0
- * @date 2022/12/08 18:26
- **/
- @Api(value = "成都单点登录", tags = "成都单点登录")
- @RestController
- @RequestMapping("/cas/cd")
- public class CdCasController extends BaseController {
- private static final Logger logger = LoggerFactory.getLogger(CdCasController.class);
- @Value("${cd.auth.baseurl}")
- private String cdAuthBaseUrl;
- @Value("${cd.auth.client_uuid}")
- private String cdAuthClientUuid;
- /**
- * 获取用户名 api
- */
- private static final String API_URL_GET_USERNAME = "/api/ps/token/getUserByToken";
- /**
- * 获取分部 api
- */
- private static final String API_URL_GET_SUBCOMPANY = "/api/hrm/resful/getHrmsubcompanyWithPage";
- /**
- * 获取部门 api
- */
- private static final String API_URL_GET_DEPARTMENT= "/api/hrm/resful/getHrmdepartmentWithPage";
- /**
- * 获取岗位 api
- */
- private static final String API_URL_GET_JOB= "/api/hrm/resful/getJobtitleInfoWithPage";
- /**
- * 获取人员信息 api
- */
- private static final String API_URL_GET_USERINFO= "/api/hrm/resful/getHrmUserInfoWithPage";
- @Autowired
- private CdCasService cdCasService;
- /**
- * 单点登录
- * @param ticket
- * @param request
- * @return
- * @throws Exception
- */
- @RequestMapping(value = "/validateTicketKey", method = RequestMethod.GET)
- public BaseResponse<Object> code(@RequestParam(value = "ticket", required = false) String ticket, HttpServletRequest request) {
- logger.debug("sso token ticket:"+ ticket);
- // request params
- Map<String, String> paramsMap = new HashMap<>(8);
- paramsMap.put("token", ticket);
- // 第一步 获取用户名
- logger.debug("1 start");
- String username = null;
- try {
- String httpGetResponse = HttpClientUtils.simpleGetInvoke(cdAuthBaseUrl.concat(API_URL_GET_USERNAME), paramsMap);
- logger.debug("sso username httpGetResponse-------------------------" + httpGetResponse + "-------------------------");
- if(null == httpGetResponse || "false".equals(httpGetResponse)){
- // 获取用户名 失败 说明登录凭证 无效
- logger.info("sso login false");
- return buildFailResponse("无法获取用户信息");
- }
- JSONObject jsonObjectResp = JSONObject.parseObject(httpGetResponse);
- Object obj = jsonObjectResp.get("username");
- if(null == obj || "" == String.valueOf(obj)){
- // 获取用户名 失败 说明登录凭证 无效
- logger.info("sso ticket error");
- return buildFailResponse("获取用户信息为空");
- }
- username = obj.toString();
- logger.debug("username-------------------------" + username + "-------------------------");
- // 真实姓名
- String realName = jsonObjectResp.getString("lastname");
- // 移动电话
- String phoneNoMobile = jsonObjectResp.getString("mobile");
- // 分部名称(处室)
- String subcompanyname = jsonObjectResp.getString("subcompanyname");
- // 部门名称
- String departmentname = jsonObjectResp.getString("departmentname");
- if( StringUtils.isNotEmpty(phoneNoMobile) && StringUtils.isNotEmpty(realName) && (Pattern.matches(REGEX_PHONE,username) || Pattern.matches(REGEX_PHONE,phoneNoMobile) ) ){
- Map<String,Object> ssoUserMap = new HashMap<>(8) ;
- // 登录名
- ssoUserMap.put("loginName",username);
- // 真实姓名
- ssoUserMap.put("userName",realName);
- if( Pattern.matches(REGEX_PHONE,username) ){
- // 登录名 格式为手机号码
- ssoUserMap.put("phone",username);
- }else{
- ssoUserMap.put("phone",phoneNoMobile);
- }
- // 分部名称(处室)
- ssoUserMap.put("subcompanyname",subcompanyname);
- // 部门名称
- ssoUserMap.put("departmentname",departmentname);
- logger.info("sso user validate");
- BisInspAllRlationPers bisInspAllRlationPers = cdCasService.validateUser(ssoUserMap);
- if (bisInspAllRlationPers == null) {
- return buildFailResponse(1001, "登陆名或密码错误", "", "");
- } else {
- logger.info("---------------sso user null -------------------------------");
- String uuid = UuidUtil.uuid();
- bisInspAllRlationPers = cdCasService.validateTicket(bisInspAllRlationPers, uuid, request);
- logger.info("---------------sso add -------------------------------");
- return buildSuccessResponse(bisInspAllRlationPers, uuid);
- }
- }else{
- return buildFailResponse("api获取用户信息为空");
- }
- } catch (Exception e) {
- return buildFailResponse("api获取用户信息失败");
- }
- }
- /**
- * 手机号 正则表达式
- */
- private static final String REGEX_PHONE = "^1[3|4|5|6|7|8|9][0-9]{9}$";
- /**
- *
- * @return
- */
- private Map<String,String> getRequestToken() {
- // 时间戳毫秒数
- long l = System.currentTimeMillis() ;
- String code = cdAuthClientUuid.concat(Long.toString(l)) ;
- try {
- String md5key = DigestUtils.md5DigestAsHex(code.getBytes("utf-8")).toUpperCase() ;
- //md5加密 然后转大写
- Map<String,String> map = new HashMap<>() ;
- map.put("key",md5key) ;
- map.put("ts",Long.toString(l)) ;
- return map;
- } catch (UnsupportedEncodingException e) {
- logger.error("生成请求token异常", e);
- }
- return null ;
- }
- }
|