springboot中如何指定某些接口不被攔截
1、監(jiān)聽(tīng)器(Interceptor)攔截處理
在 Spring Boot應(yīng)用中,如果你希望某些請(qǐng)求地址不被監(jiān)聽(tīng)器(Interceptor)攔截處理,可以通過(guò)配置攔截器的路徑來(lái)實(shí)現(xiàn)。攔截器通常用于在請(qǐng)求前后進(jìn)行處理,比如權(quán)限驗(yàn)證、日志記錄等,但有時(shí)候你可能希望某些請(qǐng)求可以跳過(guò)這些處理。
以下是實(shí)現(xiàn)這一目標(biāo)的一般步驟:
1)定義攔截器:
@Component
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 在此處編寫你的攔截邏輯
// 返回 true 表示繼續(xù)處理請(qǐng)求,返回 false 表示結(jié)束請(qǐng)求
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// 在請(qǐng)求處理之后進(jìn)行處理
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// 在請(qǐng)求完成之后進(jìn)行處理
}
}
2)配置攔截器:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private MyInterceptor myInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor)
.addPathPatterns("/**") // 攔截所有路徑
.excludePathPatterns("/public/**"); // 跳過(guò) /public 下的路徑
}
}
addPathPatterns("/**") 表示攔截所有路徑,而 excludePathPatterns("/public/**")
表示跳過(guò)以 /public/ 開(kāi)頭的路徑,即不對(duì)這些路徑應(yīng)用攔截器邏輯。
2、繞過(guò)Spring Security 認(rèn)證處理
在 Spring Security 中,AuthenticationEntryPoint 主要用于處理未經(jīng)認(rèn)證的請(qǐng)求,例如需要登錄但用戶未提供憑證時(shí)的處理邏輯。如果你希望某些接口請(qǐng)求不經(jīng)過(guò) AuthenticationEntryPoint 的認(rèn)證處理,通??梢酝ㄟ^(guò)配置 Spring Security 的 HttpSecurity 來(lái)實(shí)現(xiàn)。
1)配置類中定義 HTTP Security:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/public/**").permitAll() // 允許訪問(wèn)的接口路徑
.anyRequest().authenticated() // 其他接口路徑需要認(rèn)證
.and()
.httpBasic()
.authenticationEntryPoint(new MyAuthenticationEntryPoint()); // 設(shè)置自定義的認(rèn)證入口點(diǎn)
}
}
說(shuō)明:
antMatchers("/public/**").permitAll() 指定了 /public/** 路徑下的接口不需要認(rèn)證,可以直接訪問(wèn)。
.anyRequest().authenticated() 告訴 Spring Security 其他所有請(qǐng)求都需要認(rèn)證。
.httpBasic().authenticationEntryPoint(new MyAuthenticationEntryPoint()) 指定了自定義的 AuthenticationEntryPoint,你可以根據(jù)需要進(jìn)行自定義邏輯,例如返回特定的錯(cuò)誤信息或跳轉(zhuǎn)頁(yè)面。
2)自定義 AuthenticationEntryPoint:
/**
* 認(rèn)證失敗處理類 返回未授權(quán)
*
* @author dongxiajun
*/
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {
private static final long serialVersionUID = -8970718410437077606L;
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {
String msg = StringUtils.format("請(qǐng)求訪問(wèn):{},認(rèn)證失敗,無(wú)法訪問(wèn)系統(tǒng)資源", request.getRequestURI());
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(401, msg)));
}
}
到此這篇關(guān)于springboot中如何指定某些接口不被攔截的文章就介紹到這了,更多相關(guān)springboot指定接口不被攔截內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于JavaMail實(shí)現(xiàn)郵件發(fā)送
這篇文章主要為大家詳細(xì)介紹了基于JavaMail實(shí)現(xiàn)郵件發(fā)送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
spring boot利用docker構(gòu)建gradle項(xiàng)目的實(shí)現(xiàn)步驟
這篇文章主要給大家介紹了關(guān)于spring boot利用docker構(gòu)建gradle項(xiàng)目的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
SpringBoot優(yōu)雅捕捉異常的兩種方法小結(jié)
SpringBoot框架對(duì)異常的處理提供了幾種很強(qiáng)大的方法,我們可以通過(guò)@ControllerAdvice和@ExceptionHandler注解實(shí)現(xiàn)全局異常的處理,下面就來(lái)介紹一下這兩種方法的實(shí)現(xiàn),感興趣的可以了解一下2024-08-08
IDEA報(bào)錯(cuò):java:無(wú)效的源發(fā)行版21解決方式
這篇文章主要給大家介紹了關(guān)于IDEA報(bào)錯(cuò):java:無(wú)效的源發(fā)行版21的解決方式,這個(gè)錯(cuò)誤是因?yàn)槟愕捻?xiàng)目使用的Java版本與你的IDEA使用的Java版本不一致導(dǎo)致的,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
使用java代碼實(shí)現(xiàn)一個(gè)月內(nèi)不再提醒,通用到期的問(wèn)題
這篇文章主要介紹了使用java代碼實(shí)現(xiàn)一個(gè)月內(nèi)不再提醒,通用到期的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
解決idea2020.1找不到程序包和符號(hào)的問(wèn)題
這篇文章主要介紹了解決idea2020.1找不到程序包和符號(hào)的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
SpringBoot設(shè)置HTTP代理訪問(wèn)
本文主要介紹了SpringBoot在私有服務(wù)器通過(guò)代理訪問(wèn)公網(wǎng)的配置方法,涉及WebClient和RestTemplate兩種客戶端的代理設(shè)置,幫助實(shí)現(xiàn)跨服務(wù)器網(wǎng)絡(luò)請(qǐng)求2025-06-06

