2016-06-24 6 views
0

一見すると、JWTを使用して安全なデータ転送を行うAPIバックエンドアプリケーションがSpringブートで作成されています。認証用に3番目のパラメータを追加したいので、login、password、storeIDの各パラメータが必要です。私はこの回答How implement Spring security when login page having more field apart from user name and password?に触発されていますが、私が提案した解決策に従うと、私の第3パラメータは使用されませんでした。私の印象は、セキュリティ設定で何か重要なものがないことです。私の間違いを指摘できますか?Spring - 認証時に3番目のパラメータのチェックを追加します

SecurityConfig

@SuppressWarnings("SpringJavaAutowiringInspection") 
@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private JwtAuthenticationEntryPoint unauthorizedHandler; 

    @Autowired 
    private UserDetailsService userDetailsService; 

    @Autowired 
    private AuthenticationDetailsSource<HttpServletRequest, ?> webAuthenticationDetailsSourceImpl; 

    @Autowired 
    public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { 
     authenticationManagerBuilder 
     .authenticationProvider(myAuthProvider()); 
    } 

    @Bean 
    public CustomUserDetailsAuthenticationProvider myAuthProvider() throws Exception { 
     CustomUserDetailsAuthenticationProvider provider = new CustomUserDetailsAuthenticationProvider(); 
     provider.setPasswordEncoder(passwordEncoder()); 
     provider.setUserDetailsService(userDetailsService); 

     return provider; 
    } 

    @Bean 
    public UsernamePasswordAuthenticationFilter usernamePasswordAuthenticationFilter() throws Exception { 
     UsernamePasswordAuthenticationFilter usernamePasswordAuthenticationFilter = new UsernamePasswordAuthenticationFilter(); 
usernamePasswordAuthenticationFilter.setAuthenticationManager(authenticationManager()); 
     usernamePasswordAuthenticationFilter.setAuthenticationDetailsSource(webAuthenticationDetailsSourceImpl); 

     return usernamePasswordAuthenticationFilter; 
    } 

    @Bean 
    public PasswordEncoder passwordEncoder() { 
     return new BCryptPasswordEncoder(); 
    } 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

    @Bean 
    public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception { 
     JwtAuthenticationTokenFilter authenticationTokenFilter = new JwtAuthenticationTokenFilter(); 
    authenticationTokenFilter.setAuthenticationManager(authenticationManagerBean()); 

     return authenticationTokenFilter; 
    } 

    @Override 
    protected void configure(HttpSecurity httpSecurity) throws Exception { 
     httpSecurity 
       // we don't need CSRF because our token is invulnerable 
       .csrf().disable() 
       .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() 

       // don't create session 
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() 
       .authorizeRequests() 
       // allow anonymous resource requests 
       .antMatchers(
         HttpMethod.GET, 
         "/", 
         "/*.html", 
         "/favicon.ico", 
         "/**/*.html", 
         "/**/*.css", 
         "/**/*.js" 
       ).permitAll() 
       .antMatchers("/auth/**").permitAll() 
       .anyRequest().authenticated(); 

     // Custom JWT based security filter 
     httpSecurity 
       .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class); 
     // disable page caching 
     httpSecurity.headers().cacheControl(); 
    } 
} 

私はWebAuthenticationDetailsS​​ourceImplにstoreIDフィールドに対してチェックすることができた印象の下にあったが、私は、ログの関連は何も表示されていないので、それが実行されていないように見えます。

WebAuthenticationDetailsS​​ourceImpl:

@Component 
public class WebAuthenticationDetailsSourceImpl implements AuthenticationDetailsSource<HttpServletRequest, JwtAuthenticationRequest> { 

    @Override 
    public JwtAuthenticationRequest buildDetails(HttpServletRequest context) { 
     System.out.println("___#####_____"); 
     System.out.println(context); 
     System.out.println("___#####_____"); 
     return new JwtAuthenticationRequest(); 
    } 
} 

答えて

0

あなたcuzの春のセキュリティの認証フィルタチェインにwebAuthenticationDetailsS​​ourceImplを設定し、 "あなた" usernamePasswordAuthenticationFilterを挿入しないでください。追加usernamePasswordAuthenticationFilter "あなた" にあなたの追加のパラメータを取得する場合

おそらく現在のあなたの認証フィルタチェーンは、それゆえ


JwtAuthenticationTokenFilter
(春のセキュリティのオリジナル)UsernamePasswordAuthenticationFilter

ですこのフィルタもJwtAuthenticationTokenFilterのようになります

単に私がトークンを検証し、それが有効でない場合は/あなたは私はおそらくに持っていたようなので、私は `` chain.doFilter(要求、応答)を行う存在JwtAuthenticationTokenFilterであっ

@Bean 
public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception { 
    JwtAuthenticationTokenFilter authenticationTokenFilter = new JwtAuthenticationTokenFilter(); 
    authenticationTokenFilter.setAuthenticationManager(authenticationManagerBean()); 
    authenticationTokenFilter.setAuthenticationDetailsSource(webAuthenticationDetailsSourceImpl); 
    return authenticationTokenFilter; 
} 
+0

でJwtAuthenticationTokenFilter 使用setAuthenticationDetailsS​​ourceでパラメータを取得"私のusernamePasswordAuthenticationFilter"を追加してください。 –

+0

'@Bean public UsernamePasswordStoreAuthenticationFilter authenticationTokenFilterBean2()は例外をスローします{ UsernamePasswordStoreAuthenticationFilter authenticationTokenFilter = new UsernamePasswordStoreAuthenticationFilter(); authenticationTokenFilter.setAuthenticationManager(authenticationManagerBean()); authenticationTokenFilter.setAuthenticationDetailsS​​ource(webAuthenticationDetailsS​​ourceImpl); return authenticationTokenFilter; } ' –

+0

また修飾構成()メソッド' //カスタムJWTベースのセキュリティ・フィルタ httpSecurity .addFilterBefore(authenticationTokenFilterBean()、UsernamePasswordAuthenticationFilter.class) .addFilterBefore(authenticationTokenFilterBean2()、UsernamePasswordAuthenticationFilter.class); 'しかしWebAuthenticationDetailsS​​ourceImplを有しますneveが呼び出されました。 –

関連する問題