gpt4 book ai didi

Spring 设计契约(Contract) : where to start?

转载 作者:行者123 更新时间:2023-12-01 00:04:02 25 4
gpt4 key购买 nike

我试图在方法调用上放置一个“契约(Contract)”。我的 Web 应用程序在 Spring 3 中。

编写海关注释是正确的方法。如果是这样,任何指针(我在 spring 引用文档中没有找到任何东西)。

我应该使用“Modern Jass”、JML 之类的工具吗?同样,任何指针都会有用。

谢谢

最佳答案

使用 Spring ELSpring security可以帮助您完成大部分工作。 Spring 安全定义了 @PreAuthorize 在方法调用之前触发的注释,允许您使用 Spring 3 的新表达式引擎,例如:

@PreAuthorize("#customerId > 0")
public Customer getCustomer(int customerId) { .. }

或更高级的规则,如下所示,确保通过 用户 没有角色 管理员 .
@PreAuthorize("#user.role != T(com.company.Role).ADMIN)")
public void saveUser(User user) { .. }

您还可以使用 @Value 注释为您的契约(Contract)提供默认值
public Customer getCustomer(@Value("#{434}") int customerId) { .. }

您甚至可以在值表达式中引用系统属性。

为此目的设置 Spring 安全性并不难,因为您只需创建一个 UserDetailsService授予所有用户一些默认角色。或者你可以让你自己自定义 Spring 方面,然后让它使用 SpelExpressionParser检查方法值。

关于 Spring 设计契约(Contract) : where to start?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2247302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com