【发布时间】:2015-12-15 22:04:11
【问题描述】:
我不确定 Spring Security 的能力是否正确。
我的问题是,我想prevent a logged in user to send arbitrary IDs to my server and therefore access data that does not belong to him。但我能找到的每个教程都是关于一个简单的login procedure。但是我怎么能用它来摆脱
if(item .getStore().getId() == store.getId()) { /* .. */ }
在这个例子中:
// StoreService.java
@Transactional
public ItemDTO deleteItem(String sessionId, Long storeId, ItemDTO itemDto) {
// sessionId is the cookie I have placed in my database
// This way I want to ensure that I am only accessing a store
// that is associated with the logged in store owner (the user basically)
Store store = this.storeOwnerRepository.getStore(sessionId, storeId);
Item item = ConvertDTO.convertItem(store, itemDto);
// THIS CHECK IS WHAT I WANT TO GET RID OF:
// Check if the store ID that I got using the cookie is the
// same ID as the store ID from the item that should be deleted
if(item.getStore().getId() == store.getId()) {
item = this.storeOwnerRepository.deleteItem(item);
} else {
// If this didn't work we have a potentially hostile user:
throw new RuntimeException("Is somebody trying to delete items from a store he doesn't own?");
}
itemDto = ConvertEntity.convertItem(item);
return itemDto;
}
使用 Spring 注解? Spring Security 甚至可以做到这一点吗?
可能起作用的另一件事是Hibernate Filters,但我不确定我是否希望我的数据库了解我的数据的安全方面。
所以我很困惑如何正确地做到这一点。有什么想法吗?
【问题讨论】:
-
如果查询数据库,看看用户是否与该商店关联的商店正在尝试修改,如果是,则执行查询,如果不是,返回其他内容?
-
@jpganz18 这就是我在
item.getStore().getId() == store.getId()的示例代码中所做的,但我认为 Spring Security 可以帮助我摆脱这种代码。 -
Spring Security 中是否有与特定商店相关的角色?
-
@jpganz18 好吧,不。目前我没有使用 Spring Security,因为我不知道如何做到这一点。但是如果有一个角色,那么
StoreOwner(在我的代码中由storeOwnerRepository表示)实际上是一个“拥有”一个或多个商店的登录用户。该用户基本上将拥有ADMIN的角色,但由于我只有一个角色,我不确定这是否重要。问题是这个用户可以简单地向我发送任意 ID 并修改他不拥有的商店,除非我在那里进行此检查,但我找不到任何可以帮助我的 Spring Security 示例代码。 -
1.你关心与其他用户“共享”实体,还是只关心用户(或管理员)看到他们自己的东西? 2. 哪个数据库品牌,在db做的感觉如何? 3. Spring Security 4?
标签: java hibernate spring-security access-control