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 0d5ae7a..0e4974c 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 @@ -6,6 +6,7 @@ import api.Result; import api.StatusCode; import com.luoo.comment.pojo.*; import com.luoo.comment.service.CommentService; +import com.luoo.comment.util.IpUtil; import constants.Constants; import controller.BaseController; import dto.UserLoginDto; @@ -97,7 +98,7 @@ public class CommentController extends BaseController { comment.setUserId(userId); comment.setNickName(userLoginDto.getNickName()); comment.setAvatar(Constants.RESOURCE_PREFIX+userLoginDto.getAvatar()); - comment.setLocation(getIpAddr(request)); + comment.setLocation(IpUtil.getIpLocation(getIpAddr(request))); commentService.save(comment,userLoginDto); return Result.success(); diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/Comment.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/Comment.java index df26ffe..98c28ca 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/pojo/Comment.java +++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/Comment.java @@ -1,7 +1,9 @@ package com.luoo.comment.pojo; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import org.springframework.data.annotation.Id; +import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; import java.util.Date; @@ -15,6 +17,8 @@ public class Comment implements Serializable { // 评论内容 private String content; // 发布时间 + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date publishTime; private String userId; diff --git a/luoo_tag/src/main/java/com/luoo/tag/interceptor/JwtInterceptor.java b/luoo_tag/src/main/java/com/luoo/tag/interceptor/JwtInterceptor.java index e3e833b..4fee220 100644 --- a/luoo_tag/src/main/java/com/luoo/tag/interceptor/JwtInterceptor.java +++ b/luoo_tag/src/main/java/com/luoo/tag/interceptor/JwtInterceptor.java @@ -1,12 +1,7 @@ package com.luoo.tag.interceptor; -import com.luoo.tag.config.RequestContext; -import exception.BizException; import io.jsonwebtoken.Claims; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang.StringUtils; -import org.springframework.lang.Nullable; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import util.JwtUtil; @@ -14,35 +9,37 @@ import util.JwtUtil; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -/** - * JWT拦截器 - */ -@Slf4j @Component -@RequiredArgsConstructor public class JwtInterceptor implements HandlerInterceptor { - private final JwtUtil jwtUtil; - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{ - log.info("JWT拦截器"); - String authHeader = request.getHeader("Admin-token"); - if(StringUtils.isBlank(authHeader) || !authHeader.startsWith("Bearer ")){ - throw new BizException("JWT令牌缺失"); - } + @Autowired + private JwtUtil jwtUtil; - try { - String token = authHeader.substring(7); - Claims claims = jwtUtil.parseJWT(token); - RequestContext.set(claims.getId(), claims.getSubject()); - } catch (Exception e) { - throw new BizException("JWT令牌解析异常"); - } + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + System.out.println("经过了拦截器"); + String header = request.getHeader("Authorization"); - return true; - } + if (header!=null && !"".equals(header)) { - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, - @Nullable Exception ex) throws Exception { - RequestContext.remove(); + if (header.startsWith("Bearer ")){ + String token = header.substring(7); + try { + Claims claims = jwtUtil.parseJWT(token); + String roles = (String) claims.get("roles"); + if (roles != null || !roles.equals("admin")) { + request.setAttribute("claims_admin",token); + } + if (roles != null || !roles.equals("user")) { + request.setAttribute("claims_user",token); + request.setAttribute("userid",claims.getId()); + request.setAttribute("mobile",claims.getSubject()); + + } + } catch (Exception e) { + throw new RuntimeException("令牌不正确!"); + } + } + } + return true; } }