If you use the hasAuthority
or hasRole
functions you can use @WithMockUser
in your tests. However if I use access
there is no way to use @WithMockUser
with the correct user object.
@Bean
public HttpSecurityConfig securityConfiguration() {
return http -> {
http.authorizeExchange()
.pathMatchers(HttpMethod.GET, "/api/**")
.access(<ReactiveAuthorizationManager>));
};
}
How can I write a test for a controller that is protected with the above HttpSecurityConfig
? What I need is to create a fake UserDetail
object that is used in the exchange (I think). I use Webflux.
UPDATE: This is the manager i want to use:
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));
});
}
}