Spring 中使用 @Secured 注解的方法安全性
该注解提供了一种向业务方法添加安全配置的方法。
它将使用角色来检查用户是否有权调用此方法。注解是 Spring Security 的一部分。因此,要启用它的使用,您需要 spring security 依赖项。
示例场景
您有一个具有产品 CRUD 的应用程序。在此 CRUD 中,您希望使用两个特定角色来控制操作。
- 用户:可以创建产品并查看产品。但无法更新或删除产品。
- 管理员:可以执行所有用户操作,还可以更新和删除产品。
您可以使用@Secured 来管理这些角色对每个操作的访问权限。
运营角色
我们可以在示例场景中定义以下角色。
- ROLE_USER、ROLE_ADMIN
阅读:
- ROLE_USER、ROLE_ADMIN
更新:
- ROLE_ADMIN
删除:
- ROLE_ADMIN
让我们看一个代码示例并观察应用程序的行为。
添加 Spring Security 依赖
要使用 @Secured 注释,请添加 Spring Security 的 Maven 依赖项:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
使用@Secured注释方法
我们使用 @Secured 注释方法,定义哪些角色可以访问方法行为。
public class Product { private Long id; private String name; private BigDecimal value; //getters and setters } @Service public class ProductService { @Secured({"ROLE_USER", "ROLE_ADMIN"}) public Product createProduct(Product product) { // Logic for creating a product return product; } @Secured({"ROLE_USER", "ROLE_ADMIN"}) public Product getProductById(Long id) { // Logic for fetching a product return null; } @Secured("ROLE_ADMIN") public Product updateProduct(Product product) { // Logic for updating a product return product; } @Secured("ROLE_ADMIN") public void deleteProduct(Long id) { // Logic for deleting a product } }
应用配置
您需要添加@EnableGlobalMethodSecurity(securedEnabled = true)来配置您的Spring应用程序以使用@Secured启用方法安全性。
@SpringBootApplication @EnableTransactionManagement @EnableGlobalMethodSecurity(securedEnabled = true) public class MasteryApplication { public static void main(String[] args) { SpringApplication.run(MasteryApplication.class, args); } }
测试行为
在我们的示例中,我们将使用测试来测试行为,因此我们添加 spring boot 测试依赖项。
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency>
然后我们创建测试来验证是否使用模拟用户并为其分配特定角色,我们可以测试每个角色中的用户以及我们的应用程序的行为方式。通过这样做,我们可以确保只有正确的角色才能执行允许的操作。
@SpringBootTest class ProductServiceTests { @Autowired private ProductService productService; @Test @WithMockUser(roles = "USER") void testCreateProductAsUser() { Product product = new Product(); assertDoesNotThrow(() -> productService.createProduct(product)); } @Test @WithMockUser(roles = "ADMIN") void testCreateProductAsAdmin() { Product product = new Product(); assertDoesNotThrow(() -> productService.createProduct(product)); } @Test @WithAnonymousUser void testCreateProductAsAnonymous() { Product product = new Product(); assertThrows(AccessDeniedException.class, () -> productService.createProduct(product)); } @Test @WithMockUser(roles = "USER") void testGetProductByIdAsUser() { assertDoesNotThrow(() -> productService.getProductById(1L)); // Assuming product with ID 1 exists } @Test @WithMockUser(roles = "ADMIN") void testGetProductByIdAsAdmin() { assertDoesNotThrow(() -> productService.getProductById(1L)); } @Test @WithAnonymousUser void testGetProductByIdAsAnonymous() { assertThrows(AccessDeniedException.class, () -> productService.getProductById(1L)); } @Test @WithMockUser(roles = "USER") void testUpdateProductAsUser() { Product product = new Product(); assertThrows(AccessDeniedException.class, () -> productService.updateProduct(product)); } @Test @WithMockUser(roles = "ADMIN") void testUpdateProductAsAdmin() { Product product = new Product(); assertDoesNotThrow(() -> productService.updateProduct(product)); } @Test @WithAnonymousUser void testUpdateProductAsAnonymous() { Product product = new Product(); assertThrows(AccessDeniedException.class, () -> productService.updateProduct(product)); } @Test @WithMockUser(roles = "USER") void testDeleteProductAsUser() { assertThrows(AccessDeniedException.class, () -> productService.deleteProduct(1L)); } @Test @WithMockUser(roles = "ADMIN") void testDeleteProductAsAdmin() { assertDoesNotThrow(() -> productService.deleteProduct(1L)); } @Test @WithAnonymousUser void testDeleteProductAsAnonymous() { assertThrows(AccessDeniedException.class, () -> productService.deleteProduct(1L)); } }
就是这样,现在您可以使用带有 @Secured 注释的角色来管理用户对应用程序的访问。
如果你喜欢这个话题,记得关注我。在接下来的几天里,我将详细解释 Spring 注解!请继续关注!
跟我来!
以上是Spring 中使用 @Secured 注解的方法安全性的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本启动Spring...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

Redis缓存方案如何实现产品排行榜列表的需求?在开发过程中,我们常常需要处理排行榜的需求,例如展示一个�...
