| | |
| | | package com.dy.sso.busi; |
| | | |
| | | 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.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; |
| | |
| | | //@Autowired |
| | | //private CacheManager cacheManager ; |
| | | |
| | | |
| | | @Autowired |
| | | public void setSv(SsoSv sv ){ |
| | | this.sv = sv ; |
| | |
| | | /** |
| | | * 客户端请求用户登录,客户端提交Json数据 |
| | | * @param vo 用户登录值对象 |
| | | * @param bindingResult 输入验证 |
| | | * @return 登录用户值对象 |
| | | */ |
| | | @Operation(summary = "单点登录", description = "提交登录用户值对象(json格式),进行单点登录") |
| | |
| | | if(bindingResult != null && bindingResult.hasErrors()){ |
| | | return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); |
| | | } |
| | | if(vo.orgTag == null || vo.orgTag.trim().length() == 0){ |
| | | return BaseResponseUtils.buildFail("未选择组织单位"); |
| | | } |
| | | //把组织单位标签作为数据源名称 |
| | | DataSourceContext.set(vo.orgTag); |
| | | |
| | | String uuid ; |
| | | BaUser userPo ; |
| | | try { |
| | | //Boolean flag = cacheManager.getCacheNames().isEmpty() ; |
| | | uuid = UUID.randomUUID().toString(); |
| | | userPo = this.sv.login(uuid, vo.phone, vo.password); |
| | | 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); |
| | | return BaseResponseUtils.buildException(e.getMessage()) ; |
| | |
| | | if(bindingResult != null && bindingResult.hasErrors()){ |
| | | return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); |
| | | } |
| | | if(vo.orgTag == null || vo.orgTag.trim().length() == 0){ |
| | | return BaseResponseUtils.buildFail("未选择组织单位"); |
| | | } |
| | | //把组织单位标签作为数据源名称 |
| | | DataSourceContext.set(vo.orgTag); |
| | | |
| | | String uuid ; |
| | | BaUser userPo ; |
| | | try { |
| | | //Boolean flag = cacheManager.getCacheNames().isEmpty() ; |
| | | uuid = UUID.randomUUID().toString(); |
| | | userPo = this.sv.login(uuid, vo.phone, vo.password); |
| | | 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); |
| | | return BaseResponseUtils.buildException(e.getMessage()) ; |
| | |
| | | * 此方法供子模块系统调用,所以不公开在API接口中 |
| | | * 方法功能:验证是否已经登录,如果登录了,再验证权限 |
| | | * @param token 登录用户token |
| | | * @param power 验证一个权限 |
| | | * @param allPower 验证所有权限 |
| | | * @param anyPower 验证任何一个权限 |
| | | * @param privilege 验证一个权限 |
| | | * @param allPrivilege 验证所有权限 |
| | | * @param anyPrivilege 验证任何一个权限 |
| | | * @return SsoVo |
| | | */ |
| | | @Hidden |
| | | @GetMapping(path = "ssoCheck") |
| | | public SsoVo ssoCheck(String token, String power, String[] allPower, String[] anyPower){ |
| | | public SsoVo ssoCheck(String token, String privilege, String[] allPrivilege, String[] anyPrivilege){ |
| | | BaUser userPo = this.sv.getByUuid(token) ; |
| | | SsoVo vo = new SsoVo(); |
| | | if(userPo != null){ |
| | | vo.dataSourceName = userPo.orgTag ; |
| | | vo.logined = true ; |
| | | vo.hasPower = true ; |
| | | vo.dataSourceName = userPo.getOrgTag() ; |
| | | vo.hasPower = false ;//默认是无权限 |
| | | if(userPo.supperAdmin != null && userPo.supperAdmin == 1){ |
| | | vo.hasPower = true ; |
| | | }else{ |
| | | 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.hasAllPrivilege(allPrivilege, userPo) ; |
| | | if(!vo.hasPower){ |
| | | vo.hasPower = this.hasAnyPrivilege(anyPrivilege, userPo) ; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }else{ |
| | | vo.logined = false ; |
| | | vo.hasPower = false ; |
| | |
| | | return vo ; |
| | | } |
| | | |
| | | ///////////////////////////////////////////////////////////////// |
| | | // |
| | | // 以下私有方法 |
| | | // |
| | | ///////////////////////////////////////////////////////////////// |
| | | /** |
| | | * 判断登录用户是否拥有指定的一个权限 |
| | | * @param privilege 指定的一个权限 |
| | | * @param userPo 当前登录用户 |
| | | * @return 是否有权限 |
| | | */ |
| | | private boolean hasOnePrivilege(String privilege, BaUser userPo){ |
| | | boolean hasPrivilege = false ; |
| | | if (privilege != null && !privilege.trim().equals("")) { |
| | | int intPri = Integer.parseInt(privilege); |
| | | for (Integer pri : userPo.privileges) { |
| | | if (pri == intPri) { |
| | | hasPrivilege = true; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | return hasPrivilege ; |
| | | } |
| | | |
| | | /** |
| | | * 判断登录用户是否拥有指定的多个权限 |
| | | * @param allPrivilege 指定的多个权限 |
| | | * @param userPo 当前登录用户 |
| | | * @return 是否有权限 |
| | | */ |
| | | private boolean hasAllPrivilege(String[] allPrivilege, BaUser userPo){ |
| | | boolean hasPrivilege = false ; |
| | | if(allPrivilege != null && allPrivilege.length > 0){ |
| | | int intPri ; |
| | | int count = 0 ; |
| | | for(String strPri : allPrivilege){ |
| | | intPri = Integer.parseInt(strPri) ; |
| | | for(Integer pri : userPo.privileges){ |
| | | if(pri == intPri){ |
| | | count++ ; |
| | | break ; |
| | | } |
| | | } |
| | | } |
| | | if(count == allPrivilege.length){ |
| | | hasPrivilege = true ; |
| | | } |
| | | } |
| | | return hasPrivilege ; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 判断登录用户是否拥有指定的某个权限 |
| | | * @param anyPrivilege 指定的多个权限 |
| | | * @param userPo 当前登录用户 |
| | | * @return 是否有权限 |
| | | */ |
| | | private boolean hasAnyPrivilege(String[] anyPrivilege, BaUser userPo){ |
| | | boolean hasPrivilege = false ; |
| | | int intPri ; |
| | | if(anyPrivilege != null && anyPrivilege.length > 0){ |
| | | for(String strPri : anyPrivilege){ |
| | | intPri = Integer.parseInt(strPri) ; |
| | | for(Integer pri : userPo.privileges){ |
| | | if(pri == intPri){ |
| | | hasPrivilege = true ; |
| | | break ; |
| | | } |
| | | } |
| | | if(hasPrivilege){ |
| | | break ; |
| | | } |
| | | } |
| | | } |
| | | return hasPrivilege ; |
| | | } |
| | | |
| | | |
| | | } |