فهرست منبع

高并发投票

孙永军 1 سال پیش
والد
کامیت
6fdad1493a

+ 12 - 1
src/main/java/com/smcic/api/hotrank/dto/HotRankDTO.java

@@ -5,9 +5,12 @@ public class HotRankDTO {
 
     private String rk;
 
-    public HotRankDTO(String content, String rk) {
+    private Long nowHot;
+
+    public HotRankDTO(String content, String rk, Long nowHot) {
         this.content = content;
         this.rk = rk;
+        this.nowHot = nowHot;
     }
 
     public String getContent() {
@@ -25,4 +28,12 @@ public class HotRankDTO {
     public void setRk(String rk) {
         this.rk = rk;
     }
+
+    public Long getNowHot() {
+        return nowHot;
+    }
+
+    public void setNowHot(Long nowHot) {
+        this.nowHot = nowHot;
+    }
 }

+ 1 - 1
src/main/java/com/smcic/api/hotrank/service/impl/HotRankServiceImpl.java

@@ -31,7 +31,7 @@ public class HotRankServiceImpl extends ServiceImpl<HotRankMapper, HotRank> impl
         String dt = baseMapper.getLastDt(rank);
 
         return cache.get(new String[]{rank,dt}, (key) -> lambdaQuery().eq(HotRank::getDt, key[1]).eq(HotRank::getPlatform, key[0]).orderByAsc(HotRank::getId).last("limit 50").list()
-                .stream().map(data -> new HotRankDTO(data.getContent(), data.getRk())).collect(Collectors.toList()));
+                .stream().map(data -> new HotRankDTO(data.getContent(), data.getRk(), Long.valueOf(data.getRsCount()))).collect(Collectors.toList()));
 
     }
 }

+ 4 - 0
src/main/java/com/smcic/api/operate/controller/EnrollInfoController.java

@@ -3,6 +3,7 @@ package com.smcic.api.operate.controller;
 
 import com.smcic.api.operate.entity.EnrollInfo;
 import com.smcic.api.operate.service.impl.EnrollInfoServiceImpl;
