我正在使用 Spring Boot spring-data-rest 创建 CrudRepository,并向用户公开其余端点。
但我想阻止用户创建新记录。 仅允许用户更新现有记录。
怎样才能实现呢? CrudRepository 没有 create
方法,只有 save
delete
...
最佳答案
您可以添加拦截器:
public class SecurityInterceptor extends EmptyInterceptor {
@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
// insert logic here to check if it's an update
return super.onSave(entity, id, state, propertyNames, types);
}
}
使用您的 application.yaml 添加拦截器:
jpa:
properties:
hibernate:
ejb:
interceptor: hello.SecurityInterceptor
关于java - 限制 CrudRepository 中的创建操作,仅允许编辑操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42223415/