【发布时间】:2019-03-03 01:08:58
【问题描述】:
在 Spring Web 中,我使用 @PreAuthorize 和 SpEl 来检查当前用户的权限。类似的东西:
@Component
public class CustomSecurity {
public boolean checkPermission(){
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
CurrentAccount currentAccount = (CurrentAccount)authentication.getPrincipal();
return currentAccount.getUsername().equals("admin");
}
}
在 RestController 中:
@PreAuthorize("@customSecurity.checkPermission()")
@GetMapping("/")
public Object getWidgetValues() {
return "Hello admin";
}
现在我尝试使用 WebFlux。 写了 reactiveCheckPermission。
public boolean checkPermission2() {
return ReactiveSecurityContextHolder.getContext()
.map(SecurityContext::getAuthentication)
.map(Authentication::getPrincipal)
.map(o -> (CurrentAccount) o)
.map(currentAccount -> currentAccount.getUsername().equals("admin"))
.block();
}
但是会抛出 IllegalStateException("block()/blockFirst()/blockLast() 是阻塞的,线程并行不支持这种情况
将布尔值更改为 Mono,但 @PreAuthroze 只需要布尔值,而不需要 Mono。
如何在 WebFlux 中使用 @PreAuthorize 对吗?
【问题讨论】:
标签: spring-security spring-webflux