|
|
@ -1,12 +1,7 @@
|
|
|
|
package com.luoo.tag.interceptor;
|
|
|
|
package com.luoo.tag.interceptor;
|
|
|
|
|
|
|
|
|
|
|
|
import com.luoo.tag.config.RequestContext;
|
|
|
|
|
|
|
|
import exception.BizException;
|
|
|
|
|
|
|
|
import io.jsonwebtoken.Claims;
|
|
|
|
import io.jsonwebtoken.Claims;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
|
|
|
import org.springframework.lang.Nullable;
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
import util.JwtUtil;
|
|
|
|
import util.JwtUtil;
|
|
|
@ -14,35 +9,37 @@ import util.JwtUtil;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* JWT拦截器
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
|
|
@Component
|
|
|
|
@Component
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
|
|
|
public class JwtInterceptor implements HandlerInterceptor {
|
|
|
|
public class JwtInterceptor implements HandlerInterceptor {
|
|
|
|
private final JwtUtil jwtUtil;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
|
|
|
|
@Autowired
|
|
|
|
log.info("JWT拦截器");
|
|
|
|
private JwtUtil jwtUtil;
|
|
|
|
String authHeader = request.getHeader("Admin-token");
|
|
|
|
|
|
|
|
if(StringUtils.isBlank(authHeader) || !authHeader.startsWith("Bearer ")){
|
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
|
throw new BizException("JWT令牌缺失");
|
|
|
|
System.out.println("经过了拦截器");
|
|
|
|
}
|
|
|
|
String header = request.getHeader("Authorization");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (header!=null && !"".equals(header)) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (header.startsWith("Bearer ")){
|
|
|
|
|
|
|
|
String token = header.substring(7);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String token = authHeader.substring(7);
|
|
|
|
|
|
|
|
Claims claims = jwtUtil.parseJWT(token);
|
|
|
|
Claims claims = jwtUtil.parseJWT(token);
|
|
|
|
RequestContext.set(claims.getId(), claims.getSubject());
|
|
|
|
String roles = (String) claims.get("roles");
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (roles != null || !roles.equals("admin")) {
|
|
|
|
throw new BizException("JWT令牌解析异常");
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
|
|
throw new RuntimeException("令牌不正确!");
|
|
|
|
@Nullable Exception ex) throws Exception {
|
|
|
|
}
|
|
|
|
RequestContext.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|