spring boot配置攔截器代碼實(shí)例
這篇文章主要介紹了spring boot配置攔截器代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
首先引入web模塊的依賴:
復(fù)制代碼
<!-- spring boot web 組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot web 組件 -->
復(fù)制代碼
然后編寫攔截器類:
復(fù)制代碼
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;
/**
* api訪問攔截器
*
* @author Zxz
* @version 1.0
* @date Created at 2018/11/24
**/
@Slf4j
@Component
public class ApiAccessInterceptor extends HandlerInterceptorAdapter {
@Autowired
private CommProp commProp;
/**
* http響應(yīng)類型字段
*/
private static final String RESPONSE_CONTENT_TYPE = "Content-Type";
/**
* http響應(yīng)類型:json
*/
private static final String RESPONSE_HEADER_JSON = "application/json";
/**
* 訪問認(rèn)證攔截
*
* @param request 請(qǐng)求
* @param response 響應(yīng)
* @param handler 數(shù)據(jù)
* @return boolean
* @author Zxz
* @date 2019/10/25 17:34
**/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//方法類型過濾
if (!(handler instanceof HandlerMethod)) {
return super.preHandle(request, response, handler);
}
//token驗(yàn)證
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);
}
/**
* 驗(yàn)證認(rèn)證信息是否可以
*
* @param token token串
* @return boolean
* @author Zxz
* @date 2019/10/25 17:48
**/
private boolean auth(String token) {
return token.equals(commProp.getUserPermanentAuthorization());
}
}
復(fù)制代碼
最后在配置類里面加入攔截器以及要攔截的路徑:
復(fù)制代碼
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;
/**
* web配置
*
* @author zhangxz
* 2019/10/25
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private ApiAccessInterceptor apiAccessInterceptor;
@Autowired
private CommProp commProp;
/**
* 攔截器配置
*
* @param registry 攔截器注冊(cè)類
* @return void
* @author Zxz
* @date 2019/10/25 17:53
**/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(apiAccessInterceptor).addPathPatterns("/" + commProp.getPlayApiVersionLatest() + "/**");
//注意,攔截器配置不能使用配置文件的統(tǒng)一api路徑配置:server.servlet.context-path,這樣配置是無效的。
//只能使用controller里面的具體路徑配置,才能有效攔截
// registry.addInterceptor(apiAccessInterceptor).addPathPatterns("/play/api/**");
}
}
復(fù)制代碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在SpringBoot項(xiàng)目中解決依賴沖突問題的方法
在SpringBoot項(xiàng)目中,依賴沖突是一個(gè)常見的問題,特別是當(dāng)項(xiàng)目引入多個(gè)第三方庫或框架時(shí),依賴沖突可能導(dǎo)致編譯錯(cuò)誤、運(yùn)行時(shí)異?;虿豢深A(yù)測(cè)的行為,本文給大家介紹了如何在SpringBoot項(xiàng)目中解決以來沖突問題的方法,需要的朋友可以參考下2024-01-01java微信開發(fā)API第三步 微信獲取以及保存接口調(diào)用憑證
這篇文章主要為大家詳細(xì)介紹了java微信開發(fā)API第二步,微信獲取以及保存接口調(diào)用憑證,感興趣的小伙伴們可以參考一下2016-06-06Java復(fù)制(拷貝)數(shù)組的4種方法:arraycopy()方法、clone() 方法、copyOf()和copyOfRa
這篇文章主要介紹了Java復(fù)制(拷貝)數(shù)組的4種方法:arraycopy()方法、clone() 方法、copyOf()和copyOfRan,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01springboot執(zhí)行延時(shí)任務(wù)之DelayQueue實(shí)例
這篇文章主要介紹了springboot執(zhí)行延時(shí)任務(wù)之DelayQueue實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02SpringBoot中的FailureAnalyzer使用詳解
這篇文章主要介紹了SpringBoot中的FailureAnalyzer使用詳解,Spring Boot的FailureAnalyzer是一個(gè)接口,它用于在Spring Boot應(yīng)用啟動(dòng)失敗時(shí)提供有關(guān)錯(cuò)誤的詳細(xì)信息,這對(duì)于開發(fā)者來說非常有用,因?yàn)樗梢詭椭覀兛焖僮R(shí)別問題并找到解決方案,需要的朋友可以參考下2023-12-12JAVA實(shí)現(xiàn)簡(jiǎn)單系統(tǒng)登陸注冊(cè)模塊
這篇文章主要介紹了一個(gè)簡(jiǎn)單完整的登陸注冊(cè)模塊的實(shí)現(xiàn)過程,文章條理清晰,在實(shí)現(xiàn)過程中加深了對(duì)相關(guān)概念的理解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-07-07