2017-04-19 9 views
0

私はSpring Securityを使用してアプリケーションを作成しています。 カスタムUserDetails、UserDetailsS​​ervice、AccessDecisionVoter、およびWebSecurityConfigurerAdapterを実装しました。 許可されていないユーザーが/authenthication/loginページにアクセスしてログインすることを許可したいが、ページへの他のすべてのアクセスは、カスタムAccessDecisionVoterによって処理される必要がある。
マイカスタムWebSecurityConfigurerAdapterクラスには、次のようになります。私は私のデータベースにいくつかの役割を定義しているSpring Security:認証ページpermitAllは無視されるようです。

@Configuration 
@EnableWebSecurity 
@ComponentScan(value = "security") 
public class Configuration extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private UserDetailsServiceImpl userDetailsServiceImpl; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
       .antMatchers("/authentication/login**") 
       .permitAll() 
       .and() 
      .authorizeRequests() 
       .anyRequest() 
       .authenticated() 
       .accessDecisionManager(accessDecisionManager()) 
     ; 
    } 


    @Bean 
    public AccessDecisionManager accessDecisionManager() { 
     List<AccessDecisionVoter<? extends Object>> decisionVoters = 
       Arrays.asList(new AccessDecisionVoterImpl()); 
     return new UnanimousBased(decisionVoters); 
    } 


    @Override 
    protected void configure(AuthenticationManagerBuilder auth) { 
     try { 
      auth.userDetailsService(userDetailsServiceImpl); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 


    @Bean 
    public UserDetailsServiceImpl userDetailsService() { 
     return userDetailsServiceImpl; 
    } 

} 

。私のカスタムAccessDecisionVoterのvoteメソッドは、ログインしているユーザーのアクセス権を取得し、アクセス権とアクセス権をURL + httpMethodに基づいて拒否します。

問題:
しかし、私は私のコードは私のカスタムAccessDecisionVoterでNullPointerExceptionが与えユーザー名とパスワード/認証/ログインにPOSTを送信するとき:ユーザー名(authentication.getPrincipal();リターンになりanonymousUser、経由で取得します後でコード内のNullPointer。しかし、構成ファイルがSpringにpermittAllへのアクセスに/ authentication/loginにアクセスしたので、なぜvoteメソッドが呼び出されたのか分かりません。

答えて

0

私は、すべての要求が許可されるようにします(認証されていない要求も許可します)が、その要求のセキュリティチェーンを無効にしません。それでも、tを処理しようとしますそれらすべてを覆っている。すべてのセキュリティをバイパスしたいのであれば、私は通常webSecurity設定メソッドをオーバーライドして例外のケースを追加します。

@Override 
public void configure(WebSecurity webSecurity) throws Exception 
{ 
    webSecurity 
     .ignoring() 
     .antMatchers("/authentication/login**"); 
} 
+0

ありがとうございます。これは本当に私の問題を解決しました。しかし、別の..露出 – Matthias

関連する問題