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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue