Spring boot 拦截器
2018-02-12 18:44
295 查看
拦截器写法:
1.定义拦截器
2.注册拦截器
3.conf加入扫描
1.定义拦截器
其实呢就是实现下面这个接口啦~ 不过有封装好的抽象类 那就用喽 AsyncHandlerInterceptor
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.factoriesorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.test.auth.AuthConfig路径类似于这个:
maven依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<optional>true</optional>
</dependency>
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.factoriesorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.test.auth.AuthConfig路径类似于这个:
maven依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<optional>true</optional>
</dependency>
相关文章推荐
- SpringBoot--实现拦截器
- 详解SpringBoot AOP 拦截器(Aspect注解方式)
- 【第四十章】Spring Boot 自定义拦截器
- spring boot 中使用spring mvc interceptor(拦截器)判断操作权限
- Spring boot之拦截器的实现
- spring-boot配置拦截器
- Spring Boot 拦截器
- SpringBoot拦截器中service或者redis注入为空的问题
- 分享知识-快乐自己:SpringBoot结合使用拦截器(判断是否用户是否已登陆)
- springboot 拦截器注入不进service
- (22)Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
- spring boot 拦截器
- SpringBoot之拦截器
- [置顶] 一步一步学springboot (六)集成拦截器和过滤器及监听器
- Spring Boot 拦截器
- spring boot 添加 拦截器
- Spring Boot干货:静态资源和拦截器处理
- spring boot 拦截器中无法注入serivce
- spring boot LoginInterceptor(登录拦截器)无法注入bean