parent
eb7d4a37e5
commit
d390e45f14
@ -0,0 +1,48 @@
|
||||
package com.luoo.tag.interceptor;
|
||||
|
||||
import com.luoo.tag.config.RequestContext;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
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("Authorization");
|
||||
if(StringUtils.isBlank(authHeader) && !authHeader.startsWith("Bearer ")){
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
String token = authHeader.substring(7);
|
||||
Claims claims = jwtUtil.parseJWT(token);
|
||||
RequestContext.set(claims.getId(), claims.getSubject());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("JWT令牌解析异常");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
|
||||
@Nullable Exception ex) throws Exception {
|
||||
RequestContext.remove();
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.luoo.tag.pojo;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 用户信息
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class UserInfo {
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String name;
|
||||
}
|
Loading…
Reference in new issue