wangliang 4 lat temu
rodzic
commit
664b0e634e

+ 3 - 3
teachcloud-common/src/main/java/com/qmth/teachcloud/common/SignatureEntityTest.java

@@ -13,9 +13,9 @@ public class SignatureEntityTest {
 
     private static final String PATTERN = "{0} {1}{2}{3}";
 
-    private static final String FIELD_JOINER = ":";
+    public static final String FIELD_JOINER = ":";
 
-    private static final String PARAM_JOINER = "&";
+    public static final String PARAM_JOINER = "&";
 
     private static Map<String, SignatureType> typeMap = new HashMap<>();
 
@@ -98,7 +98,7 @@ public class SignatureEntityTest {
         this.secret = secret;
     }
 
-    private static String encrypt(String... values) {
+    public static String encrypt(String... values) {
         return Base64Util.encode(ShaUtils.sha1(StringUtils.join(values, PARAM_JOINER)));
     }
 

+ 46 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/bean/dto/UserAuthenticationDto.java

@@ -0,0 +1,46 @@
+package com.qmth.teachcloud.common.bean.dto;
+
+import io.swagger.annotations.ApiModelProperty;
+
+import java.io.Serializable;
+
+/**
+ * @Description: 临时授权码
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/6/29
+ */
+public class UserAuthenticationDto implements Serializable {
+
+    @ApiModelProperty("uid")
+    private String uid;
+
+    @ApiModelProperty("临时授权码")
+    private String code;
+
+    public UserAuthenticationDto() {
+
+    }
+
+    public UserAuthenticationDto(String uid, String code) {
+        this.uid = uid;
+        this.code = code;
+    }
+
+    public String getUid() {
+        return uid;
+    }
+
+    public void setUid(String uid) {
+        this.uid = uid;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+}

+ 1 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/contant/SystemConstant.java

@@ -114,6 +114,7 @@ public class SystemConstant {
     public static final int DEFAULT_SESSION_EXPIRE = 1;//过期时间1天
     public static final long REDIS_DEFAULT_EXPIRE_TIME = 24 * 60L * 60L;//过期时间24小时
     public static final long REDIS_CREATE_PDF_EXPIRE_TIME = 1 * 60L * 60L;//过期时间1小时
+    public static final long REDIS_WHU_USER_AUTH_EXPIRE_TIME = 2 * 60L;//过期时间2分钟
 
     /**
      * redis lock

+ 17 - 0
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/cache/RedisKeyHelper.java

@@ -0,0 +1,17 @@
+package com.qmth.teachcloud.report.business.cache;
+
+/**
+ * @Description: redis cache helper
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/4/26
+ */
+public class RedisKeyHelper {
+
+    private static String whuUserAuthFix = "whu_user_auth:";
+
+    public static String whuUserAuth(String key) {
+        return whuUserAuthFix + key;
+    }
+}

+ 31 - 0
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/cache/WhuUserAuthCacheUtil.java

@@ -0,0 +1,31 @@
+package com.qmth.teachcloud.report.business.cache;
+
+import com.qmth.teachcloud.common.bean.dto.UserAuthenticationDto;
+import com.qmth.teachcloud.common.contant.SpringContextHolder;
+import com.qmth.teachcloud.common.contant.SystemConstant;
+import com.qmth.teachcloud.common.util.RedisUtil;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @Description: 武大开发鉴权用户临时授权cache
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/6/29
+ */
+public class WhuUserAuthCacheUtil {
+    private static RedisUtil redisUtil = SpringContextHolder.getBean(RedisUtil.class);
+
+    public static void setAuthCode(UserAuthenticationDto userAuthenticationDto) {
+        redisUtil.set(RedisKeyHelper.whuUserAuth(userAuthenticationDto.getUid()), userAuthenticationDto.getCode(), SystemConstant.REDIS_WHU_USER_AUTH_EXPIRE_TIME, TimeUnit.SECONDS);
+    }
+
+    public static String getAuthCode(String key) {
+        return (String) redisUtil.get(RedisKeyHelper.whuUserAuth(key));
+    }
+
+    public static void deleteAuthCode(String key) {
+        redisUtil.delete(RedisKeyHelper.whuUserAuth(key));
+    }
+}

+ 64 - 11
teachcloud-report/src/main/java/com/qmth/teachcloud/report/api/WudaOpenApiController.java

@@ -1,33 +1,39 @@
 package com.qmth.teachcloud.report.api;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.annotation.BOOL;
 import com.qmth.boot.api.constant.ApiConstant;
+import com.qmth.teachcloud.common.SignatureEntityTest;
+import com.qmth.teachcloud.common.bean.dto.UserAuthenticationDto;
+import com.qmth.teachcloud.common.bean.result.LoginResult;
 import com.qmth.teachcloud.common.config.DictionaryConfig;
 import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.entity.BasicSchool;
 import com.qmth.teachcloud.common.entity.SysUser;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
+import com.qmth.teachcloud.common.enums.RoleTypeEnum;
 import com.qmth.teachcloud.common.service.CacheService;
-import com.qmth.teachcloud.common.util.JacksonUtil;
-import com.qmth.teachcloud.common.util.Result;
-import com.qmth.teachcloud.common.util.ServletUtil;
+import com.qmth.teachcloud.common.service.SysUserService;
+import com.qmth.teachcloud.common.service.TeachcloudCommonService;
+import com.qmth.teachcloud.common.util.*;
 import com.qmth.teachcloud.report.aspect.ApiControllerAspect;
-import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiOperation;
-import io.swagger.annotations.ApiResponse;
-import io.swagger.annotations.ApiResponses;
+import com.qmth.teachcloud.report.business.cache.WhuUserAuthCacheUtil;
+import io.swagger.annotations.*;
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.validation.BindingResult;
+import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.validation.Valid;
 import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
+import java.text.MessageFormat;
+import java.util.List;
 import java.util.Objects;
 
 @Api(tags = "武大开放接口层apiController")
@@ -42,6 +48,12 @@ public class WudaOpenApiController {
     @Resource
     CacheService cacheService;
 
+    @Resource
+    SysUserService sysUserService;
+
+    @Resource
+    TeachcloudCommonService teachcloudCommonService;
+
     @ApiOperation(value = "cas鉴权接口")
     @RequestMapping(value = "/authentication", method = RequestMethod.GET)
     @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = Result.class)})
@@ -72,6 +84,47 @@ public class WudaOpenApiController {
         response.sendRedirect(dictionaryConfig.sysDomain().getReportUrl() + basicSchool.getId() + "/" + uid);
     }
 
+    @ApiOperation(value = "cas用户鉴权测试接口")
+    @RequestMapping(value = "/user/authentication/test", method = RequestMethod.GET)
+    @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = Result.class)})
+    @Aac(auth = BOOL.FALSE)
+    public void userAuthenticationTest(HttpServletRequest request, HttpServletResponse response) throws IOException {
+        String uid = "yz1";
+        UserAuthenticationDto userAuthenticationDto = new UserAuthenticationDto(uid, SystemConstant.getUuid());
+        WhuUserAuthCacheUtil.setAuthCode(userAuthenticationDto);
+        String PATTERN = "{0}{1}{2}";
+        String code = MessageFormat.format(PATTERN, userAuthenticationDto.getUid(), SignatureEntityTest.FIELD_JOINER, SignatureEntityTest.encrypt(userAuthenticationDto.getCode()));
+        log.info("code:{}", code);
+        response.setHeader("Access-Control-Allow-Origin", "*");
+        response.sendRedirect(dictionaryConfig.sysDomain().getReportUrl() + code);
+    }
+
+    @ApiOperation(value = "cas用户临时授权登录接口")
+    @RequestMapping(value = "/user/authentication/login", method = RequestMethod.POST)
+    @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = LoginResult.class)})
+    @Aac(auth = BOOL.FALSE)
+    public Result login(@ApiParam(value = "授权码信息", required = true) @RequestParam String code) throws NoSuchAlgorithmException {
+        String[] values = StringUtils.split(code, SignatureEntityTest.FIELD_JOINER);
+        log.info("values:{}", JacksonUtil.parseJson(values));
+        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
+        wrapper.lambda().eq(SysUser::getLoginName, values[0]);
+        SysUser sysUser = sysUserService.getOne(wrapper);
+        //用户不存在
+        if (Objects.isNull(sysUser)) {
+            throw ExceptionResultEnum.USER_NO_DATA.exception();
+        }
+        //停用
+        if (!sysUser.getEnable()) {
+            throw ExceptionResultEnum.USER_ENABLE.exception();
+        }
+
+        String userAuthCode = WhuUserAuthCacheUtil.getAuthCode(values[0]);
+        if (Objects.isNull(userAuthCode) || !Objects.equals(values[1], SignatureEntityTest.encrypt(userAuthCode))) {
+            throw ExceptionResultEnum.ERROR.exception("临时授权码已过期");
+        }
+        return ResultUtil.ok(teachcloudCommonService.login(sysUser.getPassword(), sysUser));
+    }
+
 //    @ApiOperation(value = "cas鉴权接口")
 //    @RequestMapping(value = "/authentication/{studentCode}", method = RequestMethod.GET)
 //    @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = Result.class)})