1 回答

TA貢獻1799條經驗 獲得超6個贊
您始終可以實現一個RequestMatcher來定義您自定義的 HTTP 請求匹配邏輯。如果匹配器為 HTTP 請求返回真值,它將允許該請求訪問:
public MyRequestMatcher implements RequestMatcher {
boolean matches(HttpServletRequest request){
//Define the matching logic here....
if(request.getHeader("xxx") != null &&
request.getHeader("xxx").equals("yyyy"){
return true;
}
//blablablab
}
}
并配置使用這個匹配器:
httpSecurity.authorizeRequests().requestMatchers(new MyRequestMatcher()).permitAll();
Spring Security 也提供了一些常用的RequestMatcher比如RequestHeaderRequestMatcher和AndRequestMatcher,應該適合你的需求:
//This matches if the request has X-Operation-Name header and its value is forgot-password
RequestHeaderRequestMatcher headerMatcher = new RequestHeaderRequestMatcher("X-Operation-Name","forgot-password" );
// This matches if the request is POST to the /api/website-user
AntPathRequestMatcher antRequestMatcher = new AntPathRequestMatcher("/api/website-user", HttpMethod.POST)
// This matches if both of the above matches matches
AndRequestMatcher andMatcher = new AndRequestMatcher(headerMatcher,antRequestMatcher );
httpSecurity.authorizeRequests().requestMatchers(andMatcher).permitAll();
添加回答
舉報