+import com.smcic.core.auth.LoginRequired;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
@@ -27,6 +28,7 @@ public class EnrollInfoController {
     private EnrollInfoServiceImpl enrollInfoService;
 
     @RequestMapping("/store")
+    @LoginRequired
     public void upload(
             @RequestParam("name") String name,
             @RequestParam("phone") String phone,
@@ -42,6 +44,7 @@ public class EnrollInfoController {
     }
 
     @RequestMapping("list")
+    @LoginRequired
     public List<EnrollInfo> list(@RequestParam(value = "type", required = false, defaultValue = "") String type,
                                  @RequestParam(value = "operateId", required = false, defaultValue = "2") Integer operateId,
                                  @RequestParam(value = "phone", required = false, defaultValue = "") String phone){
@@ -49,6 +52,7 @@ public class EnrollInfoController {
     }
 
     @RequestMapping("rank")
+    @LoginRequired
     public List<EnrollInfo> rank(@RequestParam(value = "type", required = false, defaultValue = "") String type,
                                  @RequestParam(value = "operateId", required = false, defaultValue = "2") Integer operateId,
                                  @RequestParam(value = "phone", required = false, defaultValue = "") String phone){

+ 5 - 23
src/main/java/com/smcic/api/operate/controller/UsersController.java

@@ -25,22 +25,6 @@ public class UsersController {
     @Resource
     private UsersServiceImpl usersService;
 
-    @PostMapping("store")
-    public Map<String, String> store(@RequestBody Users users){
-        usersService.store(users);
-        return ImmutableMap.of("token", AESUtil.encryptHex(users.getPhone()));
-    }
-
-    @RequestMapping("info")
-    public Users info(@RequestParam("phone") String phone, @RequestParam(value = "operateId", required = false, defaultValue = "1") Integer operateId){
-        return usersService.getUserByPhone(phone, operateId);
-    }
-
-    @RequestMapping("draw-plus")
-    public void drawPlus(@RequestParam("phone") String phone, @RequestParam(value = "operateId", required = false, defaultValue = "1") Integer operateId){
-        usersService.drawPlus(phone, operateId);
-    }
-
     @RequestMapping("sendVerifyCode")
     public void sendVerifyCode(@RequestParam("phone") String phone){
         usersService.sendVerifyCode(phone);
@@ -48,14 +32,12 @@ public class UsersController {
 
     @RequestMapping("verifyCode")
     public Map<String, String> verifyCode(@RequestParam("phone") String phone, @RequestParam("code") String code){
-        if(!code.equals("76767676")){
-            usersService.verifyCode(phone, code);
-        }
-        return ImmutableMap.of("token", AESUtil.encryptHex(phone));
+        return  usersService.verifyCode(phone, code);
     }
 
-    @PostMapping("update")
-    public void update(@RequestBody Users users){
-        usersService.update(users);
+    @RequestMapping("sspUser")
+    public Map<String, String> sspUser(@RequestParam("jToken") String jToken){
+        return  usersService.tideLogin(jToken);
     }
+
 }

+ 7 - 4
src/main/java/com/smcic/api/operate/controller/VoteInfoController.java

@@ -3,6 +3,8 @@ package com.smcic.api.operate.controller;
 
 import com.google.common.collect.ImmutableMap;
 import com.smcic.api.operate.service.impl.VoteInfoServiceImpl;
+import com.smcic.core.auth.HttpContextUtil;
+import com.smcic.core.auth.LoginRequired;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
@@ -26,15 +28,16 @@ public class VoteInfoController {
     private VoteInfoServiceImpl voteInfoService;
 
     @RequestMapping("vote")
+    @LoginRequired
     public void vote(@RequestParam("target") String target,
-                     @RequestParam("source") String source,
                      @RequestParam(value = "client",required = false,defaultValue = "") String client,
                      @RequestParam(value = "operateId", required = false, defaultValue = "2") Integer operateId){
-        voteInfoService.vote(target, source, client, operateId);
+        voteInfoService.vote(target, client, operateId);
     }
 
     @RequestMapping("times")
-    public Map<String, Long> votedNum(@RequestParam("phone") String phone, @RequestParam(value = "operateId", required = false, defaultValue = "2")Integer operateId){
-        return ImmutableMap.of("times", voteInfoService.votedTimes(operateId, phone));
+    @LoginRequired
+    public Map<String, Long> votedNum(@RequestParam(value = "operateId", required = false, defaultValue = "2")Integer operateId){
+        return ImmutableMap.of("times", voteInfoService.votedTimes(operateId, HttpContextUtil.getUser().get().getUid()));
     }
 }

+ 87 - 0
src/main/java/com/smcic/api/operate/dto/TideUser.java

@@ -0,0 +1,87 @@
+package com.smcic.api.operate.dto;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@NoArgsConstructor
+@Data
+public class TideUser {
+
+    @JsonProperty("data")
+    private DataDTO data;
+    @JsonProperty("code")
+    private Integer code;
+    @JsonProperty("msg")
+    private String msg;
+
+    @NoArgsConstructor
+    @Data
+    public static class DataDTO {
+        @JsonProperty("result")
+        private ResultDTO result;
+        @JsonProperty("message")
+        private String message;
+        @JsonProperty("status")
+        private String status;
+
+        @NoArgsConstructor
+        @Data
+        public static class ResultDTO {
+            @JsonProperty("moblie")
+            private String moblie;
+            @JsonProperty("is_baoliao")
+            private Integer isBaoliao;
+            @JsonProperty("authentication_state")
+            private Integer authenticationState;
+            @JsonProperty("weibosign")
+            private String weibosign;
+            @JsonProperty("weixinsign")
+            private String weixinsign;
+            @JsonProperty("user_summary")
+            private String userSummary;
+            @JsonProperty("hassign")
+            private Integer hassign;
+            @JsonProperty("nick")
+            private String nick;
+            @JsonProperty("score")
+            private String score;
+            @JsonProperty("currentday")
+            private Integer currentday;
+            @JsonProperty("weibo")
+            private Integer weibo;
+            @JsonProperty("nickname_status")
+            private String nicknameStatus;
+            @JsonProperty("banned")
+            private Integer banned;
+            @JsonProperty("qqsign")
+            private String qqsign;
+            @JsonProperty("qq")
+            private Integer qq;
+            @JsonProperty("extraday")
+            private Integer extraday;
+            @JsonProperty("level")
+            private String level;
+            @JsonProperty("sex")
+            private Integer sex;
+            @JsonProperty("avatar")
+            private String avatar;
+            @JsonProperty("summary_status")
+            private Integer summaryStatus;
+            @JsonProperty("status2")
+            private Integer status2;
+            @JsonProperty("friends")
+            private Integer friends;
+            @JsonProperty("levelnum")
+            private String levelnum;
+            @JsonProperty("avatar_status")
+            private String avatarStatus;
+            @JsonProperty("weixin")
+            private Integer weixin;
+            @JsonProperty("watch")
+            private Integer watch;
+            @JsonProperty("mediaIdState")
+            private Integer mediaIdState;
+        }
+    }
+}

+ 4 - 0
src/main/java/com/smcic/api/operate/entity/KeysConst.java

@@ -14,4 +14,8 @@ public class KeysConst {
     public static final String ENROLL_LIST = "OPERATE_ENROLL_LIST_";
 
     public static final String OPERATE = "OPERATE_";
+
+    public static final String PHONE_CODE = "OPERATE_PHONE_VERIFY_CODE_";
+
+    public static final String TIDE_TOKEN = "OPERATE_TIDE_TOKEN_";
 }

+ 17 - 0
src/main/java/com/smcic/api/operate/entity/UserAuth.java

@@ -0,0 +1,17 @@
+package com.smcic.api.operate.entity;
+
+import lombok.Data;
+
+@Data
+public class UserAuth {
+    private UserPlatform platform;
+    private String uid;
+
+    public UserAuth() {
+    }
+
+    public UserAuth(UserPlatform platform, String uid) {
+        this.platform = platform;
+        this.uid = uid;
+    }
+}

+ 6 - 0
src/main/java/com/smcic/api/operate/entity/UserPlatform.java

@@ -0,0 +1,6 @@
+package com.smcic.api.operate.entity;
+
+public enum UserPlatform {
+    SSP,
+    OTHER
+}

+ 1 - 3
src/main/java/com/smcic/api/operate/service/impl/EnrollInfoServiceImpl.java

@@ -159,9 +159,7 @@ public class EnrollInfoServiceImpl extends ServiceImpl<EnrollInfoMapper, EnrollI
                     }
                     return null;
                 }).filter(data ->
-        {
-            return null != data && data.getStatus() == 1 && (TextUtils.isEmpty(type) || type.equals(data.getType()));
-        })
+                        null != data && data.getStatus() == 1 && (TextUtils.isEmpty(type) || type.equals(data.getType())))
                 .sorted(comparing).collect(Collectors.toList());
     }
 

+ 46 - 80
src/main/java/com/smcic/api/operate/service/impl/UsersServiceImpl.java

@@ -1,24 +1,34 @@
 package com.smcic.api.operate.service.impl;
 
 import cn.hutool.core.util.RandomUtil;
+import cn.hutool.http.HttpRequest;
 import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
 import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
 import com.aliyun.teaopenapi.models.Config;
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableMap;
+import com.smcic.api.operate.dto.TideUser;
+import com.smcic.api.operate.entity.KeysConst;
+import com.smcic.api.operate.entity.UserAuth;
+import com.smcic.api.operate.entity.UserPlatform;
 import com.smcic.api.operate.entity.Users;
-import com.smcic.api.operate.entity.VerifyCode;
 import com.smcic.api.operate.mapper.UsersMapper;
 import com.smcic.api.operate.mapper.VerifyCodeMapper;
 import com.smcic.api.operate.service.IUsersService;
 import com.smcic.core.advice.APIException;
+import com.smcic.core.auth.AESUtil;
+import org.apache.http.util.TextUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.time.Duration;
+import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -33,14 +43,11 @@ import java.util.regex.Pattern;
 @Service
 public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
 
-    @Resource
-    private VerifyCodeMapper verifyCodeMapper;
-
     @Autowired
     private ObjectMapper objectMapper;
 
     @Resource
-    private OperateServiceImpl operateService;
+    private RedisTemplate<String, String> redisTemplate;
 
     private final Logger log = LoggerFactory.getLogger(this.getClass());
 
@@ -58,16 +65,17 @@ public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements
         }
     }
 
-    public void verifyCode(String phone, String code) {
-        VerifyCode verifyCode = verifyCodeMapper.selectOne(new LambdaQueryWrapper<VerifyCode>()
-                .eq(VerifyCode::getPhone, phone)
-                .eq(VerifyCode::getCode, code)
-        );
-        if (verifyCode == null) {
+    public Map<String, String> verifyCode(String phone, String code) {
+        String verifyCode = redisTemplate.opsForValue().get(KeysConst.PHONE_CODE + phone);
+        if (TextUtils.isEmpty(verifyCode) || !code.equals(verifyCode)) {
             throw new APIException(400, "验证码错误");
         }
-        if (System.currentTimeMillis() > verifyCode.getExpireTime()) {
-            throw new APIException(400, "验证码已超时,请重新发送");
+        try {
+            return ImmutableMap.of("token", AESUtil.encryptHex(objectMapper.writeValueAsString(
+                    new UserAuth(UserPlatform.OTHER, phone)
+            )));
+        } catch (JsonProcessingException e) {
+            throw new APIException(400, "验证码错误");
         }
     }
 
@@ -79,14 +87,9 @@ public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements
             throw new APIException(400, "请输入正确的手机号码");
         }
 
-        VerifyCode verifyCode = verifyCodeMapper.selectOne(new LambdaQueryWrapper<VerifyCode>()
-                .eq(VerifyCode::getPhone, phone)
-        );
-        if (verifyCode != null) {
-            if (System.currentTimeMillis() - (verifyCode.getExpireTime() - 60 * 5 * 1000) < 60 * 1000) {
-                throw new APIException("验证码已发送,请等待60秒后重新发送");
-            }
-            verifyCodeMapper.deleteById(phone);
+        Long expire = redisTemplate.getExpire(KeysConst.PHONE_CODE + phone);
+        if (expire != null && expire > 240) {
+            throw new APIException("验证码已发送,请等待60秒后重新发送");
         }
 
         String code = RandomUtil.randomNumbers(4);
@@ -109,71 +112,34 @@ public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements
             }
         }
 
-        verifyCode = new VerifyCode();
-        verifyCode.setPhone(phone);
-        verifyCode.setCode(code);
-        // 5分钟的过期时间
-        verifyCode.setExpireTime(System.currentTimeMillis() + 60 * 5 * 1000);
-        verifyCodeMapper.insert(verifyCode);
-
-    }
-
-    public void store(Users user){
-        operateService.verify(user.getOperateId());
-        verifyCode(user.getPhone(), user.getVerifyCode());
-        Users one = lambdaQuery().eq(Users::getPhone, user.getPhone()).eq(Users::getOperateId, user.getOperateId()).one();
-        if(null != one){
-            one.setAddr(user.getAddr());
-            one.setUserName(user.getUserName());
-            updateById(one);
-        }else{
-            user.setMaxDrawNum(2);
-            user.setDrawNum(1);
-            save(user);
-        }
-
+        redisTemplate.opsForValue().set(KeysConst.PHONE_CODE + phone, code, Duration.ofSeconds(300));
     }
 
-    public void update(Users user){
-        operateService.verify(user.getOperateId());
-        Users one = lambdaQuery().eq(Users::getPhone, user.getPhone()).eq(Users::getOperateId, user.getOperateId()).one();
-        if(null == one){
-            throw new APIException(400, "不存在的用户");
+    public Map<String, String> tideLogin(String jToken){
+        String body = redisTemplate.opsForValue().get(KeysConst.TIDE_TOKEN + jToken);
+        if(TextUtils.isEmpty(body)){
+            body = HttpRequest.post("https://ssp-api.sxtvs.com.cn/apiv4.3/api/user_information")
+                    .body("jToken=" + jToken + "&site=76").execute().body();
+            redisTemplate.opsForValue().set(KeysConst.TIDE_TOKEN + jToken, body, Duration.ofDays(30));
         }
-        one.setAddr(user.getAddr());
-        one.setUserName(user.getUserName());
-        updateById(one);
 
-    }
+        try {
+            TideUser tideUser = objectMapper.readValue(body, TideUser.class);
+            String uid = tideUser.getData().getResult().getMoblie();
+            if(TextUtils.isEmpty(uid)){
+               uid = tideUser.getData().getResult().getWeixinsign();
+            }
+            if(TextUtils.isEmpty(uid)){
+                uid = tideUser.getData().getResult().getWeibosign();
+            }
 
-    public Users getUserByPhone(String phone, Integer operateId){
-        Users one = lambdaQuery().eq(Users::getPhone, phone).eq(Users::getOperateId, operateId).one();
-        if (null == one){
-            throw new APIException(400, "不存在的用户");
+            return ImmutableMap.of("token", AESUtil.encryptHex(objectMapper.writeValueAsString(
+                    new UserAuth(UserPlatform.SSP, uid))));
+        } catch (Exception e) {
+            redisTemplate.delete(KeysConst.TIDE_TOKEN + jToken);
+            throw new APIException(400, "获取用户失败");
         }
-
-        //userDrawNumService.drawNumHandle(one);
-        return one;
     }
 
-    public void drawPlus(String phone, Integer operateId){
-        Users one = lambdaQuery().eq(Users::getPhone, phone).eq(Users::getOperateId, operateId).one();
-        if (null == one){
-            throw new APIException(400, "不存在的用户");
-        }
-        //userDrawNumService.drawPlus(one);
 
-        if(one.getDrawNum() + one.getDrewNum() >= one.getMaxDrawNum()){
-            throw new APIException(400, "您的抽奖次数已达上限");
-        }
-
-        one.setDrawNum(one.getDrawNum() + 1);
-        updateById(one);
-    }
-
-    public void drawDec(Users users){
-        users.setDrawNum(users.getDrawNum() - 1);
-        users.setDrewNum(users.getDrewNum() + 1);
-        updateById(users);
-    }
 }

+ 10 - 24
src/main/java/com/smcic/api/operate/service/impl/VoteInfoServiceImpl.java

@@ -5,12 +5,11 @@ import cn.hutool.core.lang.generator.SnowflakeGenerator;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import com.smcic.api.operate.entity.EnrollInfo;
-import com.smcic.api.operate.entity.KeysConst;
-import com.smcic.api.operate.entity.VoteInfo;
+import com.smcic.api.operate.entity.*;
 import com.smcic.api.operate.mapper.VoteInfoMapper;
 import com.smcic.api.operate.service.IVoteInfoService;
 import com.smcic.core.advice.APIException;
+import com.smcic.core.auth.HttpContextUtil;
 import org.apache.http.util.TextUtils;
 import org.apache.kafka.clients.consumer.ConsumerRecord;
 import org.springframework.data.redis.core.RedisTemplate;
@@ -25,6 +24,7 @@ import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.Map;
+import java.util.Optional;
 
 /**
  * <p>
@@ -56,20 +56,18 @@ public class VoteInfoServiceImpl extends ServiceImpl<VoteInfoMapper, VoteInfo> i
 
     private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
 
-    public void vote(String target, String source, String client, Integer operateId){
+    public void vote(String target, String client, Integer operateId){
+        Optional<UserAuth> user = HttpContextUtil.getUser();
+        String source = user.get().getUid();
 
         String dt = formatter.format(LocalDate.now());
         operateService.verify(operateId);
         long max = 2L;
-        if(client.equals("闪视频")){
+        //client.equals("闪视频")
+        if(UserPlatform.SSP.equals(user.get().getPlatform())){
             max = 10L;
         }
 
-        /*EnrollInfo one = enrollInfoService.lambdaQuery().eq(EnrollInfo::getPhone, target).eq(EnrollInfo::getOperateId, operateId).one();
-
-        if(null == one){
-            throw new APIException(400, "不存在的目标");
-        }*/
         String one = redisTemplate.opsForValue().get(KeysConst.TARGET + operateId + "_" + target);
 
         if (null == one){
@@ -84,10 +82,8 @@ public class VoteInfoServiceImpl extends ServiceImpl<VoteInfoMapper, VoteInfo> i
             vd = Long.parseLong(vds);
         }
 
-        // Long vd = lambdaQuery().eq(VoteInfo::getSource, source).gt(VoteInfo::getCreateTime, LocalDate.now()).count();
-
         if (vd >= max){
-            if(client.equals("闪视频")){
+            if(UserPlatform.SSP.equals(user.get().getPlatform())){
                 throw new APIException(400, "今日投票次数已用完,请明日再来!");
             }else{
                 throw new APIException(400, "今日投票次数已用完,点击下载闪视频获取更多投票机会!");
@@ -109,14 +105,13 @@ public class VoteInfoServiceImpl extends ServiceImpl<VoteInfoMapper, VoteInfo> i
                 }
                 if (vd >= max){
                     redisTemplate.delete(lockKey);
-                    if(client.equals("闪视频")){
+                    if(UserPlatform.SSP.equals(user.get().getPlatform())){
                         throw new APIException(400, "今日投票次数已用完,请明日再来!");
                     }else{
                         throw new APIException(400, "今日投票次数已用完,点击下载闪视频获取更多投票机会!");
                     }
                 }
 
-
                 try {
                     kafkaTemplate.send(KeysConst.KAFKA_QUEUE,
                             objectMapper.writeValueAsString(
@@ -132,14 +127,6 @@ public class VoteInfoServiceImpl extends ServiceImpl<VoteInfoMapper, VoteInfo> i
             }
         }
 
-
-
-        /*one.setVotes(one.getVotes()+1);
-        enrollInfoService.updateById(one);
-
-        VoteInfo voteInfo = new VoteInfo(target, source, client, LocalDateTime.now(), LocalDate.now());
-        save(voteInfo);*/
-
     }
 
     public void lsVerify(Integer operateId, String phone){
@@ -153,7 +140,6 @@ public class VoteInfoServiceImpl extends ServiceImpl<VoteInfoMapper, VoteInfo> i
         if(TextUtils.isEmpty(source))
             return 0L;
         return Long.valueOf(source);
-        //return lambdaQuery().eq(VoteInfo::getSource, phone).eq(VoteInfo::getDt, LocalDate.now()).count();
     }
 
 }

+ 32 - 0
src/main/java/com/smcic/core/auth/HttpContextUtil.java

@@ -0,0 +1,32 @@
+package com.smcic.core.auth;
+
+import cn.hutool.core.util.IdUtil;
+import com.smcic.api.operate.entity.UserAuth;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+
+import java.util.Optional;
+
+public class HttpContextUtil {
+
+    public static Optional<String> getToken() {
+        return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
+                .map(x -> x.getAttribute("token", RequestAttributes.SCOPE_REQUEST))
+                .map(Object::toString);
+    }
+
+    public static void setToken(String token) {
+        Optional.ofNullable(RequestContextHolder.getRequestAttributes())
+                .ifPresent(x -> x.setAttribute("token", token, RequestAttributes.SCOPE_REQUEST));
+    }
+
+    public static Optional<UserAuth> getUser() {
+        return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
+                .map(x -> (UserAuth) x.getAttribute("user", RequestAttributes.SCOPE_REQUEST));
+    }
+
+    public static void setUser(UserAuth userAuth) {
+        Optional.ofNullable(RequestContextHolder.getRequestAttributes())
+                .ifPresent(x -> x.setAttribute("user", userAuth, RequestAttributes.SCOPE_REQUEST));
+    }
+}

+ 11 - 2
src/main/java/com/smcic/core/auth/LoginInterceptor.java

@@ -2,6 +2,8 @@ package com.smcic.core.auth;
 
 
 import cn.hutool.core.util.StrUtil;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.smcic.api.operate.entity.UserAuth;
 import com.smcic.core.advice.APIException;
 import com.smcic.core.conf.Constant;
 import org.springframework.web.method.HandlerMethod;
@@ -12,6 +14,8 @@ import javax.servlet.http.HttpServletResponse;
 import java.lang.reflect.Method;
 
 public class LoginInterceptor implements AsyncHandlerInterceptor {
+
+    private final ObjectMapper objectMapper = new ObjectMapper();
     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
         if (!(handler instanceof HandlerMethod)) {
@@ -28,11 +32,16 @@ public class LoginInterceptor implements AsyncHandlerInterceptor {
 
         // 这写你拦截需要干的事儿,比如取缓存,SESSION,权限判断等
         String token = request.getHeader(Constant.TOKEN_HEADER_NAME);
-
-        if (!AESUtil.isOk(token)) {
+        try {
+            String ds = AESUtil.decryptStr(token);
+            UserAuth userAuth = objectMapper.readValue(ds, UserAuth.class);
+            HttpContextUtil.setUser(userAuth);
+        } catch (Exception e) {
             throw new APIException("没有登录");
         }
 
+        HttpContextUtil.setToken(token);
+
         return true;
     }
 }