2016-02-12 68 views
1

可以說我有一個User實體,ManyToMany映射到UserGroup實體。如果我創建兩個實體庫,並獲得URI /users/1,我得到的迴應是這樣的:彈簧數據保持安全HATEOAS鏈接

{ 
    "enabled" : true, 
    "password" : "xxxxxx", 
    "username" : "xxxxxx", 
    "credentialsNonExpired" : true, 
    "accountNonLocked" : true, 
    "accountNonExpired" : true, 
    "_links" : { 
    "self" : { 
     "href" : "http://127.0.0.1:45950/users/1" 
    }, 
    "user" : { 
     "href" : "http://127.0.0.1:45950/users/1" 
    }, 
    "userGroups" : { 
     "href" : "http://127.0.0.1:45950/users/1/userGroups" 
    } 
    } 
} 

這裏的羣組鏈接是非常有用的。

我可以使用/userGroups端點列出所有UserGroup。 我想保護/userGroups端點和/users/1/userGroups端點使用不同的spring-security表達式。

使用參考這裏:http://docs.spring.io/spring-data/rest/docs/current/reference/html/#security我知道如何確保第一個端點:

public interface UserGroupRepository extends PagingAndSortingRepository<UserGroup, Long> { 

    @PreAuthorize("hasRole('ROLE_ADMIN')") 
    @Override 
    Iterable<T> findAll(); 
} 

但是我怎麼保證第二端點?目前甚至有可能嗎?有沒有一些工作計劃在這個功能上。我很樂意貢獻。

回答

1

我也遇到過這個問題,沒有找到任何解決Spring的安全註解的解決方案。作爲一種變通方法,我沿着線補上一:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
     // Whatever config you already have. 
     .authorizeRequests() 
      .antMatchers("https://stackoverflow.com/users/*/userGroups").hasRole("ADMIN"); 
} 

我實施WebSecurityConfigurerAdapter。雖然這可以起作用,但它會重複在保護存儲庫時完成的工作,並且在添加新存儲庫時很容易忘記添加java config和annotations。