1. handle user info

main
lyp 10 months ago
parent 7d984e4c7f
commit 0bb25f2f73

@ -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;
}
}

@ -0,0 +1,9 @@
package com.luoo.comment.constant;
public class GlobalConstant {
/**
*
*/
public static final String TOKEN_AUTH_DTO = "CURRENT_USER_DTO";
}

@ -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;
}
}

@ -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<Void> 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();
}
}

@ -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;
}
}

@ -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<Map<String, Object>> 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<Map<String, Object>> {
/**
* Initial value map.
*
* @return the map
*/
@Override
protected Map<String, Object> initialValue() {
return new HashMap<String, Object>(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<String, Object> getContextMap() {
return THREAD_CONTEXT.get();
}
/**
* 线hold便
*/
public static void remove() {
getContextMap().clear();
}
}
Loading…
Cancel
Save