|
|
@@ -1,24 +1,26 @@
|
|
|
package cn.reghao.dfs.store.inerceptor;
|
|
|
|
|
|
import cn.reghao.dfs.store.util.UserContext;
|
|
|
-import cn.reghao.jutil.tool.jwt.Jwt;
|
|
|
-import cn.reghao.jutil.tool.jwt.JwtPayload;
|
|
|
-import io.jsonwebtoken.ExpiredJwtException;
|
|
|
-import io.jsonwebtoken.JwtException;
|
|
|
+import cn.reghao.tnb.account.api.dto.AccountInfo;
|
|
|
+import cn.reghao.tnb.account.api.iface.JwtService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.servlet.*;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
-import java.io.PrintWriter;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
* @date 2022-08-25 21:14:23
|
|
|
*/
|
|
|
@Slf4j
|
|
|
+@Component
|
|
|
public class JwtTokenFilter implements Filter {
|
|
|
+ @DubboReference(check = false)
|
|
|
+ private JwtService jwtService;
|
|
|
+
|
|
|
@Override
|
|
|
public void init(FilterConfig filterConfig) throws ServletException {
|
|
|
}
|
|
|
@@ -26,29 +28,19 @@ public class JwtTokenFilter implements Filter {
|
|
|
@Override
|
|
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
|
|
throws IOException, ServletException {
|
|
|
+ long userId = -1L;
|
|
|
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
|
|
- String jwtToken = getJwtToken(httpServletRequest);
|
|
|
- Long userId = -1L;
|
|
|
- if (jwtToken != null) {
|
|
|
- /*try {
|
|
|
- JwtPayload jwtPayload = Jwt.parse(jwtToken, "");
|
|
|
- String userId = jwtPayload.getUserId();
|
|
|
- mutableHttpServletRequest.putHeader("x-user-id", userId);
|
|
|
- } catch (JwtException jwtException) {
|
|
|
- String msg;
|
|
|
- if (jwtException instanceof ExpiredJwtException) {
|
|
|
- msg = "登录已过期, 请重新登录";
|
|
|
- } else {
|
|
|
- // token 无效
|
|
|
- msg = "token is invalid";
|
|
|
+ String auth = httpServletRequest.getHeader("Authorization");
|
|
|
+ if (auth != null) {
|
|
|
+ String jwt = auth.replace("Bearer ", "");
|
|
|
+ try {
|
|
|
+ AccountInfo accountInfo = jwtService.getAccountInfo(jwt);
|
|
|
+ if (accountInfo != null) {
|
|
|
+ userId = accountInfo.getUserId();
|
|
|
}
|
|
|
-
|
|
|
- HttpServletResponse httpServletResponse = (HttpServletResponse) response;
|
|
|
- writeResponse(httpServletResponse, msg);
|
|
|
- return;
|
|
|
- }*/
|
|
|
- } else {
|
|
|
- //mutableHttpServletRequest.putHeader("x-user-id", "-1");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
try (UserContext context = new UserContext(userId)) {
|
|
|
@@ -56,22 +48,6 @@ public class JwtTokenFilter implements Filter {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void writeResponse(HttpServletResponse response, String msg) throws IOException {
|
|
|
- response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
- response.setContentType("text/html; charset=utf-8");
|
|
|
- PrintWriter printWriter = response.getWriter();
|
|
|
- printWriter.write(msg);
|
|
|
- }
|
|
|
-
|
|
|
- private String getJwtToken(HttpServletRequest request) {
|
|
|
- String headerValue = request.getHeader(Jwt.AUTH_HEADER);
|
|
|
- if (headerValue != null && headerValue.startsWith(Jwt.JWT_PREFIX)) {
|
|
|
- return headerValue.replace(Jwt.JWT_PREFIX, "");
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public void destroy() {
|
|
|
}
|