package com.dy.sso.busi; import com.dy.common.aop.SsoVo; 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 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.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; /** * 注解Tag 在API中显示: Tag 注解, 给整个接口起了个名字与描述" * 注解ApiResponses 和 注解ApiResponse 用来配置响应; * 注解Operation 用来设置接口名称和描述; * 注解Parameter 用来设置请求参数的描述、是否必填和示例。 */ @Slf4j @Tag(name = "用户登录", description = "单点登录系统(sso)") @RestController @RequestMapping(path="sso") @SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked") public class SsoCtrl { //在属性上注解@Autowired时,会警告 Field injection is not recommended(不再推荐使用字段注入) private SsoSv sv ; //@Autowired //private CacheManager cacheManager ; @Autowired public void setSv(SsoSv sv ){ this.sv = sv ; } /** * 客户端请求用户登录,客户端提交Json数据 * @param vo 用户登录值对象 * @return 登录用户值对象 */ @Operation(summary = "单点登录", description = "提交登录用户值对象(json格式),进行单点登录") /* //下面这个不起作用,通过@RequestBody=直接显示LoginVo的API @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 = "返回登录用户值对象(数据基类的content)", content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = UserVo.class))} ) }) @PostMapping(path = "loginJson", consumes = MediaType.APPLICATION_JSON_VALUE) public BaseResponse loginJson(@Valid @RequestBody LoginVo vo, @Parameter(hidden = true) BindingResult bindingResult) { if(bindingResult != null && bindingResult.hasErrors()){ return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); } String uuid ; BaUser userPo ; try { //Boolean flag = cacheManager.getCacheNames().isEmpty() ; uuid = UUID.randomUUID().toString(); userPo = this.sv.login(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.buildFail("登录失败"); } } /** * 客户端请求用户登录,客户端提交form表单 * @param vo 登录用户form表单对象 * @return 登录用户值对象 */ @Operation(summary = "单点登录", description = "提交登录用户数据(form表单),进行单点登录") @ApiResponses(value = { @ApiResponse( responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, description = "返回登录用户值对象(数据基类的content)", content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = UserVo.class))} ) }) @PostMapping(path = "loginForm", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public BaseResponse 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()); } String uuid ; BaUser userPo ; try { //Boolean flag = cacheManager.getCacheNames().isEmpty() ; uuid = UUID.randomUUID().toString(); userPo = this.sv.login(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.buildFail("登录失败"); } } /** * 通过UUID退出登录,因为参数是uuid,所以此调用必须是后端相关代码调用,因为前端得不到cookie中的uuid * @param hr HttpServletRequest * @return 正常退出登录返回true,否则返回false */ @Operation(summary = "单点登出", description = "提交token(在header中),进行单点登出") @ApiResponses(value = { @ApiResponse( responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, description = "返回处理结果(成功true,失败false)(数据基类的content)", content = {@Content(mediaType = MediaType.TEXT_PLAIN_VALUE, schema = @Schema(implementation = Boolean.class))} ) }) @GetMapping(path = "logout") public BaseResponse 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.buildFail("未从header中得到token"); } } /** * 此方法供子模块系统调用,所以不公开在API接口中 * 方法功能:得到登录用户id,否则返回null * @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 ; } /** * 此方法供子模块系统调用,所以不公开在API接口中 * 方法功能:验证是否已经登录,如果登录了,再验证权限 * @param token 登录用户token * @param power 验证一个权限 * @param allPower 验证所有权限 * @param anyPower 验证任何一个权限 * @return SsoVo */ @Hidden @GetMapping(path = "ssoCheck") public SsoVo ssoCheck(String token, String power, String[] allPower, String[] anyPower){ BaUser userPo = this.sv.getByUuid(token) ; SsoVo vo = new SsoVo(); if(userPo != null){ vo.logined = true ; vo.hasPower = true ; vo.dataSourceName = userPo.getOrgTag() ; }else{ vo.logined = false ; vo.hasPower = false ; } return vo ; } }