栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot配置拦截器代码实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot配置拦截器代码实例

这篇文章主要介绍了spring boot配置拦截器代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

首先引入web模块的依赖:

复制代码


org.springframework.boot
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/**");

}
}
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/136283.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号