spring-boot - Spring-Boot:无法创建自定义安全表达式
问题描述
我正在使用 Spring boot 2.4.1 并按照链接中的说明创建自定义安全表达式。不幸的是,我无法创建SecurityExpressionRoot 方法,并且我的注释也不起作用。
调用 api 时出现错误
[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Failed to evaluate expression 'hasAccessToCollection('Administrator')'] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method hasAccessToCollection(java.lang.String) cannot be found on type org.springframework.security.access.expression.method.MethodSecurityExpressionRoot
我的服务
@PreAuthorize("hasAccessToCollection('Administrator')")
public Map getCustomPermission() {
Map<String, String> response = new HashMap<String, String>() {{
put("message", "Successful");
}};
return response;
}
我的 CustomMethodSecurityExpressionRoot
public class CustomMethodSecurityExpressionRoot
extends SecurityExpressionRoot implements MethodSecurityExpressionOperations {
public IcodeMethodSecurityExpressionRoot(Authentication authentication) {
super(authentication);
}
public boolean hasAccessToCollection(String permission) {
return true;
}
public boolean hasAccessToCollection(String permission, String attribute) {
return true;
}
...
}
我的 CustomMethodSecurityExpressionHandler
public class CustomMethodSecurityExpressionHandler extends DefaultMethodSecurityExpressionHandler {
private AuthenticationTrustResolver trustResolver =
new AuthenticationTrustResolverImpl();
protected MethodSecurityExpressionOperations createSecurityExpressionRoot(
Authentication authentication, MethodInvocation invocation) {
CustomMethodSecurityExpressionRoot root =
new CustomMethodSecurityExpressionRoot(authentication);
root.setPermissionEvaluator(getPermissionEvaluator());
root.setTrustResolver(this.trustResolver);
root.setRoleHierarchy(getRoleHierarchy());
return root;
}
}
解决方案
推荐阅读
- matlab - Matlab:如何从 4-d 矩阵绘图
- oauth-2.0 - 您如何强制合作伙伴安全存储访问令牌?
- angularjs - 不同的html文件和相同的控制器Angular JS
- perl - 将 Log::Log4perl 与 MCE(多核引擎)Perl 一起使用
- javascript - 如何在 if 失败中使 nightwatch.js 测试失败
- reactjs - 如何使用 webapi Core 在 React 中使用 axios.post 下载文件
- sql - 用多行替换一行形成不同的表
- queue - 分布式应用程序之间的消息中包含哪些数据?
- angular - *ngFor in *ngFor on firebase 数据库
- gcc - 如何知道.exe程序的堆栈大小限制?