feat: add jwt interceptor

main
itao 10 months ago
parent eb7d4a37e5
commit d390e45f14

@ -16,7 +16,9 @@ import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import util.IdWorker; import util.IdWorker;
@SpringBootApplication(scanBasePackages = "com.luoo") import util.JwtUtil;
@SpringBootApplication(scanBasePackages = "com.luoo.tag")
@EnableEurekaClient @EnableEurekaClient
@EnableDiscoveryClient @EnableDiscoveryClient
@EnableFeignClients @EnableFeignClients
@ -46,5 +48,10 @@ public class TagApplication {
public IdWorker idWorker(){ public IdWorker idWorker(){
return new IdWorker(1, 1); return new IdWorker(1, 1);
} }
@Bean
public JwtUtil jwtUtil(){
return new JwtUtil();
}
} }

@ -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<UserInfo> 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();
}
}

@ -1,11 +1,26 @@
package com.luoo.tag.config; package com.luoo.tag.config;
import com.luoo.tag.interceptor.JwtInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration; 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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration @Configuration
@RequiredArgsConstructor
public class WebMvcConfig extends WebMvcConfigurationSupport { public class WebMvcConfig extends WebMvcConfigurationSupport {
private final JwtInterceptor jwtInterceptor;
/**
*
* @param registry
*/
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtInterceptor).
//addPathPatterns("/**").
excludePathPatterns("/**");
}
@Override @Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) { protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");

@ -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…
Cancel
Save