2017-11-28 20 views
0

私はエンドポイントに春のJWTセキュリティを実装する必要があります。私は2つのルートを持っています - 内部は1、外部は2です。私は以下のコードを追加しようとしましたが、両方のフィルタがすべての要求に対して実行されています。 URLに基​​づいてフィルタにロジックを追加できます。しかし、私は正しいアプローチをしていませんでした。適切なアプローチと解決方法について教えてください。スプリングブート複数のJWTを使用する複数のルート

http 
    .csrf().disable() 
    .authorizeRequests() 
    .antMatchers("/internal/**") 
     .authenticated() 
    .and() 
    .addFilterBefore(jwtAuthenticationInternalFilter(), BasicAuthenticationFilter.class) 
    .authorizeRequests() 
    .antMatchers("/external/**") 
    .authenticated() 
    .and() 
    .addFilterBefore(jwtAuthenticationExternalFilter(), BasicAuthenticationFilter.class); 



public class ExternalAuthenticationFilter extends OncePerRequestFilter { 

    @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 
     System.out.println("Its hitting here - External");//GET THE Information and build Authentication object.. 


     // SecurityContextHolder.getContext().setAuthentication(token); 
     filterChain.doFilter(request, response); 
    } 

} 


public class InternalAuthenticationFilter extends OncePerRequestFilter { 

    @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 
     System.out.println("Its hitting here - Internal");//GET THE Information and build Authentication object.. 


     // SecurityContextHolder.getContext().setAuthentication(token); 
     filterChain.doFilter(request, response); 
    } 

} 

任意の要求に対して内部コードと外部コードの両方が実行されています。 サンプル要求 /内部/ ABC、
/外部/ XYZ ..両方のケースの両方のフィルタが呼び出されている。..

次の2つの異なるコンフィギュレーションクラスにセキュリティ設定を分割し、それらをマークすることができ

答えて

0

を提案して下さい例えば@Order(1)@Order(2)注釈。一つのconfigが/internalエンドポイントと/externalと1に対処します。 configure(HttpSecurity http)メソッドでは、最初に構成するエンドポイントを指定してから、設定を適用します。

1コンフィグ怒鳴るの例を参照してください、二設定はanological次のようになります。

@EnableWebSecurity 
@Order(1) 
public class ExternalEndpointsSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .antMatcher("/internal/**") 
      .authorizeRequests() 
      .authenticated() 
      .and() 
      .addFilterBefore(jwtAuthenticationInternalFilter(), BasicAuthenticationFilter.class) 
    } 
} 
+0

ザッツはまさに私が -1 投票 ダウン受け入れる..下のリンクで見つけ https://github.com /spring-projects/spring-security-javaconfig/blob/master/samples-web.md#sample-multi-http-web-configuration ..私は自分に答えましたが、誰かがそれを削除しました – KNS

関連する問題