springboot中如何指定某些接口不被攔截
1、監(jiān)聽器(Interceptor)攔截處理
在 Spring Boot應(yīng)用中,如果你希望某些請求地址不被監(jiān)聽器(Interceptor)攔截處理,可以通過配置攔截器的路徑來實(shí)現(xiàn)。攔截器通常用于在請求前后進(jìn)行處理,比如權(quán)限驗(yàn)證、日志記錄等,但有時(shí)候你可能希望某些請求可以跳過這些處理。
以下是實(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ù)處理請求,返回 false 表示結(jié)束請求 return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 在請求處理之后進(jìn)行處理 } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 在請求完成之后進(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/**"); // 跳過 /public 下的路徑 } }
addPathPatterns("/**") 表示攔截所有路徑,而 excludePathPatterns("/public/**")
表示跳過以 /public/ 開頭的路徑,即不對這些路徑應(yīng)用攔截器邏輯。
2、繞過Spring Security 認(rèn)證處理
在 Spring Security 中,AuthenticationEntryPoint 主要用于處理未經(jīng)認(rèn)證的請求,例如需要登錄但用戶未提供憑證時(shí)的處理邏輯。如果你希望某些接口請求不經(jīng)過 AuthenticationEntryPoint 的認(rèn)證處理,通常可以通過配置 Spring Security 的 HttpSecurity 來實(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() // 允許訪問的接口路徑 .anyRequest().authenticated() // 其他接口路徑需要認(rèn)證 .and() .httpBasic() .authenticationEntryPoint(new MyAuthenticationEntryPoint()); // 設(shè)置自定義的認(rèn)證入口點(diǎn) } }
說明:
antMatchers("/public/**").permitAll() 指定了 /public/** 路徑下的接口不需要認(rèn)證,可以直接訪問。
.anyRequest().authenticated() 告訴 Spring Security 其他所有請求都需要認(rèn)證。
.httpBasic().authenticationEntryPoint(new MyAuthenticationEntryPoint()) 指定了自定義的 AuthenticationEntryPoint,你可以根據(jù)需要進(jìn)行自定義邏輯,例如返回特定的錯(cuò)誤信息或跳轉(zhuǎn)頁面。
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("請求訪問:{},認(rèn)證失敗,無法訪問系統(tǒng)資源", request.getRequestURI()); ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(401, msg))); } }
到此這篇關(guān)于springboot中如何指定某些接口不被攔截的文章就介紹到這了,更多相關(guān)springboot指定接口不被攔截內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis?在使用上的注意事項(xiàng)及其辨析(最新最全整理)
這篇文章主要介紹了MyBatis的在使用上的注意事項(xiàng)及其辨析,本文內(nèi)容比較長,是小編用心給大家整理的,圖文實(shí)例代碼相結(jié)合給大家講解的非常詳細(xì),需要的朋友參考下吧2024-06-06解決springboot生成bean名稱沖突(AnnotationBeanNameGenerator)
這篇文章主要介紹了解決springboot生成bean名稱沖突(AnnotationBeanNameGenerator),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java實(shí)戰(zhàn)之利用POI生成Excel圖表
Apache POI是Java生態(tài)中處理Office文檔的核心工具,這篇文章主要為大家詳細(xì)介紹了如何在Excel中創(chuàng)建折線圖,柱狀圖,餅圖等常見圖表,需要的可以參考下2025-02-02使用java將動(dòng)態(tài)網(wǎng)頁生成靜態(tài)網(wǎng)頁示例
這篇文章主要介紹了使用java將動(dòng)態(tài)網(wǎng)頁生成靜態(tài)網(wǎng)頁示例,需要的朋友可以參考下2014-03-03