c4ec943b514942ac975b0ed11a261493eb858573.svn-base 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package org.jeecg.modules.cas.controller;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import cn.hutool.crypto.SecureUtil;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.jeecg.common.api.vo.Result;
  8. import org.jeecg.common.constant.CacheConstant;
  9. import org.jeecg.common.constant.CommonConstant;
  10. import org.jeecg.common.system.util.JwtUtil;
  11. import org.jeecg.common.system.vo.LoginUser;
  12. import org.jeecg.common.util.RedisUtil;
  13. import org.jeecg.modules.cas.util.CASServiceUtil;
  14. import org.jeecg.modules.cas.util.XmlUtils;
  15. import org.jeecg.modules.system.entity.SysDepart;
  16. import org.jeecg.modules.system.entity.SysUser;
  17. import org.jeecg.modules.system.service.ISysDepartService;
  18. import org.jeecg.modules.system.service.ISysUserService;
  19. import org.springframework.beans.BeanUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.http.HttpEntity;
  23. import org.springframework.web.bind.annotation.GetMapping;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RequestParam;
  26. import org.springframework.web.bind.annotation.RestController;
  27. import com.alibaba.fastjson.JSONObject;
  28. import lombok.extern.slf4j.Slf4j;
  29. /**
  30. * <p>
  31. * CAS单点登录客户端登录认证
  32. * </p>
  33. *
  34. * @Author zhoujf
  35. * @since 2018-12-20
  36. */
  37. @Slf4j
  38. @RestController
  39. @RequestMapping("/sys/cas/client")
  40. public class CasClientController {
  41. @Autowired
  42. private ISysUserService sysUserService;
  43. @Autowired
  44. private ISysDepartService sysDepartService;
  45. @Autowired
  46. private RedisUtil redisUtil;
  47. @Value("${cas.prefixUrl}")
  48. private String prefixUrl;
  49. @GetMapping("/validateLogin")
  50. public Object validateLogin(@RequestParam(name="ticket") String ticket,
  51. @RequestParam(name="service") String service,
  52. HttpServletRequest request,
  53. HttpServletResponse response) throws Exception {
  54. Result<JSONObject> result = new Result<JSONObject>();
  55. log.info("Rest api login.");
  56. try {
  57. String validateUrl = prefixUrl+"/p3/serviceValidate";
  58. String res = CASServiceUtil.getSTValidate(validateUrl, ticket, service);
  59. log.info("res."+res);
  60. final String error = XmlUtils.getTextForElement(res, "authenticationFailure");
  61. if(StringUtils.isNotEmpty(error)) {
  62. throw new Exception(error);
  63. }
  64. final String principal = XmlUtils.getTextForElement(res, "user");
  65. if (StringUtils.isEmpty(principal)) {
  66. throw new Exception("No principal was found in the response from the CAS server.");
  67. }
  68. log.info("-------token----username---"+principal);
  69. //1. 校验用户是否有效
  70. SysUser sysUser = sysUserService.getUserByName(principal);
  71. result = sysUserService.checkUserIsEffective(sysUser);
  72. if(!result.isSuccess()) {
  73. return result;
  74. }
  75. String token = JwtUtil.sign(sysUser.getUsername(), sysUser.getPassword());
  76. // 设置超时时间
  77. redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
  78. redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
  79. //获取用户部门信息
  80. JSONObject obj = new JSONObject();
  81. List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
  82. obj.put("departs", departs);
  83. if (departs == null || departs.size() == 0) {
  84. obj.put("multi_depart", 0);
  85. } else if (departs.size() == 1) {
  86. sysUserService.updateUserDepart(principal, departs.get(0).getOrgCode());
  87. obj.put("multi_depart", 1);
  88. } else {
  89. obj.put("multi_depart", 2);
  90. }
  91. obj.put("token", token);
  92. obj.put("userInfo", sysUser);
  93. result.setResult(obj);
  94. result.success("登录成功");
  95. } catch (Exception e) {
  96. //e.printStackTrace();
  97. result.error500(e.getMessage());
  98. }
  99. return new HttpEntity<>(result);
  100. }
  101. }