|  |  | 
 |  |  |  | 
 |  |  | import com.dy.common.aop.SsoVo; | 
 |  |  | import com.dy.common.multiDataSource.DataSourceContext; | 
 |  |  | import com.dy.common.util.MD5; | 
 |  |  | import com.dy.common.webUtil.BaseResponse; | 
 |  |  | import com.dy.common.webUtil.BaseResponseUtils; | 
 |  |  | import com.dy.common.webUtil.ResultCodeMsg; | 
 |  |  | import com.dy.pipIrrGlobal.pojoBa.BaUser; | 
 |  |  | import com.dy.pipIrrGlobal.util.Org; | 
 |  |  | import com.mysql.cj.util.StringUtils; | 
 |  |  | import io.swagger.v3.oas.annotations.Hidden; | 
 |  |  | import io.swagger.v3.oas.annotations.Operation; | 
 |  |  | import io.swagger.v3.oas.annotations.Parameter; | 
 |  |  | 
 |  |  | import jakarta.validation.Valid; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | //import org.springframework.cache.CacheManager; | 
 |  |  | import org.springframework.http.MediaType; | 
 |  |  | import org.springframework.validation.BindingResult; | 
 |  |  | import org.springframework.web.bind.annotation.*; | 
 |  |  |  | 
 |  |  | import java.util.Objects; | 
 |  |  | import java.util.UUID; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 注解Tag 在API中显示: Tag 注解, 给整个接口起了个名字与描述" | 
 |  |  | 
 |  |  | @RequestMapping(path="sso") | 
 |  |  | @SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked") | 
 |  |  | public class SsoCtrl { | 
 |  |  |     //万用token | 
 |  |  |     private static final String UniversalUserToken = "0000-0000-1234-9876-5"; | 
 |  |  |  | 
 |  |  |     //在属性上注解@Autowired时,会警告 Field injection is not recommended(不再推荐使用字段注入) | 
 |  |  |     private SsoSv sv ; | 
 |  |  |  | 
 |  |  |     //private KaptchaConfig kaptchaConfig; | 
 |  |  |     //@Autowired | 
 |  |  |     //private CacheManager cacheManager ; | 
 |  |  |  | 
 |  |  | 
 |  |  |         this.sv = sv ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //@Autowired | 
 |  |  |     //public void setKaptchaConfig(KaptchaConfig kaptchaConfig) { | 
 |  |  |     //    this.kaptchaConfig = kaptchaConfig; | 
 |  |  |     //} | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 客户端请求得到所有组织机构 | 
 |  |  |      * @return 所有组织机构数据 | 
 |  |  |      */ | 
 |  |  |     @Operation(summary = "所有组织机构", description = "返回所有所有组织机构数据") | 
 |  |  |     @ApiResponses(value = { | 
 |  |  |             @ApiResponse( | 
 |  |  |                     responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, | 
 |  |  |                     description = "返回所有组织机构数据(BaseResponse.content:[ { \"tag\":\"ym\", \"name\":\"元谋\" }, { \"tag\":\"片角\", \"name\":\"片角镇\" }])", | 
 |  |  |                     content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, | 
 |  |  |                             schema = @Schema(implementation = Org.class))} | 
 |  |  |             ) | 
 |  |  |     }) | 
 |  |  |     @GetMapping(path = "allOrg") | 
 |  |  |     public BaseResponse<List<Org.OrgVo>> allOrg(){ | 
 |  |  |         //List<Org> list = Arrays.asList(Org.Ym, Org.Pj) ; | 
 |  |  |         return BaseResponseUtils.buildSuccess(Org.OrgList); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 客户端请求用户登录,客户端提交Json数据 | 
 |  |  | 
 |  |  |             ) | 
 |  |  |     }) | 
 |  |  |     @PostMapping(path = "loginJson", consumes = MediaType.APPLICATION_JSON_VALUE) | 
 |  |  |     public BaseResponse<UserVo> loginJson(@Valid @RequestBody LoginVo vo,  @Parameter(hidden = true) BindingResult bindingResult) { | 
 |  |  |     public BaseResponse<UserVo> loginJson(HttpServletRequest request, @RequestBody @Parameter(description = "登录json数据", required = true) @Valid LoginVo vo,  @Parameter(hidden = true) BindingResult bindingResult) { | 
 |  |  |         if(bindingResult != null && bindingResult.hasErrors()){ | 
 |  |  |             return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  |         } | 
 |  |  |         if(!vo.phone.equals("admin")){ | 
 |  |  |             if(vo.phone.length() != 11){ | 
 |  |  |                 return BaseResponseUtils.buildErrorMsg("手机号(长度不是11位)不正确"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         if(vo.orgTag == null || vo.orgTag.trim().length() == 0){ | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("未选择组织单位"); | 
 |  |  |         } | 
 |  |  |         //把组织单位标签作为数据源名称 | 
 |  |  |         DataSourceContext.set(vo.orgTag); | 
 |  |  |  | 
 |  |  |         if(vo.token != null && vo.token.trim().length() > 0) { | 
 |  |  |             // 从session中获取验证码 | 
 |  |  |             //HttpSession session = (HttpSession) request.getSession(); | 
 |  |  |             //String localCode = session.getAttribute(token).toString(); | 
 |  |  |             // 从数据库获取验证码 | 
 |  |  |             Map map = sv.getCodeByToken(vo.token); | 
 |  |  |             Long expiration = Long.parseLong(map.get("expiration").toString()); | 
 |  |  |             Long currentTimestamp = System.currentTimeMillis(); | 
 |  |  |             if(currentTimestamp > expiration) { | 
 |  |  |                 return BaseResponseUtils.buildErrorMsg("验证码已超时"); | 
 |  |  |             } | 
 |  |  |             String localCode = map.get("code").toString(); | 
 |  |  |             if(!vo.code.equals(localCode)) { | 
 |  |  |                 return BaseResponseUtils.buildErrorMsg("验证码错误"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         String uuid ; | 
 |  |  |         BaUser userPo ; | 
 |  |  |         if(!sv.existPhone(vo.phone)){ | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("账号不存在"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         String uuid4Token = null; | 
 |  |  |         BaUser userPo = null ; | 
 |  |  |         try { | 
 |  |  |             //Boolean flag = cacheManager.getCacheNames().isEmpty() ; | 
 |  |  |             uuid = UUID.randomUUID().toString(); | 
 |  |  |             userPo = this.sv.loginWithMapperXml(uuid, vo.phone, vo.password); | 
 |  |  |             uuid4Token = UUID.randomUUID().toString(); | 
 |  |  |             if(!StringUtils.isNullOrEmpty(vo.password)){ | 
 |  |  |                 /* | 
 |  |  |                 如果前端进行了base64加密 | 
 |  |  |                 po.password = new String(Base64.getDecoder().decode(po.password)) ; | 
 |  |  |                 */ | 
 |  |  |                 vo.password = MD5.encrypt(vo.password) ; | 
 |  |  |             } | 
 |  |  |             userPo = this.sv.loginWithMapperXml(uuid4Token, vo.phone, vo.password); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("用户登录异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()) ; | 
 |  |  | 
 |  |  |  | 
 |  |  |         if(userPo != null){ | 
 |  |  |             UserVo uVo = UserVoMapper.INSTANCT.po2vo(userPo); | 
 |  |  |             uVo.token = uuid ; | 
 |  |  |             uVo.token = uuid4Token ; | 
 |  |  |             return BaseResponseUtils.buildSuccess(uVo); | 
 |  |  |         }else{ | 
 |  |  |             return BaseResponseUtils.buildFail("登录失败"); | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("登录失败"); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |     @PostMapping(path = "loginForm", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) | 
 |  |  |     public BaseResponse<UserVo> loginForm(@Parameter(description = "form表单数据", required = true) @Valid LoginVo vo,  @Parameter(hidden = true) BindingResult bindingResult){ | 
 |  |  |         if(bindingResult != null && bindingResult.hasErrors()){ | 
 |  |  |             return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  |         } | 
 |  |  |         if(vo.orgTag == null || vo.orgTag.trim().length() == 0){ | 
 |  |  |             return BaseResponseUtils.buildFail("未选择组织单位"); | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("未选择组织单位"); | 
 |  |  |         } | 
 |  |  |         //把组织单位标签作为数据源名称 | 
 |  |  |         DataSourceContext.set(vo.orgTag); | 
 |  |  | 
 |  |  |         try { | 
 |  |  |             //Boolean flag = cacheManager.getCacheNames().isEmpty() ; | 
 |  |  |             uuid = UUID.randomUUID().toString(); | 
 |  |  |             if(!StringUtils.isNullOrEmpty(vo.password)){ | 
 |  |  |                 /* | 
 |  |  |                 如果前端进行了base64加密 | 
 |  |  |                 po.password = new String(Base64.getDecoder().decode(po.password)) ; | 
 |  |  |                 */ | 
 |  |  |                 vo.password = MD5.encrypt(vo.password) ; | 
 |  |  |             } | 
 |  |  |             userPo = this.sv.loginWithMapperXml(uuid, vo.phone, vo.password); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("用户登录异常", e); | 
 |  |  | 
 |  |  |             uVo.token = uuid ; | 
 |  |  |             return BaseResponseUtils.buildSuccess(uVo); | 
 |  |  |         }else{ | 
 |  |  |             return BaseResponseUtils.buildFail("登录失败"); | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("登录失败"); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |             this.sv.logout(token) ; | 
 |  |  |             return BaseResponseUtils.buildSuccess(true); | 
 |  |  |         }else{ | 
 |  |  |             return BaseResponseUtils.buildFail("未从header中得到token"); | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("未从header中得到token"); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |         BaUser userPo = this.sv.getByUuid(token) ; | 
 |  |  |         return userPo == null ? null : userPo.id ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 此方法供子模块系统调用,所以不公开在API接口中 | 
 |  |  |      * 方法功能:验证是否已经登录 | 
 |  |  |      * @param token 登录用户token | 
 |  |  |      * @return SsoVo | 
 |  |  |      */ | 
 |  |  |     @Hidden | 
 |  |  |     @GetMapping(path = "ssoCheck") | 
 |  |  |     public SsoVo ssoCheck(String token){ | 
 |  |  |         BaUser userPo = null ; | 
 |  |  |         if(token.equals(UniversalUserToken)){ | 
 |  |  |             //调试阶段,用的万用token | 
 |  |  |             userPo = new BaUser() ; | 
 |  |  |             Org.OrgVo orgVo = Org.OrgList.get(0) ; | 
 |  |  |             userPo.orgTag = orgVo.tag ; | 
 |  |  |         }else{ | 
 |  |  |             userPo = this.sv.getByUuid(token) ; | 
 |  |  |         } | 
 |  |  |         SsoVo vo = new SsoVo(); | 
 |  |  |         if(userPo != null){ | 
 |  |  |             vo.dataSourceName = userPo.orgTag ; | 
 |  |  |             vo.logined = true ; | 
 |  |  |             vo.hasPower = true ;//默认有权限。2023-12-21 经商议,由前端鉴权 | 
 |  |  |         }else{ | 
 |  |  |             vo.logined = false ; | 
 |  |  |             vo.hasPower = true ;//默认有权限。2023-12-21 经商议,由前端鉴权 | 
 |  |  |         } | 
 |  |  |         return vo ; | 
 |  |  |     } | 
 |  |  |     /** | 
 |  |  |      * 此方法供子模块系统调用,所以不公开在API接口中 | 
 |  |  |      * 方法功能:验证是否已经登录,如果登录了,再验证权限 | 
 |  |  | 
 |  |  |      * @return SsoVo | 
 |  |  |      */ | 
 |  |  |     @Hidden | 
 |  |  |     @GetMapping(path = "ssoCheck") | 
 |  |  |     public SsoVo ssoCheck(String token, String privilege, String[] allPrivilege, String[] anyPrivilege){ | 
 |  |  |     @GetMapping(path = "ssoPowerCheck") | 
 |  |  |     public SsoVo ssoPowerCheck(String token, String privilege, String[] allPrivilege, String[] anyPrivilege){ | 
 |  |  |         BaUser userPo = this.sv.getByUuid(token) ; | 
 |  |  |         SsoVo vo = new SsoVo(); | 
 |  |  |         if(userPo != null){ | 
 |  |  | 
 |  |  |             if(userPo.supperAdmin != null && userPo.supperAdmin == 1){ | 
 |  |  |                 vo.hasPower = true ; | 
 |  |  |             }else{ | 
 |  |  |                 if(userPo.privileges != null && userPo.privileges.size() > 0){ | 
 |  |  |                     vo.hasPower = this.hasOnePrivilege(privilege, userPo) ; | 
 |  |  |                     if(!vo.hasPower){ | 
 |  |  |                         vo.hasPower = this.hasAllPrivilege(allPrivilege, userPo) ; | 
 |  |  |                 if(privilege.equals("-1")){ | 
 |  |  |                     //无需权限验证 | 
 |  |  |                     vo.hasPower = true ; | 
 |  |  |                 }else{ | 
 |  |  |                     if(userPo.privileges != null && userPo.privileges.size() > 0){ | 
 |  |  |                         vo.hasPower = this.hasOnePrivilege(privilege, userPo) ; | 
 |  |  |                         if(!vo.hasPower){ | 
 |  |  |                             vo.hasPower = this.hasAnyPrivilege(anyPrivilege, userPo) ; | 
 |  |  |                             vo.hasPower = this.hasAllPrivilege(allPrivilege, userPo) ; | 
 |  |  |                             if(!vo.hasPower){ | 
 |  |  |                                 vo.hasPower = this.hasAnyPrivilege(anyPrivilege, userPo) ; | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  | 
 |  |  |         return vo ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获得当前登录用户 | 
 |  |  |      * @param token 登录用户token | 
 |  |  |      * @return SsoVo | 
 |  |  |      */ | 
 |  |  |     @Hidden | 
 |  |  |     @GetMapping(path = "ssoCurUser") | 
 |  |  |     public CurUserVo ssoCurUser(String token){ | 
 |  |  |         BaUser userPo = this.sv.getByUuid(token) ; | 
 |  |  |         CurUserVo vo = new CurUserVo(); | 
 |  |  |         if(userPo != null){ | 
 |  |  |             vo.id = userPo.id ; | 
 |  |  |             vo.name = userPo.userName; | 
 |  |  |         } | 
 |  |  |         return vo ; | 
 |  |  |     } | 
 |  |  |     ///////////////////////////////////////////////////////////////// | 
 |  |  |     // | 
 |  |  |     // 以下私有方法 |