私が達成しようとしているのは、残りのAPIのjwtトークンベースの認証です。 /apiの下にあるものは、すべてトークンでアクセス可能である必要があります。春のセキュリティ認証フィルタのURLが無視されました
私は私のウェブセキュリティ設定で、以下のconfigureメソッドがあります。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authenticationProvider(authenticationProvider())
.csrf().disable()
.authorizeRequests().antMatchers("/api/**").authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint())
.and()
.addFilterBefore(authenticationFilter(),BasicAuthenticationFilter.class)
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
をそして、これはフィルタです:
public class JwtAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
public JwtAuthenticationFilter(AuthenticationManager manager) {
super("/api/**");
this.setAuthenticationManager(manager);
}
@Override
protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) {
return true;
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
String header = request.getHeader("Authorization");
if (header == null || !header.startsWith("Bearer ")) {
throw new JwtTokenMissingException("No JWT token found in request headers");
}
String authToken = header.substring(7);
JwtAuthenticationToken authRequest = new JwtAuthenticationToken(authToken);
return getAuthenticationManager().authenticate(authRequest);
}
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult)
throws IOException, ServletException {
super.successfulAuthentication(request, response, chain, authResult);
chain.doFilter(request, response);
}
}
私の問題は、フィルタは、現在すべてのURLに適用されることではありません/ api接頭辞付きのものだけです。
私の "configure"メソッドが間違っているかもしれないという事実を知っていますが、どのように見えますか?達成したいのは、/ apiパスにフィルタを使用することだけです。
+1質問:フィルタを適用するパスを設定する2つの値は何ですか? (設定でantMatchersメソッドのパラメータとして1回、次にAbstractAuthenticationProcessingFilterのコンストラクタ引数 "filterProcessesUrl")を呼び出します。これらの値はどういった関係にあり、どちらを使うべきですか?
'.addFilterBefore'の前に' .antMatcher( "/ api/**") 'を追加すると、問題が解決されると思います。 – MNos