这篇文章主要介绍了spring boot配置拦截器代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
首先引入web模块的依赖:
复制代码
spring-boot-starter-web
复制代码
然后编写拦截器类:
复制代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import simple.proj.zxz.play.comm.GeneralConsts;
import simple.proj.zxz.play.pojo.vo.comm.CommOutVO;
import simple.proj.zxz.play.prop.CommProp;
import simple.proj.zxz.play.utils.JsonUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Slf4j
@Component
public class ApiAccessInterceptor extends HandlerInterceptorAdapter {
@Autowired
private CommProp commProp;
private static final String RESPONSE_CONTENT_TYPE = "Content-Type";
private static final String RESPONSE_HEADER_JSON = "application/json";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//方法类型过滤
if (!(handler instanceof HandlerMethod)) {
return super.preHandle(request, response, handler);
}
//token验证
String token = request.getHeader(GeneralConsts.REQ_HEADER_AUTH);
if (StringUtils.isEmpty(token)) {
//没有token信息,未登录
response.setHeader(RESPONSE_CONTENT_TYPE, RESPONSE_HEADER_JSON);
response.getWriter().write(JsonUtil.toFormattedJsonString(CommOutVO.getNotAuth()));
return false;
} else if (!auth(token)) {
return false;
}
return super.preHandle(request, response, handler);
}
private boolean auth(String token) {
return token.equals(commProp.getUserPermanentAuthorization());
}
}
复制代码
最后在配置类里面加入拦截器以及要拦截的路径:
复制代码
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import simple.proj.zxz.play.interceptors.ApiAccessInterceptor;
import simple.proj.zxz.play.prop.CommProp;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private ApiAccessInterceptor apiAccessInterceptor;
@Autowired
private CommProp commProp;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(apiAccessInterceptor).addPathPatterns("/" + commProp.getPlayApiVersionLatest() + "/**");
//注意,拦截器配置不能使用配置文件的统一api路径配置:server.servlet.context-path,这样配置是无效的。
//只能使用controller里面的具体路径配置,才能有效拦截
// registry.addInterceptor(apiAccessInterceptor).addPathPatterns("/play/api/**");
}
}
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



