From d390e45f142203a92df1324c6b3933314981312e Mon Sep 17 00:00:00 2001 From: itao Date: Sun, 14 Jan 2024 18:57:21 +0800 Subject: [PATCH] feat: add jwt interceptor --- .../java/com/luoo/tag/TagApplication.java | 11 ++++- .../com/luoo/tag/config/RequestContext.java | 34 +++++++++++++ .../com/luoo/tag/config/WebMvcConfig.java | 15 ++++++ .../luoo/tag/interceptor/JwtInterceptor.java | 48 +++++++++++++++++++ .../main/java/com/luoo/tag/pojo/UserInfo.java | 21 ++++++++ 5 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 luoo_tag/src/main/java/com/luoo/tag/config/RequestContext.java create mode 100644 luoo_tag/src/main/java/com/luoo/tag/interceptor/JwtInterceptor.java create mode 100644 luoo_tag/src/main/java/com/luoo/tag/pojo/UserInfo.java diff --git a/luoo_tag/src/main/java/com/luoo/tag/TagApplication.java b/luoo_tag/src/main/java/com/luoo/tag/TagApplication.java index c86c290..219910c 100644 --- a/luoo_tag/src/main/java/com/luoo/tag/TagApplication.java +++ b/luoo_tag/src/main/java/com/luoo/tag/TagApplication.java @@ -16,7 +16,9 @@ import org.springframework.core.env.Environment; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import util.IdWorker; -@SpringBootApplication(scanBasePackages = "com.luoo") +import util.JwtUtil; + +@SpringBootApplication(scanBasePackages = "com.luoo.tag") @EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients @@ -46,5 +48,10 @@ public class TagApplication { public IdWorker idWorker(){ return new IdWorker(1, 1); } - + + @Bean + public JwtUtil jwtUtil(){ + return new JwtUtil(); + } + } diff --git a/luoo_tag/src/main/java/com/luoo/tag/config/RequestContext.java b/luoo_tag/src/main/java/com/luoo/tag/config/RequestContext.java new file mode 100644 index 0000000..afbf359 --- /dev/null +++ b/luoo_tag/src/main/java/com/luoo/tag/config/RequestContext.java @@ -0,0 +1,34 @@ +package com.luoo.tag.config; + +import com.luoo.tag.pojo.UserInfo; + +import java.util.Objects; + +/** + * 请求上下文:用户信息 + */ +public class RequestContext { + private final static ThreadLocal THREAD_LOCAL = new ThreadLocal<>(); + + /** + * 设置用户信息 + * @param id 用户ID + * @param name 用户名 + */ + public static void set(String id, String name) { + THREAD_LOCAL.set(UserInfo.builder().id(id).name(name).build()); + } + + public static UserInfo get() { + UserInfo userInfo = THREAD_LOCAL.get(); + // TODO 测试值 + if(Objects.isNull(userInfo)) { + userInfo = UserInfo.builder().id("1627863701048659968").name("foo").build(); + } + return userInfo; + } + + public static void remove() { + THREAD_LOCAL.remove(); + } +} diff --git a/luoo_tag/src/main/java/com/luoo/tag/config/WebMvcConfig.java b/luoo_tag/src/main/java/com/luoo/tag/config/WebMvcConfig.java index 204e573..c23b018 100644 --- a/luoo_tag/src/main/java/com/luoo/tag/config/WebMvcConfig.java +++ b/luoo_tag/src/main/java/com/luoo/tag/config/WebMvcConfig.java @@ -1,11 +1,26 @@ package com.luoo.tag.config; +import com.luoo.tag.interceptor.JwtInterceptor; +import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration +@RequiredArgsConstructor public class WebMvcConfig extends WebMvcConfigurationSupport { + private final JwtInterceptor jwtInterceptor; + + /** + * 拦截器配置 + * @param registry + */ + protected void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(jwtInterceptor). + //addPathPatterns("/**"). + excludePathPatterns("/**"); + } @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); diff --git a/luoo_tag/src/main/java/com/luoo/tag/interceptor/JwtInterceptor.java b/luoo_tag/src/main/java/com/luoo/tag/interceptor/JwtInterceptor.java new file mode 100644 index 0000000..5ac1e18 --- /dev/null +++ b/luoo_tag/src/main/java/com/luoo/tag/interceptor/JwtInterceptor.java @@ -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(); + } +} diff --git a/luoo_tag/src/main/java/com/luoo/tag/pojo/UserInfo.java b/luoo_tag/src/main/java/com/luoo/tag/pojo/UserInfo.java new file mode 100644 index 0000000..4f4c6d5 --- /dev/null +++ b/luoo_tag/src/main/java/com/luoo/tag/pojo/UserInfo.java @@ -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; +}