- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 spring OAuth2 和 JWT token 来保护应用程序。我正在扩展 org.springframework.security.core.userdetails 以便向 token 添加一些额外的属性,这些属性随后可用于执行调用端点的授权。
public class CustomUser extends User {
private Set<String> bookIds;
public CustomUser(String username, String password, Collection<? extends GrantedAuthority> authorities) {
super(username, password, authorities);
}
public CustomUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
}
}
我还有一个 org.springframework.security.access.PermissionEvaluator 的自定义实现,它能够反序列化 token 并检查自定义属性是否是其中的一部分,可以将其添加到 Controller 端点。
@PreAuthorize("hasPermission(authentication, #bookId,'read')")
现在一切正常,我可以通过 postman 测试我的应用程序,只有拥有有效 JWT token 且其 bookId 集的 URL 部分中有 bookID 的用户才能访问资源
@PreAuthorize("hasPermission(authentication, #bookId,'read')")
@GetMapping(value = "api/books/{bookId}")
public Book getBook(@PathVariable String bookId) {}
但是,我正在努力测试它,因为这是微服务应用程序的一部分,其中身份验证和服务不是同一项目的一部分,而是在不同的虚拟机上运行。理想情况下,我希望能够在每个服务中模拟 token ,并在此 bookId 集中添加我需要的任何值。我知道在 spring 4 之后我们可以使用@WithMockUser,但是,据我所知,这仅限于用户名/密码/角色/权限,例如:
@WithMockUser(username = "ram", roles={"ADMIN"})
我真正想做的是扩展此注释以支持我的自定义属性“bookId”,或者在其中注入(inject)模拟集的方法。这是否可能,如果不可能,我的替代方案是什么,因为我无法在我的单元测试中调用我的身份验证提供程序,因为实现将存在于托管在单独应用程序上的另一个 spring 上下文中。
提前致谢!
最佳答案
您可以使用@WithUserDetails
注解。
如果您有一个用户名为“user1”的用户,那么您可以使用 @WithUserDetails("user1")
注释您的测试,它将使用 CustomUser
主体执行,包括与“user1”关联的自定义属性“bookId”。
您可以在 spring-security docs 中找到有关注释的更多信息。 .
如果你想要更多的灵 active ,你也可以使用@WithSecurityContext
注解。
这允许您创建自己的注释,例如 @WithMockCustomUser
,您可以在其中自行设置安全上下文。
您可以在 spring-security docs 中找到更多详细信息.
还有一个选项,如果您使用MockMvc
来运行您的测试,它会使用request post processor。 .
你的测试看起来像这样
mvc
.perform(get("/api/books/1")
.with(user(customUser)));
其中 customUser
是您在测试中创建的 CustomUser
的实例。
关于java - @WithMockUser 自定义用户实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52861849/
如何在 Spring security 单元测试中运行具有不同角色的每一行代码?使用时@WithMockUser只能以选定的权限运行整个方法,但我想以不同的权限运行每一行代码,所以我需要一个无注释的解
我正在使用 spring OAuth2 和 JWT token 来保护应用程序。我正在扩展 org.springframework.security.core.userdetails 以便向 toke
尽管我的测试方法使用@WithMockUser 进行了注释,但我仍然遇到访问被拒绝的情况。为什么这在集成测试中不起作用?使用@WebAppConfiguration 和 MockMvc 测试一切正常。
我已经使用 Spring Security 以经典方式在我的 Controller 中设置了基本身份验证,如下所示: @EnableWebSecurity @EnableGlobalMethodSec
我正在尝试使用 @WithMockUser 测试来测试我的 CommentController,但似乎找不到用户(因为用户 id (author_id) 为空)或评论内容。我不确定测试是否以正确的方式
我正在使用: request.IsUserInRole("ADMIN") 在我的 Controller 之一中确定对请求的响应。我尝试在测试中模拟请求,例如: @Mock private HttpSe
我将 Spring Boot 用于我的 Web 应用程序并使用 TestNG 进行单元测试。以下是我正在尝试的单元测试 @ContextConfiguration public class Autho
我刚刚发现了新的 Spring Security 4 测试注释 @WithMockUser,但我无法让它用于我的 Selenium 测试。问题是,这个注解创建了一个模拟的 SecurityContex
我正在使用 Cucumber 测试来测试启用了 Spring Security 的 Spring Boot 应用程序。一切正常,除非我使用 Cucumber 测试运行我的测试套件,一些使用 Sprin
环境:我有一个基于 spring boot 的微服务架构应用程序,由多个基础设施服务和资源服务(包含业务逻辑)组成。授权和身份验证由管理用户实体并为客户端创建 JWT token 的 oAuth2 服
由于我从 1.x 迁移到 Spring Boot 2.0.5,并且无意禁用安全性,因此我无法让测试角色进行模拟 MVC 测试: @RunWith(SpringRunner.class) @Spring
我正在为一个 spring 项目构建一个基于权限的访问控制,它具有分层权限。为此,我将 GrantedAuthority 对象扩展为 Permission。自定义 PermissionVoter 最终
我已经花了整整几天时间试图弄清楚我做错了什么,但不知道为什么它不起作用。首先,我想说的是,以下配置主要是从我正在处理的另一个项目中复制而来的,并且这些项目可以毫无问题地运行(但是它们的配置略有不同,并
我是一名优秀的程序员,十分优秀!