diff --git a/luoo_comment/src/main/java/com/luoo/comment/config/TokenInterceptor.java b/luoo_comment/src/main/java/com/luoo/comment/config/TokenInterceptor.java new file mode 100644 index 0000000..256dd1d --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/config/TokenInterceptor.java @@ -0,0 +1,58 @@ +package com.luoo.comment.config; + +import com.luoo.comment.constant.GlobalConstant; +import com.luoo.comment.pojo.LoginAuthDto; +import com.luoo.comment.utils.ThreadLocalMap; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpHeaders; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Component +@Slf4j +public class TokenInterceptor implements HandlerInterceptor { + + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + return HandlerInterceptor.super.preHandle(request, response, handler); + } + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { + + String token = StringUtils.substringAfter(request.getHeader(HttpHeaders.AUTHORIZATION), "Bearer "); + LoginAuthDto loginUser= decode(token); + if (loginUser == null) { + log.error("获取用户信息失败, 不允许操作,需重新登录"); + throw new RuntimeException("获取用户信息失败, 不允许操作,需重新登录"); + } + log.info("请求uri => {} method => {} user => {} userId => {} token => {}", + request.getRequestURI(), request.getMethod(), loginUser.getLoginName(), loginUser.getUserId(),token); + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + ThreadLocalMap.remove(GlobalConstant.TOKEN_AUTH_DTO); + } + + /** + * 解析token + * @param token + * @return + */ + public static LoginAuthDto decode(String token){ +// Jwt decode = JwtHelper.decode(token); +// return decodeLoginUser(decode); + // 解析 token + return null; + } + + + +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/constant/GlobalConstant.java b/luoo_comment/src/main/java/com/luoo/comment/constant/GlobalConstant.java new file mode 100644 index 0000000..12f2ed3 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/constant/GlobalConstant.java @@ -0,0 +1,9 @@ +package com.luoo.comment.constant; + +public class GlobalConstant { + + /** + * 全局用户名 + */ + public static final String TOKEN_AUTH_DTO = "CURRENT_USER_DTO"; +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/BaseController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/BaseController.java new file mode 100644 index 0000000..cca3b1d --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/BaseController.java @@ -0,0 +1,21 @@ +package com.luoo.comment.controller; + +import cn.hutool.core.util.ObjectUtil; +import com.luoo.comment.pojo.LoginAuthDto; +import com.luoo.comment.utils.ThreadLocalMap; + +public class BaseController { + + /** + * 全局用户名 + */ + public static final String TOKEN_AUTH_DTO = "CURRENT_USER_DTO"; + + protected LoginAuthDto getLoginAuthDto() { + LoginAuthDto loginAuthDto = (LoginAuthDto) ThreadLocalMap.get(TOKEN_AUTH_DTO); + if (ObjectUtil.isEmpty(loginAuthDto)) { + throw new RuntimeException(); + } + return loginAuthDto; + } +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java index 7799fbb..e374675 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java @@ -7,6 +7,7 @@ import api.StatusCode; import com.luoo.comment.pojo.Comment; import com.luoo.comment.pojo.CommentDto; import com.luoo.comment.pojo.CommentVo; +import com.luoo.comment.pojo.LoginAuthDto; import com.luoo.comment.service.CommentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -18,7 +19,7 @@ import java.util.List; @RestController @CrossOrigin @RequestMapping("/comment") -public class CommentController { +public class CommentController extends BaseController{ @Autowired private CommentService commentService; @@ -40,6 +41,8 @@ public class CommentController { @PostMapping("/save") public Result save(@RequestBody CommentDto commentDto){ + LoginAuthDto loginAuthDto = getLoginAuthDto(); + commentDto.setUserId(loginAuthDto.getUserId()); commentService.save(commentDto); return Result.success(); @@ -61,12 +64,13 @@ public class CommentController { @PutMapping("/thumbup/{commentId}") public Result thumbup(@PathVariable String commentId){ - String userid="111"; - if (redisTemplate.opsForValue().get("thumbup_"+userid)!=null){ + LoginAuthDto loginAuthDto = getLoginAuthDto(); + String userId = loginAuthDto.getUserId(); + if (redisTemplate.opsForValue().get("thumbup_"+userId)!=null){ return Result.failed(StatusCode.COMMENT_REPEAT_SUBMIT); } commentService.thumbup(commentId); - redisTemplate.opsForValue().set("thumbup_"+userid,1); + redisTemplate.opsForValue().set("thumbup_"+userId,1); return Result.success(); } } diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/LoginAuthDto.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/LoginAuthDto.java new file mode 100644 index 0000000..6dee043 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/LoginAuthDto.java @@ -0,0 +1,29 @@ +package com.luoo.comment.pojo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.io.Serializable; + +@Data +@ApiModel(value = "登录人信息") +public class LoginAuthDto implements Serializable { + private static final long serialVersionUID = -1137852221455042256L; + @ApiModelProperty(value = "用户ID") + private String userId; + @ApiModelProperty(value = "登录名") + private String loginName; + @ApiModelProperty(value = "用户名") + private String userName; + + public LoginAuthDto() { + } + + public LoginAuthDto(String userId, String loginName, String userName) { + this.userId = userId; + this.loginName = loginName; + this.userName = userName; + } + +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/utils/ThreadLocalMap.java b/luoo_comment/src/main/java/com/luoo/comment/utils/ThreadLocalMap.java new file mode 100644 index 0000000..92bccf6 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/utils/ThreadLocalMap.java @@ -0,0 +1,84 @@ +package com.luoo.comment.utils; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import java.util.HashMap; +import java.util.Map; + + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class ThreadLocalMap { + /** + * The constant threadContext. + */ + private final static ThreadLocal> THREAD_CONTEXT = new MapThreadLocal(); + + /** + * Put. + * + * @param key the key + * @param value the value + */ + public static void put(String key, Object value) { + getContextMap().put(key, value); + } + + /** + * Remove object. + * + * @param key the key + * + * @return the object + */ + public static Object remove(String key) { + return getContextMap().remove(key); + } + + /** + * Get object. + * + * @param key the key + * + * @return the object + */ + public static Object get(String key) { + return getContextMap().get(key); + } + + private static class MapThreadLocal extends ThreadLocal> { + /** + * Initial value map. + * + * @return the map + */ + @Override + protected Map initialValue() { + return new HashMap(8) { + + private static final long serialVersionUID = 3637958959138295593L; + + @Override + public Object put(String key, Object value) { + return super.put(key, value); + } + }; + } + } + + /** + * 取得thread context Map的实例。 + * + * @return thread context Map的实例 + */ + private static Map getContextMap() { + return THREAD_CONTEXT.get(); + } + + /** + * 清理线程所有被hold住的对象。以便重用! + */ + public static void remove() { + getContextMap().clear(); + } +} \ No newline at end of file