2013-12-11 68 views
1

我使用Spring數據REST和我在我的庫中的查找方法:春季安全/ rolesAllowed/antMatchers

public List<Contact> findByLastNameOrderByLastNameAsc(@Param("lastName") String lastName); 

我試圖增加安全的方法,但沒有運氣。在我的數據庫中,我有1個角色爲'ROLE_USER'的用戶。服務啓動時,登錄表單出現,我可以使用數據庫中的憑據登錄。

這裏是我的web安全配置:

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 

    auth.jdbcAuthentication() 
      .dataSource(dataSource) 
      .usersByUsernameQuery("select username,identification,enabled from users where username = ?"); 


    } 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
    .authorizeRequests() 
    .antMatchers("/contacts/findByLastNameOrderByLastNameAsc").hasRole("ADMIN") 
    .antMatchers("/contacts/**").fullyAuthenticated() 
    .antMatchers("/contacts/**").hasRole("USER") 
    .anyRequest().authenticated() 

    .and() 
    .formLogin(); 
} 

當我嘗試調用服務在我的倉庫,我沒有看到任何驗證錯誤。使用我的瀏覽器,即使數據庫中的用戶不具有'ADMIN'角色,URL也可以正常工作。

在我的倉庫我嘗試添加「RolesAllowed」的方法,但沒有運氣:

@RolesAllowed(value = { "ADMIN" }) 
public List<Contact> findByLastNameOrderByLastNameAsc(@Param("lastName") String lastName); 

我要對增加安全性進行春季正確的數據所提供的REST API?想法如何讓這個工作?

謝謝

回答

5

FWIW:我忘了添加jsr250支持。所以,我添加了這個配置:

@Configuration 
@EnableGlobalMethodSecurity(jsr250Enabled = true) 
public class MethodSecurityConfig { 

} 

現在RolesAllowed註釋工作正常。