Просмотр исходного кода

userId 的获取在 dfs-gw 中完成

reghao 3 лет назад
Родитель
Сommit
ccde4f95dc

+ 2 - 2
src/main/java/cn/reghao/dfs/store/StoreApplication.java → src/main/java/cn/reghao/dfs/store/DfsStoreApplication.java

@@ -4,8 +4,8 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 @SpringBootApplication
-public class StoreApplication {
+public class DfsStoreApplication {
     public static void main(String[] args) {
-        SpringApplication.run(StoreApplication.class, args);
+        SpringApplication.run(DfsStoreApplication.class, args);
     }
 }

+ 0 - 44
src/main/java/cn/reghao/dfs/store/inerceptor/UserContextFiler.java

@@ -1,44 +0,0 @@
-package cn.reghao.dfs.store.inerceptor;
-
-import cn.reghao.dfs.store.util.jwt.Jwt;
-import cn.reghao.jutil.jdk.result.WebBody;
-import cn.reghao.dfs.store.util.UserContext;
-import org.springframework.core.annotation.Order;
-import org.springframework.stereotype.Component;
-import org.springframework.web.filter.OncePerRequestFilter;
-
-import javax.servlet.*;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-
-/**
- * @author reghao
- * @date 2022-04-27 14:05:11
- */
-@Component
-@Order(1)
-public class UserContextFiler extends OncePerRequestFilter {
-    @Override
-    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
-            throws IOException, ServletException {
-        String userIdStr = "-1";
-        String value = request.getHeader("Authorization");
-        if (value != null) {
-            String token = value.replace("Bearer ", "");
-            userIdStr = Jwt.parse(token).getUserId();
-        }
-
-        try(UserContext userContext = new UserContext(userIdStr)) {
-            chain.doFilter(request, response);
-        }
-    }
-
-    private void writeResp(HttpServletResponse response) throws IOException {
-        String resp = WebBody.failWithMsg("用户未登录");
-        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
-        response.setContentType("application/json; charset=utf-8");
-        response.getOutputStream().write(resp.getBytes(StandardCharsets.UTF_8));
-    }
-}

+ 0 - 47
src/main/java/cn/reghao/dfs/store/util/ServletUtil.java

@@ -1,47 +0,0 @@
-package cn.reghao.dfs.store.util;
-
-import org.springframework.util.StringUtils;
-import org.springframework.web.context.request.RequestContextHolder;
-import org.springframework.web.context.request.ServletRequestAttributes;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-
-/**
- * @author reghao
- * @date 2021-06-02 13:16:58
- */
-public class ServletUtil {
-    public static HttpSession getSession() {
-        return getRequest().getSession();
-    }
-
-    public static String getSessionId() {
-        return getRequest().getSession().getId();
-    }
-
-    /**
-     * 获取 query 参数值
-     *
-     * @param
-     * @return
-     * @date 2021-06-02 下午1:19
-     */
-    public static String getRequestParam(String param, String defaultValue){
-        String parameter = getRequest().getParameter(param);
-        return StringUtils.isEmpty(parameter) ? defaultValue : parameter;
-    }
-
-    public static HttpServletRequest getRequest(){
-        return getServletRequest().getRequest();
-    }
-
-    public static HttpServletResponse getResponse(){
-        return getServletRequest().getResponse();
-    }
-
-    private static ServletRequestAttributes getServletRequest(){
-        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
-    }
-}

+ 0 - 24
src/main/java/cn/reghao/dfs/store/util/UserContext.java

@@ -1,24 +0,0 @@
-package cn.reghao.dfs.store.util;
-
-/**
- * 获取当前请求的用户
- *
- * @author reghao
- * @date 2020-06-23 19:20:56
- */
-public class UserContext implements AutoCloseable {
-    static final ThreadLocal<String> CURRENT = new ThreadLocal<>();
-
-    public UserContext(String userId) {
-        CURRENT.set(userId);
-    }
-
-    public static String getUserId() {
-        return CURRENT.get();
-    }
-
-    @Override
-    public void close() {
-        CURRENT.remove();
-    }
-}

+ 0 - 66
src/main/java/cn/reghao/dfs/store/util/jwt/Jwt.java

@@ -1,66 +0,0 @@
-package cn.reghao.dfs.store.util.jwt;
-
-import cn.reghao.dfs.store.util.ServletUtil;
-import io.jsonwebtoken.Claims;
-import io.jsonwebtoken.Jwts;
-import io.jsonwebtoken.SignatureAlgorithm;
-
-import java.util.Date;
-
-/**
- * JWT 令牌
- * TODO 将 JWT 令牌存放在 redis 中
- *
- * @author reghao
- * @date 2019-11-17 23:10:58
- */
-public class Jwt {
-    public static final String JWT_PREFIX = "Bearer ";
-    public static final String AUTH_HEADER = "Authorization";
-
-    // TODO 有效期和 key 都应该可以动态设置,有效期一周
-    private static final long EXPIRATION_TIME = 60_000*60*24*7;
-    private static final String SIGN_KEY = "tnb.reghao.cn";
-
-    /**
-     * 生成一个 token
-     *
-     * @param
-     * @return
-     * @date 2019-11-21 下午4:39
-     */
-    public static String create(JwtPayload payload) {
-        return Jwts.builder()
-                .claim("authorities", payload.getRoles())
-                .setSubject(payload.getUserId())
-                .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
-                .signWith(SignatureAlgorithm.HS256, SIGN_KEY)
-                .compact();
-    }
-
-    /**
-     * 解析 token,过期的 token 会抛出 ExpiredJwtException 异常
-     *
-     * @param
-     * @return
-     * @date 2021-07-27 下午2:37
-     */
-    public static JwtPayload parse(String token) {
-        Claims claims = Jwts.parser().setSigningKey(SIGN_KEY).parseClaimsJws(token).getBody();
-        String username = claims.getSubject();
-        String roles = (String) claims.get("authorities");
-        Date expiration = claims.getExpiration();
-        return new JwtPayload(username, roles, expiration);
-    }
-
-    // TODO 不应该依赖 ServletUtil
-    @Deprecated
-    public static JwtPayload getUserInfo() {
-        String value = ServletUtil.getRequest().getHeader("Authorization");
-        if (value != null) {
-            String token = value.replace("Bearer ", "");
-            return parse(token);
-        }
-        return null;
-    }
-}

+ 0 - 28
src/main/java/cn/reghao/dfs/store/util/jwt/JwtPayload.java

@@ -1,28 +0,0 @@
-package cn.reghao.dfs.store.util.jwt;
-
-import lombok.Data;
-
-import java.util.Date;
-
-/**
- * @author reghao
- * @date 2021-07-26 09:58:45
- */
-@Data
-public class JwtPayload {
-    private String userId;
-    private String roles;
-    private Date expiration;
-    private String signKey;
-
-    public JwtPayload(String userId, String roles) {
-        this.userId = userId;
-        this.roles = roles;
-    }
-
-    public JwtPayload(String userId, String roles, Date expiration) {
-        this.userId = userId;
-        this.roles = roles;
-        this.expiration = expiration;
-    }
-}