Spring boot 拦截器

拦截器写法:

1.定义拦截器

2.注册拦截器

3.conf加入扫描



1.定义拦截器

其实呢就是实现下面这个接口啦~ 不过有封装好的抽象类 那就用喽

 AsyncHandlerInterceptor 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.baidu.bce.plat.webframework.exception.BceException;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Component
public class AuthInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        log.debug("[CHECK internal AUTH AOP] begin");
        checkSign(request);
        setUserId(request);
        log.debug("[CHECK internal AUTH AOP] end");
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                                Exception ex) throws Exception {
        UserIdHolder.removeAuthorizedUserId();
    }

    private void checkSign(HttpServletRequest request) {
        // 做写业务
    }

    private void setUserId(HttpServletRequest request) {
        String userId = request.getHeader("user_id"); // 看你的需求
      
        UserIdHolder.setAuthorizedUserId(userId);  // 这个是ThreadLocal
    }
}

2.注册

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import lombok.extern.slf4j.Slf4j;

@Configuration
@ConditionalOnWebApplication
@ConditionalOnExpression("${auth.enable:false}")
@Slf4j
public class InternalAuthConfig {
    @Configuration
    public static class InternalAuthInterceptorConfigureAdapter extends WebMvcConfigurerAdapter {
        @Value("${auth.interceptor.path:/**}")
        private String path;

        @Value("${auth.interceptor.exclude.path:}")
        private String excludePath;

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            log.info("add AuthInterceptorAdapter");
            AuthInterceptor interceptor = new AuthInterceptor();
            registry.addInterceptor(interceptor)
                    .addPathPatterns(path)
                    .excludePathPatterns(excludePath.split(";"));
        }
    }
}


3.加入spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.test.auth.AuthConfig

路径类似于这个:




maven依赖


    org.springframework
    spring-webmvc
    true



你可能感兴趣的:(spring,boot)