Java WithMockUser在HttpSecurityConfig上使用访问控制时不工作
如果使用Java WithMockUser在HttpSecurityConfig上使用访问控制时不工作,java,spring-security,spring-webflux,Java,Spring Security,Spring Webflux,如果使用hasAuthority或hasRole函数,则可以在测试中使用@WithMockUser。但是,如果使用access,则无法将@与mockuser一起使用正确的用户对象 @Bean public HttpSecurityConfig securityConfiguration() { return http -> { http.authorizeExchange() .pathMatchers(HttpMethod.GET, "/a
hasAuthority
或hasRole
函数,则可以在测试中使用@WithMockUser
。但是,如果使用access
,则无法将@与mockuser
一起使用正确的用户对象
@Bean
public HttpSecurityConfig securityConfiguration() {
return http -> {
http.authorizeExchange()
.pathMatchers(HttpMethod.GET, "/api/**")
.access(<ReactiveAuthorizationManager>));
};
}
您能否共享在
.access()
中使用的管理器?没有任何东西阻止@WithMockUser
与.access()
一起使用。如果您需要向模拟用户添加其他属性,这将取决于您的访问限制。添加了确切的impl。感谢您添加实现。如果我错了,请纠正我,但你的问题看起来很相似。如果有帮助,请告诉我。这能回答你的问题吗?
public class ModuleActionAuthorization implements ReactiveAuthorizationManager<AuthorizationContext> {
private final Module module;
private final Action action;
private final Brand brand;
public ModuleActionAuthorization(Module module, Action action, Brand brand) {
this.module = module;
this.action = action;
this.brand = brand;
}
public ModuleActionAuthorization(Module module, Action action) {
this(module, action, null);
}
@Override
public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, AuthorizationContext ignored) {
return authentication
.map(a -> {
CustomUserDetails userDetails = (CustomUserDetails) a.getPrincipal();
return new AuthorizationDecision(userDetails.hasAuthorityForAnyBrand(module, action));
});
}
}