私はSpring Securityを使用してアプリケーションを作成しています。 カスタムUserDetails、UserDetailsService、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
メソッドが呼び出されたのか分かりません。
ありがとうございます。これは本当に私の問題を解決しました。しかし、別の..露出 – Matthias