【问题标题】:Spring security annotations with EL -- requires debug information compiled in?带有 EL 的 Spring 安全注释——需要编译的调试信息?
【发布时间】:2011-02-23 17:47:13
【问题描述】:

我正在考虑为我的应用程序使用带有 EL(表达式语言)功能的 Spring Security 注释。例如:

@PreAuthorize("hasPermission(#contact, 'admin')")
public void deletePermission(Contact contact, Sid recipient, Permission permission);

我需要 EL 功能,因为我已经构建了自己的 ACL 实现。但是,要将此功能与“#contact”类型参数一起使用,Spring 文档中这样说:

您可以访问任何方法 按名称作为表达式的参数 变量,前提是您的代码有 调试信息编译进去。

这引出了两个问题:

  1. 可以接受 商业化生产应用 与调试信息一起分发?
  2. 如果没有,有什么办法吗 这个?

感谢您对此的任何指导!

【问题讨论】:

    标签: debugging spring-security acl


    【解决方案1】:

    我想当你第一次解决这个问题时这不是一个选项,但现在你可以这样做

    @PreAuthorize("hasPermission(#contact, 'admin')")
    public void deletePermission(@P("contact") Contact contact, Sid recipient, Permission permission);
    

    http://docs.spring.io/spring-security/site/docs/current/reference/html/el-access.html#access-control-using-preauthorize-and-postauthorize

    【讨论】:

      【解决方案2】:

      我现在找不到参考,但您可能想知道 Java 8 将始终包含参数名称,即使我相信 Java 8 将始终包含参数名称,即使在调试模式下也是如此。

      【讨论】:

        【解决方案3】:

        作为一种解决方法,您可以使用自己的策略实现自定义ParameterNameDiscoverer。这是一个生成简单编号名称的示例(arg0 等):

        public class SimpleParameterNameDiscoverer implements
                ParameterNameDiscoverer {
        
            public String[] getParameterNames(Method m) {
                return  getParameterNames(m.getParameterTypes().length);        
            }
        
            public String[] getParameterNames(Constructor c) {
                return getParameterNames(c.getParameterTypes().length);        
            }
        
            protected String[] getParameterNames(int length) {
                String[] names = new String[length];
        
                for (int i = 0; i < length; i++)
                    names[i] = "arg" + i;
        
                return names;
            }
        }
        

        及配置:

        <global-method-security ...>
            <expression-handler ref = "methodSecurityExpressionHandler" />
        </global-method-security>
        
        <beans:bean id = "methodSecurityExpressionHandler" 
            class = "org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
            <beans:property name = "parameterNameDiscoverer">
                <beans:bean class = "foo.bar.SimpleParameterNameDiscoverer" />
            </beans:property>
        </beans:bean>
        

        【讨论】:

        • 这是很棒的东西。我不知道这个接口。基于这个实现,我想我会在注释中按数字引用参数:@PreAuthorize("hasPermission(#arg0, 'admin')") 老实说——我认为这对我的目的来说很好。您如何看待分布式 JAR 中的调试信息?
        • @HDave:我不能说分布式代码中的调试信息,我个人不喜欢依赖调试信息的东西,因为它使代码的行为依赖于编译器选项。
        • 截至 2013 年 10 月 spring secruity 3.1,这仍然是最好的方法吗?
        猜你喜欢
        • 2016-04-05
        • 2012-11-15
        • 2018-03-24
        • 2011-08-22
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        相关资源
        最近更新 更多