2017-02-21 5 views
3

カスタム注釈付きの独立したパッケージ可能なjarを作成しようとしていますが、これはコントローラのマッピング関数にインクルードすると、ユーザの入力としてuserTokenを取ります。認証されているか、または今です。カスタム注釈を作成してヘッダからuserTokenを検証する

// Expected way of inclusion 
public @ResponseBody boolean isAuthenticated(@Authenticator(@RequestHeader("userToken")) Boolean isUserAuthenticated) { 
return isUserAuthenticated; 
} 

私はこのコードを使用するとRequestMappingはStringに変換することはできません(と注釈が唯一のプリミティブ値を受け入れる)エラーが発生しますので、これは、右の構文ではないだろうことを知っています。

私は他のアプローチにもオープンしていますが、必要に応じてグローバルブロッキングではなく、認証ブール値を返す柔軟性が必要です。

重要::@Authenticatorは、Mavenを介して現在のパッケージにインポートされた独立したパッケージに由来します。 HTTPServletRequestはConstraintValidatorを渡しますか?

+1

public class MyBasicAuthenticationFilter extends BasicAuthenticationFilter { private AuthenticationManager authenticationManager; public MyBasicAuthenticationFilter(AuthenticationManager authenticationManager) { super(authenticationManager); this.authenticationManager=authenticationManager; } @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { // do you checks here super.doFilterInternal(request, response, chain); } } 

を次にようなものを使用して、セキュリティの設定にこれを追加します。 –

+0

ここで、Springセキュリティのどのモジュールを再利用できるかを記述してください。さらに、私は軽いパッケージが欲しかったので、あまりにも多くの依存関係が含まれていませんでした。 –

+0

私は答えを加えました。 –

答えて

0

は春のセキュリティBasicAuthenticationFilter使用:私はあなたが春のセキュリティを書き換えしようとしていると思います

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .authorizeRequests() 
       .antMatchers("/", "/home").permitAll() 
       .anyRequest().authenticated() 
       .and() 
       .formLogin() 
       .loginPage("/login") 
       .permitAll() 
       .and() 
       .logout() 
       .permitAll(); 

     http.addFilterBefore(new MyBasicAuthenticationFilter(authenticationManager()); 
    } 

    @Bean 
    public AuthenticationManager authenticationManager() { 
     return new MyAuthenticationManager(); 
    } 
+0

助けてくれてありがとう、しかしこれは私が必要なものです。私のパッケージは他のプロジェクトにも含まれていて、注釈は単にuserTokenを受け入れて検証し、問題がある場合はエラーを投げてください。 –

関連する問題