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依赖

 

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <optional>true</optional>
</dependency>

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值