@Entity方法上的Spring安全方法安全性?
我们可以在JPA的@Entity方法上的Spring安全方法安全性?,spring,spring-security,Spring,Spring Security,我们可以在JPA的@Entity方法上使用Spring Security的@PreAuthorize、@PostAuthorize、@PreFilter或@PostFilter吗 我发现的唯一建议/答案是6年以上的线程,通常说“使用服务层”,有时说“控制器也可以有安全注释,但服务层更简单” 没有人谈论在@Entity上使用安全注释 要解释简单的用例: 假设我们为个人和团体和派对准备了3份春季jpa职位: @Entity class Person { ... @ManyToOne
@Entity
方法上使用Spring Security的@PreAuthorize
、@PostAuthorize
、@PreFilter
或@PostFilter
吗
我发现的唯一建议/答案是6年以上的线程,通常说“使用服务层”,有时说“控制器也可以有安全注释,但服务层更简单”
没有人谈论在@Entity
上使用安全注释
要解释简单的用例:
假设我们为个人
和团体
和派对
准备了3份春季jpa职位
:
@Entity
class Person {
...
@ManyToOne
Group group;
@ManyToOne
Party party;
}
@Entity
class Group {
...
@OneToMany
List<Person> people;
@ManyToOne
Party party;
}
@Entity
class Party {
...
@OneToMany
List<Person> people;
@OneToMany
List<Group> groups;
}
如果我无法保护实体方法,它会让我通过服务,如:
@Entity
class Party {
...
// Not secured here
List<People> getPeople() { return this.people; }
}
@Service
class PartyService {
@Autowired
PartyRepository repo;
@PreAuthorize("hasRole('ROLE_ADMIN')")
List<People> getPeople(Long partyId) {
return repo.getById(partyId).getPeople(); // Should either return people or throw access exception.
}
}
@Service
class GroupService {
@Autowired
GroupRepository repo;
@Autowired
PartyService partyService;
List<People> getPeopleInMyParty(Long groupId) {
return partyService.getPeople(repo.getById(groupId).getParty().getId()); // Should either return people or throw access exception.
}
}
@实体
班级聚会{
...
//这里不安全
List getPeople(){返回this.people;}
}
@服务
类PartyService{
@自动连线
部分回购;
@预授权(“hasRole('ROLE_ADMIN'))
列出getPeople(长partyId){
return repo.getById(partyId).getPeople();//应返回人或引发访问异常。
}
}
@服务
类组服务{
@自动连线
集团回购;
@自动连线
PartyService PartyService;
列出getPeopleInMyParty(长组ID){
return partyService.getPeople(repo.getById(groupId).getParty().getId());//应返回人员或引发访问异常。
}
}
这给我更复杂的JPA数据模型(及其获取服务)带来了复杂性,因为我们到处都有越来越多的@service和@Repository注入,代码变得难以维护。看看两个示例之间GroupService
的区别——如果您将其放大,您将获得大量ID和注入到所有相关服务中,而不仅仅是执行@Entity
方法调用
那么,我可以在
@实体级别执行它吗?如何启用它?这个问题产生了另一个问题,答案也解决了这个问题
基本上,我首先发现Spring安全性支持AspectJ开箱即用(在一个子项目中附带@Aspect),经过一些配置后,我成功地设置了它,有效地保护了我的@实体类
@Entity
class Party {
...
// Not secured here
List<People> getPeople() { return this.people; }
}
@Service
class PartyService {
@Autowired
PartyRepository repo;
@PreAuthorize("hasRole('ROLE_ADMIN')")
List<People> getPeople(Long partyId) {
return repo.getById(partyId).getPeople(); // Should either return people or throw access exception.
}
}
@Service
class GroupService {
@Autowired
GroupRepository repo;
@Autowired
PartyService partyService;
List<People> getPeopleInMyParty(Long groupId) {
return partyService.getPeople(repo.getById(groupId).getParty().getId()); // Should either return people or throw access exception.
}
}