使用拦截器处理通用的Http Header

在Web项目中如果我们的后台接口有一套通用的校验规则,假设校验规则放置在Http的Header中,那么我们可以通过ThreadLocal配合拦截器来完成通用属性信息的保存

以便后续处理中的使用

例子:规定所有的Http头中必须携带 User-Id 与 Token 来验证用户身份,采用在拦截器中间这两个信息绑定到ThreadLocal,以供后续方法使用

代码

1.声明 上下文

public class RequestHeaderContext {

    private static final ThreadLocal REQUEST_HEADER_CONTEXT_THREAD_LOCAL=new ThreadLocal<>();
    private String userId;
    private String token;

    public String getUserId() {
        return userId;
    }

    public String getToken() {
        return token;
    }

    public static RequestHeaderContext getInstance(){
        return REQUEST_HEADER_CONTEXT_THREAD_LOCAL.get();
    }

    public void setContext(RequestHeaderContext context){
        REQUEST_HEADER_CONTEXT_THREAD_LOCAL.set(context);
    }

    public static void clean(){
        REQUEST_HEADER_CONTEXT_THREAD_LOCAL.remove();
    }

    private RequestHeaderContext(RequestHeaderContextBuild requestHeaderContextBuild){
        this.userId=requestHeaderContextBuild.userId;
        this.token=requestHeaderContextBuild.token;
        setContext(this);
    }

    public static class RequestHeaderContextBuild{
        private String userId;
        private String token;

        public RequestHeaderContextBuild userId(String userId){
            this.userId=userId;
            return this;
        }

        public RequestHeaderContextBuild token(String token){
            this.token=token;
            return this;
        }


        public RequestHeaderContext bulid(){
            return new RequestHeaderContext(this);
        }
    }
}

2.声明拦截器

public class RequestHeaderContextInterceptor extends HandlerInterceptorAdapter{

    private static final String HEAD_USER_ID = "User-Id";
    private static final String HEAD_TOKEN = "Token";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        initHeaderContext(request);
        return super.preHandle(request, response, handler);
    }

    private void initHeaderContext(HttpServletRequest request){
        new RequestHeaderContext.RequestHeaderContextBuild()
                .token(request.getHeader(HEAD_TOKEN))
                .userId(request.getHeader(HEAD_USER_ID))
                .bulid();
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        RequestHeaderContext.clean();
        super.postHandle(request, response, handler, modelAndView);
    }
}

3.绑定拦截器

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Bean
    public RequestHeaderContextInterceptor requestHeaderContextInterceptor() {
        return new RequestHeaderContextInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestHeaderContextInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}
4.编写测试controller

@Controller
@RequestMapping("h1")
public class TestController {

    @RequestMapping(value = "h2")
    public @ResponseBody Object testHeaderContext() {
        return RequestHeaderContext.getInstance().getUserId();
    }
}




你可能感兴趣的:(使用拦截器处理通用的Http Header)