From d2263e86d837ef331d77fd39dccd79f100041e76 Mon Sep 17 00:00:00 2001 From: zhubaomin <zhubaomin> Date: 星期一, 14 四月 2025 16:28:49 +0800 Subject: [PATCH] 获取轮灌组时,项目ID不为必须条件 --- pipIrr-platform/pipIrr-web/pipIrr-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java | 423 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 423 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java new file mode 100644 index 0000000..2e349e8 --- /dev/null +++ b/pipIrr-platform/pipIrr-web/pipIrr-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java @@ -0,0 +1,423 @@ +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.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 io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.validation.Valid; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +import java.util.*; + +/** + * 娉ㄨВTag 鍦ˋPI涓樉绀猴細 Tag 娉ㄨВ, 缁欐暣涓帴鍙h捣浜嗕釜鍚嶅瓧涓庢弿杩�" + * 娉ㄨВApiResponses 鍜� 娉ㄨВApiResponse 鐢ㄦ潵閰嶇疆鍝嶅簲锛� + * 娉ㄨВOperation 鐢ㄦ潵璁剧疆鎺ュ彛鍚嶇О鍜屾弿杩帮紱 + * 娉ㄨВParameter 鐢ㄦ潵璁剧疆璇锋眰鍙傛暟鐨勬弿杩般�佹槸鍚﹀繀濉拰绀轰緥銆� + */ +@Slf4j +@Tag(name = "鐢ㄦ埛鐧诲綍", description = "鍗曠偣鐧诲綍绯荤粺锛坰so锛�") +@RestController +@RequestMapping(path="sso") +@SuppressWarnings("unchecked")//java鐗堟湰瓒婇珮锛屽娉涘瀷绾︽潫瓒婁弗锛屾墍浠ラ厤缃甋uppressWarnings("unchecked") +public class SsoCtrl { + + //鍦ㄥ睘鎬т笂娉ㄨВ@Autowired鏃讹紝浼氳鍛� Field injection is not recommended锛堜笉鍐嶆帹鑽愪娇鐢ㄥ瓧娈垫敞鍏ワ級 + private SsoSv sv ; + + //private KaptchaConfig kaptchaConfig; + //@Autowired + //private CacheManager cacheManager ; + + @Autowired + public void setSv(SsoSv sv ){ + 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); + } + + /** + * 瀹㈡埛绔姹傜敤鎴风櫥褰曪紝瀹㈡埛绔彁浜son鏁版嵁 + * @param vo 鐢ㄦ埛鐧诲綍鍊煎璞� + * @param bindingResult 杈撳叆楠岃瘉 + * @return 鐧诲綍鐢ㄦ埛鍊煎璞� + */ + @Operation(summary = "鍗曠偣鐧诲綍", description = "鎻愪氦鐧诲綍鐢ㄦ埛鍊煎璞★紙json鏍煎紡锛夛紝杩涜鍗曠偣鐧诲綍") + /* + //涓嬮潰杩欎釜涓嶈捣浣滅敤锛岄�氳繃@RequestBody=鐩存帴鏄剧ずLoginVo鐨凙PI + @io.swagger.v3.oas.annotations.parameters.RequestBody( + //required = true, + description = "form鍊煎璞�", + content = {@Content(mediaType = "application/json", + schema = @Schema(implementation = LoginVo.class))} + ) + */ + @ApiResponses(value = { + @ApiResponse( + responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, + description = "杩斿洖鐧诲綍鐢ㄦ埛鍊煎璞★紙鏁版嵁鍩虹被鐨刢ontent锛�", + content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, + schema = @Schema(implementation = UserVo.class))} + ) + }) + @PostMapping(path = "loginJson", consumes = MediaType.APPLICATION_JSON_VALUE) + 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.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); + + String token = Optional.ofNullable(vo.getToken()).orElse(""); + String code = Optional.ofNullable(vo.getCode()).orElse(""); + + if(token.length() > 0 && code.length() > 0) { + // 浠巗ession涓幏鍙栭獙璇佺爜 + //HttpSession session = (HttpSession) request.getSession(); + //String localCode = session.getAttribute(token).toString(); + + Map map = sv.getCodeByToken(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(!code.equals(localCode)) { + return BaseResponseUtils.buildErrorMsg("楠岃瘉鐮侀敊璇�"); + } + } + + //寰楀埌鎵�鏈夌敤鎴疯处鍙� + List<String> phones = sv.getPhones(); + if(!phones.contains(vo.phone)){ + return BaseResponseUtils.buildErrorMsg("璐﹀彿涓嶅瓨鍦�"); + } + + String uuid ; + BaUser userPo ; + try { + //Boolean flag = cacheManager.getCacheNames().isEmpty() ; + uuid = UUID.randomUUID().toString(); + if(!StringUtils.isNullOrEmpty(vo.password)){ + /* + 濡傛灉鍓嶇杩涜浜哹ase64鍔犲瘑 + 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(userPo != null){ + UserVo uVo = UserVoMapper.INSTANCT.po2vo(userPo); + uVo.token = uuid ; + return BaseResponseUtils.buildSuccess(uVo); + }else{ + return BaseResponseUtils.buildErrorMsg("瀵嗙爜閿欒"); + } + } + + /** + * 瀹㈡埛绔姹傜敤鎴风櫥褰曪紝瀹㈡埛绔彁浜orm琛ㄥ崟 + * @param vo 鐧诲綍鐢ㄦ埛form琛ㄥ崟瀵硅薄 + * @return 鐧诲綍鐢ㄦ埛鍊煎璞� + */ + @Operation(summary = "鍗曠偣鐧诲綍", description = "鎻愪氦鐧诲綍鐢ㄦ埛鏁版嵁锛坒orm琛ㄥ崟锛夛紝杩涜鍗曠偣鐧诲綍") + @ApiResponses(value = { + @ApiResponse( + responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, + description = "杩斿洖鐧诲綍鐢ㄦ埛鍊煎璞★紙鏁版嵁鍩虹被鐨刢ontent锛�", + content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, + schema = @Schema(implementation = UserVo.class))} + ) + }) + @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.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); + } + if(vo.orgTag == null || vo.orgTag.trim().length() == 0){ + return BaseResponseUtils.buildErrorMsg("鏈�夋嫨缁勭粐鍗曚綅"); + } + //鎶婄粍缁囧崟浣嶆爣绛句綔涓烘暟鎹簮鍚嶇О + DataSourceContext.set(vo.orgTag); + + String uuid ; + BaUser userPo ; + try { + //Boolean flag = cacheManager.getCacheNames().isEmpty() ; + uuid = UUID.randomUUID().toString(); + if(!StringUtils.isNullOrEmpty(vo.password)){ + /* + 濡傛灉鍓嶇杩涜浜哹ase64鍔犲瘑 + 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(userPo != null){ + UserVo uVo = UserVoMapper.INSTANCT.po2vo(userPo); + uVo.token = uuid ; + return BaseResponseUtils.buildSuccess(uVo); + }else{ + return BaseResponseUtils.buildErrorMsg("鐧诲綍澶辫触"); + } + } + + /** + * 閫氳繃UUID閫�鍑虹櫥褰曪紝鍥犱负鍙傛暟鏄痷uid锛屾墍浠ユ璋冪敤蹇呴』鏄悗绔浉鍏充唬鐮佽皟鐢紝鍥犱负鍓嶇寰椾笉鍒癱ookie涓殑uuid + * @param hr HttpServletRequest + * @return 姝e父閫�鍑虹櫥褰曡繑鍥瀟rue锛屽惁鍒欒繑鍥瀎alse + */ + @Operation(summary = "鍗曠偣鐧诲嚭", description = "鎻愪氦token(鍦╤eader涓�)锛岃繘琛屽崟鐐圭櫥鍑�") + @ApiResponses(value = { + @ApiResponse( + responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, + description = "杩斿洖澶勭悊缁撴灉锛堟垚鍔焧rue锛屽け璐alse锛夛紙鏁版嵁鍩虹被鐨刢ontent锛�", + content = {@Content(mediaType = MediaType.TEXT_PLAIN_VALUE, + schema = @Schema(implementation = Boolean.class))} + ) + }) + @GetMapping(path = "logout") + public BaseResponse<Boolean> logout(@Parameter(hidden = true) HttpServletRequest hr){ + String token = hr.getHeader("token") ; + if(token != null){ + this.sv.logout(token) ; + return BaseResponseUtils.buildSuccess(true); + }else{ + return BaseResponseUtils.buildErrorMsg("鏈粠header涓緱鍒皌oken"); + } + } + + /** + * 姝ゆ柟娉曚緵瀛愭ā鍧楃郴缁熻皟鐢紝鎵�浠ヤ笉鍏紑鍦ˋPI鎺ュ彛涓� + * 鏂规硶鍔熻兘锛氬緱鍒扮櫥褰曠敤鎴穒d锛屽惁鍒欒繑鍥瀗ull + * @param token 鐧诲綍鐢ㄦ埛token + * @return 鐧诲綍鐢ㄦ埛ID + */ + @Hidden + @GetMapping(path = "loginUserId") + public Long loginUserId(String token){ + BaUser userPo = this.sv.getByUuid(token) ; + return userPo == null ? null : userPo.id ; + } + /** + * 姝ゆ柟娉曚緵瀛愭ā鍧楃郴缁熻皟鐢紝鎵�浠ヤ笉鍏紑鍦ˋPI鎺ュ彛涓� + * 鏂规硶鍔熻兘锛氶獙璇佹槸鍚﹀凡缁忕櫥褰� + * @param token 鐧诲綍鐢ㄦ埛token + * @return SsoVo + */ + @Hidden + @GetMapping(path = "ssoCheck") + public SsoVo ssoCheck(String token){ + BaUser 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 ; + } + /** + * 姝ゆ柟娉曚緵瀛愭ā鍧楃郴缁熻皟鐢紝鎵�浠ヤ笉鍏紑鍦ˋPI鎺ュ彛涓� + * 鏂规硶鍔熻兘锛氶獙璇佹槸鍚﹀凡缁忕櫥褰曪紝濡傛灉鐧诲綍浜嗭紝鍐嶉獙璇佹潈闄� + * @param token 鐧诲綍鐢ㄦ埛token + * @param privilege 楠岃瘉涓�涓潈闄� + * @param allPrivilege 楠岃瘉鎵�鏈夋潈闄� + * @param anyPrivilege 楠岃瘉浠讳綍涓�涓潈闄� + * @return SsoVo + */ + @Hidden + @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){ + vo.dataSourceName = userPo.orgTag ; + vo.logined = true ; + 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 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 ; + } + ///////////////////////////////////////////////////////////////// + // + // 浠ヤ笅绉佹湁鏂规硶 + // + ///////////////////////////////////////////////////////////////// + /** + * 鍒ゆ柇鐧诲綍鐢ㄦ埛鏄惁鎷ユ湁鎸囧畾鐨勪竴涓潈闄� + * @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 ; + } + + +} -- Gitblit v1.8.0