spring-security - 如何在 Spring WebFlux 中使用 @PreAuthorize?
问题描述
在 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。写了反应检查权限。
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() are blocking,线程并行不支持
将布尔值更改为 Mono,但 @PreAuthroze 只需要布尔值,而不需要 Mono。
如何在 WebFlux 中使用 @PreAuthorize 对吗?
解决方案
我找到了一种解决方案。
@PreAuthorize("@customSecurity.checkPermission(#account)")
@GetMapping("/")
public Object getWidgetValues(@AuthenticationPrincipal(expression = "account") Account account) {
return "Hello admin";
}
在 ReactiveUserDetailsService 中使用 CurrentAccount
public class CurrentAccount extends User {
private Account account;
public CurrentAccount(Account account) {
super(account.getLogin(), account.getPassword(), true, true,
true, !account.isLocked(),
AuthorityUtils.createAuthorityList(account.getRole().name()));
this.account = account;
}
推荐阅读
- swiftui - 导航栏中的 SwiftUI 元素不响应状态
- visual-studio-code - 如何在我自己的vscode扩展中以编程方式获取文档中carret位置的类/方法/方法参数信息
- uml - UML 类图:行的属性是什么意思?
- c# - 我似乎无法理解此 C# 代码中用于从输入字段中计算字符的当前错误
- python - 熊猫合并返回一个空的数据框
- list - TypeError:'<'不支持Python中的'list'
- python - 计数到第一列并总结到其余列 pandas groupby
- eclipse - 使用 Subclipse 插件
- keras - Google AI Platform 上的所有超调试验都失败了
- xcode - 旧 iOS 版本中 Xcode 模拟器屏幕出错