2016-08-26 4 views
0

SpringでHATEOAS REST APIビルドを安全にしたい。すべてのリクエストには承認が必要で、 "/ rooms"へのPOSTリクエストには管理者の役割が必要です。その後、私は「antMatchers ...」行の前に「anyRequest()認証された()。」行を置けば今、すべてのリソースが唯一の認証を必要とするスプリングセキュリティ。リクエストには承認が必要で、特別なPOSTリクエストには管理者の役割が必要です。これを行う方法?

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .authorizeRequests() 
       // Todo: Make sure that all resources need to be authenticated and POST rooms needs ADMIN role 
        .anyRequest().authenticated() 
        .antMatchers(HttpMethod.POST, "/api/v1/rooms").hasRole("ADMIN") 
       .and() 
       .httpBasic() 
       .and() 
       .csrf().disable(); 
    } 

、しかし:私のWebSecurityConfigurerAdapterの実装コードは今、この権利のように見えます必要な「ADMIN」ロールは機能せず、適用されず、その逆もありません。

どのようにして両方の作業を同時に行うことができますか?種類よろしく、 フロリアン

答えて

0

Securityconfiguration.java

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

     http.httpBasic().and().authorizeRequests().antMatchers("/public/**") 
       .permitAll().antMatchers("/sa/**").hasAuthority("sa") 
       .antMatchers("/admin/**").hasAuthority("admin") 
       .and().logout() 
       .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
       .logoutSuccessUrl("/index.html").and() 
       .addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class) 
       .csrf().disable();  
    } 

、残りのコントローラの使用中..

@RequestMapping("/admin/adduser") 
    public void addUser(@RequestBody User user) { 
     authService.addUser(user); 
    } 
0

次のコードは、私のためにそれをやった

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .authorizeRequests() 
        .antMatchers(HttpMethod.POST, "/api/v1/rooms").hasRole("ADMIN") 
        .anyRequest().authenticated() 
       .and() 
       .httpBasic() 
       .and() 
       .csrf().disable(); 
    } 
} 

応答ありがとうPankaj。

関連する問題