java - @WithMockUser 自定义用户实现

标签 java spring unit-testing spring-security spring-security-oauth2

我正在使用 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/

相关文章:

java - 在动态添加的布局中查找 View

java - 使用批处理程序编译Jar文件

spring - 在 Spring 代理 bean 中查找注解

unit-testing - Rhino 模拟,MbUnit : Best way to check if object has raised an event

c# - 如何从字符串生成流?

java - 如果构造函数以异常结束,创建的对象是否与普通对象相同?

java - 有没有办法在没有 org.bson.types.ObjectId 的情况下使用 Mongo/Morphia ?

java - spring boot GenericFilterBean ,过滤器在客户端返回错误代码和响应头

java - spring-kafka 如何在后台运行消费者

python - 我如何自动指定在 python 3 中默认为 None 的模拟属性?