0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Spring SecurityのRequestMatcherをつかったアクセス制御

Posted at

はじめに

脆弱性の対応などで、特定のrequest(あるヘッダーの値が不正など)を受けつないようにしたい、といったことがあります。Spring SecurityにはRequestMatcherという仕組みがあり、こういったケースに対応しやすいです。その実装例の記事です。

実装例

次のような方針で実装します。

  • 不正なリクエストにマッチするRequestMatcherを実装する
  • それをつかって、SecurityFilterChainで403を返すようにする
@Component
public class IllegalHeaderMatcher implements RequestMatcher {

 @Override
 public boolean matches(HttpServletRequest request) {
     return request.getHeader("sample").contains("bad");
 }

}
@Bean
SecurityFilterChain accessDeny(HttpSecurity http, IllegalHeaderMatcher illegalHeaderMatcher) {
  http.securityMatcher(illegalHeaderMatcher)
         .authorizedHttpRequest(authz -> authz.anyRequest().denyAll());
  return http.build();       
}

RequestMatcherは複数をand/orで組み合わせこともできるので、複雑な条件にも対応しやすく、不正なリクエストの条件をRequestMatcherの形で切り出しやすいです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?