2017-12-10 11 views
0

JSONログインペイロードを受け入れるようにSpring Securityを設定しようとしています。しかし、UsernamePasswordAuthenticationFilterをカスタムフィルタとして使用した場合の副作用は、代わりにSpringセキュリティがAuthenticationSuccessHandlerの代わりに、ルートページ(デフォルトのスプリングセキュリティコンフィグレーション)へのスプリングリダイレクト要求に従わないことがあります。UsernamePasswordAuthenticationFilter AuthenticationSuccessHandlerを無視します。

注:私は設定から​​addFilterAt、削除してログインするFORM URL ENCODEDを使用する場合は、呼び出すことができます

WebSecurityConfigurer

http 
     .authorizeRequests() 
     .antMatchers(permitURI).permitAll() 
     .anyRequest().authenticated() 
     .and() 
     .addFilterAt(new JSONAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class) 
     .formLogin().loginProcessingUrl("/login") 
     .successHandler(authSuccess).failureHandler(authFailure).permitAll() 
     .and() 
     .exceptionHandling().authenticationEntryPoint(authEntry) 
     .and() 
     .rememberMe().rememberMeCookieName("AUTOLOGIN") 
     .and() 
     .cors() 
     .and() 
     .logout().logoutUrl("/logout").permitAll() 
     .clearAuthentication(true) 
     .invalidateHttpSession(true) 
     .deleteCookies("JSESSIONID", "AUTOLOGIN") 
     .logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()) 
     .permitAll(); 

http.csrf().disable(); 

UsernamePasswordAuthenticationFilter

@Override 
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { 
     if (!request.getMethod().equals("POST")) { 
      throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod()); 
     } 

     String payload; 
     try { 
      ObjectMapper mapper = new ObjectMapper(); 
      payload = IOUtils.toString(request.getInputStream(), Charset.defaultCharset()); 
      JsonAuthenticationParser auth = mapper.readValue(payload, JsonAuthenticationParser.class); 

      UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(auth.getUsername(), auth.getPassword()); 

      return authRequest; 
     } catch (IOException e) { 
      throw new InternalAuthenticationServiceException(e.getMessage(), e); 
     } 
    } 

    static class JsonAuthenticationParser { 
     private final String username; 
     private final String password; 

     @JsonCreator 
     public JsonAuthenticationParser(@JsonProperty("username") String username, @JsonProperty("password") String password) { 
      this.username = username; 
      this.password = password; 
     } 

     public String getUsername() { 
      return username; 
     } 

     public String getPassword() { 
      return password; 
     } 
    } 

答えて

0

予想通り、それはAuthenticationSuccessHandlerに従いますあなたの認証をカスタマイズする方法setAuthenticationSuccessHandlerセスアクション。 setAuthenticationSuccessHandler()は、抽象クラスAbstractAuthenticationProcessingFilterで定義されています。

+0

AbstractAuthenticationProcessingFilter'は、メソッドのトンを実装する必要が '拡張、これを行うための簡単な方法はありますか?おそらく既存の 'successHandler'(' SimpleUrlAuthenticationSuccessHandler')などにリンクしていますか? – XPLOT1ON

+0

UsernamePasswordAuthenticationFilterはすでにAbstractAuthenticationProcessingFilterを継承しています。 UsernamePasswordAuthenticationFilterを既に使用している場合、setAuthenticationSuccessHandlerをカスタムsuccessHandlerに呼び出すことができます。もちろん、必要に応じてSimpleUrlAuthenticationSuccessHandlerを使用することもできます。 – Persia

+0

@ XPLOT1ON 'AbstractAuthenticationProcessingFilter'を拡張するには、あなたがすでに持っているメソッドを実装する必要があります。 –

0

JsonAuthenticationFilter私にはうまく見えます(私はそれがAbstractAuthenticationProcessingFilterを拡張すると仮定します)。

WebConfig次試してください:あなたは、あなたのLoginSuccessHandler直接JsonAuthenticationFilterに追加する必要があります

@Bean 
public JsonAuthenticationFilter authenticationFilter() throws Exception { 
    JsonAuthenticationFilter filter = new JsonAuthenticationFilter(); 
    filter.setAuthenticationManager(authenticationManagerBean()); 
    filter.setAuthenticationSuccessHandler(new MyLoginSuccessHandler()); 
    return filter; 
} 

BasicAuthenticationFilter前に、あなたのJsonauthenticationFilterを追加します。

protected void configure(HttpSecurity httpSecurity) throws Exception { 
    httpSecurity.addFilterBefore(authenticationFilter(), BasicAuthenticationFilter.class); 
    ... 
} 
+0

あなたが言ったように、私は 'IllegalArgumentException:authenticationManagerを指定する必要があります'を取得しました。私はあなたの 'BasicAuthenticationFilter.class'を' UsernamePasswordAuthenticationFilter.class'に変更しました。 – XPLOT1ON

+0

待機すると、上記の例外はもうスローされません。しかし、それは 'AuthenticationProvider'でユーザを認証しません。それは 'successHandler'にまっすぐに行きました – XPLOT1ON

関連する問題