2017-04-14 1 views
1

は春のセキュリティを通って行くのAPIに私を聞かせて場合によっては、私がADMINを削除すると、HasAnyAuthorityいつも私はメソッドを作成し

@PreAuthorize("hasAnyAuthority('ADMIN', 'CLIENT')") 

とコメント:

.antMatchers("/api/**").hasAnyAuthority(AuthoritiesConstants.CLIENT, AuthoritiesConstants.ADMIN, AuthoritiesConstants.WORKER) 

それは常には、APIに私を聞かせて。私はいくつかの特権をつくるといつでもうまくいくと思いました。上記の例で私のSpring Securityがうまく動作しないのはなぜですか?

UPDATE: answearはあなたが追加する必要が注釈PreAuthorizeを使用して有効にすることです: @EnableGlobalMethodSecurity(prePostEnabled = true)

答えて

2

あなたはEnableGlobalMethodSecurity#securedEnabled@Securedを有効に:

は春のセキュリティの担保注釈を有効にする必要があるかどうかを決定します。

いますが、EnableGlobalMethodSecurity#prePostEnabled@PreAuthorizeを有効にする必要があります。

は春のセキュリティの事前事後の注釈を有効にする必要があるかどうかを決定します。デフォルトはfalseです。

変更した春のセキュリティ設定:

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private UserDetailsService userDetailsService; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 

     http 
      .csrf().disable() 
      .authorizeRequests() 
       .antMatchers("/static/build/app.js", "/static/app/styles/*/**", "/static/app/js/*/**", 
        "/static/build/libs.js", "/index.html", "/static/build/*/**", "/", "/static/**").permitAll() 
       .antMatchers("/auth/**").permitAll() 
       .antMatchers("/api/user/registerClient").permitAll() 
       .antMatchers("/api/user/checklogin/**").permitAll() 
       .antMatchers("/api/user/getAllAdmins").permitAll() 
       // .antMatchers("/api/**").hasAnyAuthority(AuthoritiesConstants.CLIENT, AuthoritiesConstants.ADMIN, AuthoritiesConstants.WORKER) 
       .anyRequest().authenticated() 
       .and() 
      .formLogin() 
       .loginPage("/") 
       .loginProcessingUrl("/") 
       .permitAll(); 
関連する問題