自定义spring security的安全表达式

在Spring Security中,DefaultMethodSecurityExpressionHandler是处理方法安全表达式的默认处理器。如果你想注册自定义的安全表达式方法,你需要创建一个自定义的表达式处理器,继承自DefaultMethodSecurityExpressionHandler,并重写createSecurityExpressionRoot方法来提供你自己的MethodSecurityExpressionOperations实现。

以下是注册自定义安全表达式方法的步骤:

  1. 创建一个自定义的MethodSecurityExpressionRoot类,扩展SecurityExpressionRoot并实现MethodSecurityExpressionOperations接口。在这个类中,你可以添加自定义方法,这些方法将在SpEL表达式中使用。

    java 复制代码
    public class CustomMethodSecurityExpressionRoot extends SecurityExpressionRoot implements MethodSecurityExpressionOperations {
        public CustomMethodSecurityExpressionRoot(Authentication authentication) {
            super(authentication);
        }
        // 添加你的自定义方法
        public boolean isMember(Long organizationId) {
            // 你的自定义逻辑
        }
        // 其他可能需要重写的方法
    }
  2. 创建一个自定义的表达式处理器,继承自DefaultMethodSecurityExpressionHandler,并重写createSecurityExpressionRoot方法,以返回你的自定义MethodSecurityExpressionRoot实例。

    java 复制代码
    public class CustomMethodSecurityExpressionHandler extends DefaultMethodSecurityExpressionHandler {
        @Override
        protected MethodSecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, MethodInvocation invocation) {
            CustomMethodSecurityExpressionRoot root = new CustomMethodSecurityExpressionRoot(authentication);
            root.setPermissionEvaluator(getPermissionEvaluator());
            root.setTrustResolver(getTrustResolver());
            root.setRoleHierarchy(getRoleHierarchy());
            return root;
        }
    }
  3. 在你的配置类中,使用@EnableGlobalMethodSecurity注解来启用方法安全,并使用MethodSecurityConfigurer来设置自定义的表达式处理器。

    java 复制代码
    @Configuration
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
        @Override
        protected MethodSecurityExpressionHandler createExpressionHandler() {
            return new CustomMethodSecurityExpressionHandler();
        }
    }
  4. 现在你可以在方法安全注解中使用你的自定义表达式了。

    java 复制代码
    @PreAuthorize("isMember(#id)")
    public void someMethod() {
        // ... 方法体 ...
    }

getPermissionEvaluator()方法中获取自定义的权限评估器,你需要实现PermissionEvaluator接口,并在CustomMethodSecurityExpressionHandler中通过setPermissionEvaluator方法设置它。这样,你就可以在自定义的MethodSecurityExpressionRoot中使用自定义的权限评估逻辑了。

以上步骤基于搜索结果中的信息,特别是来自Baeldung的教程,它详细解释了如何创建和注册自定义的安全表达式方法。

相关推荐
趣知岛36 分钟前
初识Java
java·开发语言
凌峰的博客2 小时前
基于深度学习的图像安全与隐私保护研究方向调研(中)
人工智能·深度学习·安全
步菲3 小时前
springboot canche 无法避免Null key错误, Null key returned for cache operation
java·开发语言·spring boot
毕设源码-朱学姐3 小时前
【开题答辩全过程】以 基于SpringBoot的中医理疗就诊系统为例,包含答辩的问题和答案
java·spring boot·后端
2201_757830877 小时前
全局异常处理器
java
小徐Chao努力8 小时前
【Langchain4j-Java AI开发】09-Agent智能体工作流
java·开发语言·人工智能
Coder_Boy_8 小时前
SpringAI与LangChain4j的智能应用-(理论篇3)
java·人工智能·spring boot·langchain
Coder_Boy_8 小时前
基于SpringAI的智能平台基座开发-(六)
java·数据库·人工智能·spring·langchain·langchain4j
网安INF9 小时前
网络杀伤链(CKC模型)与ATT&CK模型详解
网络·安全·网络安全·网络攻击模型
foo1st9 小时前
Web应用渗透测试经验教训1.0
安全·渗透测试·web app