springboot中如何指定某些接口不被攔截
1、監(jiān)聽器(Interceptor)攔截處理
在 Spring Boot應用中,如果你希望某些請求地址不被監(jiān)聽器(Interceptor)攔截處理,可以通過配置攔截器的路徑來實現(xiàn)。攔截器通常用于在請求前后進行處理,比如權限驗證、日志記錄等,但有時候你可能希望某些請求可以跳過這些處理。
以下是實現(xiàn)這一目標的一般步驟:
1)定義攔截器:
@Component
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 在此處編寫你的攔截邏輯
// 返回 true 表示繼續(xù)處理請求,返回 false 表示結束請求
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// 在請求處理之后進行處理
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// 在請求完成之后進行處理
}
}
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/ 開頭的路徑,即不對這些路徑應用攔截器邏輯。
2、繞過Spring Security 認證處理
在 Spring Security 中,AuthenticationEntryPoint 主要用于處理未經認證的請求,例如需要登錄但用戶未提供憑證時的處理邏輯。如果你希望某些接口請求不經過 AuthenticationEntryPoint 的認證處理,通??梢酝ㄟ^配置 Spring Security 的 HttpSecurity 來實現(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() // 其他接口路徑需要認證
.and()
.httpBasic()
.authenticationEntryPoint(new MyAuthenticationEntryPoint()); // 設置自定義的認證入口點
}
}
說明:
antMatchers("/public/**").permitAll() 指定了 /public/** 路徑下的接口不需要認證,可以直接訪問。
.anyRequest().authenticated() 告訴 Spring Security 其他所有請求都需要認證。
.httpBasic().authenticationEntryPoint(new MyAuthenticationEntryPoint()) 指定了自定義的 AuthenticationEntryPoint,你可以根據需要進行自定義邏輯,例如返回特定的錯誤信息或跳轉頁面。
2)自定義 AuthenticationEntryPoint:
/**
* 認證失敗處理類 返回未授權
*
* @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("請求訪問:{},認證失敗,無法訪問系統(tǒng)資源", request.getRequestURI());
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(401, msg)));
}
}
到此這篇關于springboot中如何指定某些接口不被攔截的文章就介紹到這了,更多相關springboot指定接口不被攔截內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring boot利用docker構建gradle項目的實現(xiàn)步驟
這篇文章主要給大家介紹了關于spring boot利用docker構建gradle項目的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2018-05-05
使用java代碼實現(xiàn)一個月內不再提醒,通用到期的問題
這篇文章主要介紹了使用java代碼實現(xiàn)一個月內不再提醒,通用到期的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

