注入修改后的 Spring 安全表达式语言实现后,“需要调试符号信息 (...)”

2024-01-07

我有一个示例类要测试@PreAuthorize注释,看起来或多或少像这样:

class BankService {

    @PreAuthorize("hasCustomRole('ROLE_CUSTOM') or hasRole('ROLE_EXAMPLE')")
    Double getAccountBalance(Integer accountNumber) {
        return 1234;
    }

    @PreAuthorize("#accountNumber > 400")
    int getValue(Integer accountNumber) {
        return 1234;
    }
}

你可以注意到hasCustomRole(String expression) in the @PreAuthorize注释,我正在添加:

public class CustomSecurityExpressionRoot extends SecurityExpressionRoot {

    public CustomSecurityExpressionRoot(Authentication auth) {
        super(auth);
    }

    public boolean hasCustomRole(String expression) {
       return /* some magic */;
    }
}

另外,我正在延长DefaultMethodSecurityExpressionHandler通过以下方式:

public class CustomMethodSecurityExpressionHandler extends DefaultMethodSecurityExpressionHandler {

    public CustomMethodSecurityExpressionHandler() {
        super();
    }

    @Override
    public EvaluationContext createEvaluationContext(Authentication auth, MethodInvocation mi) {
        StandardEvaluationContext ctx = (StandardEvaluationContext) super.createEvaluationContext(auth, mi);
        ctx.setRootObject(new CustomSecurityExpressionRoot(auth));
        return ctx;
    }
}

最后,一切都包裹在resources.groovy:

beans = {
  /* ... some stuff ... */

  xmlns security:'http://www.springframework.org/schema/security'

  security.'global-method-security'('pre-post-annotations': 'enabled') {
    security.'expression-handler'(ref: 'expressionHandler')
  }

  expressionHandler(my.package.plugin.security.expression.CustomMethodSecurityExpressionHandler)
}

现在,如果我从中删除安全部分resources.groovy,我自然就失去了使用的能力hasCustomRole()方法,但以下有效:

assert bankService.getValue(500) == 1234

但是如果我注入自己的实现,前面的语句会导致:

Access is denied
org.springframework.security.access.AccessDeniedException: Access is denied

经过进一步调查,我发现了这一点:

prepost.PrePostAnnotationSecurityMetadataSource Looking for Pre/Post annotations for method 'getValue' on target class 'class my.package.plugin.security.test.BankService'
prepost.PrePostAnnotationSecurityMetadataSource @org.springframework.security.access.prepost.PreAuthorize(value=#accountNumber > 400) found on specific method: public int my.package.plugin.security.test.BankService.getValue(java.lang.Integer)
method.DelegatingMethodSecurityMetadataSource Adding security method [CacheKey[my.package.plugin.security.test.BankService; public int my.package.plugin.security.test.BankService.getValue(java.lang.Integer)]] with attributes [[authorize: '#accountNumber > 400', filter: 'null', filterTarget: 'null']]
aopalliance.MethodSecurityInterceptor Secure object: ReflectiveMethodInvocation: public int my.package.plugin.security.test.BankService.getValue(java.lang.Integer); target is of class [my.package.plugin.security.test.BankService$$EnhancerByCGLIB$$c590f9ac]; Attributes: [[authorize: '#accountNumber > 400', filter: 'null', filterTarget: 'null']]
aopalliance.MethodSecurityInterceptor Previously Authenticated: org.springframework.security.authentication.TestingAuthenticationToken@b35bafc3: Principal: test; Credentials: [PROTECTED]; Authenticated: true; Details: null; Granted Authorities: ROLE_TELLER
method.MethodSecurityEvaluationContext Unable to resolve method parameter names for method: public final int my.package.plugin.security.test.BankService$$EnhancerByCGLIB$$c590f9ac.getValue(java.lang.Integer). Debug symbol information is required if you are using parameter names in expressions.

有趣的部分是Debug symbol information is required if you are using parameter names in expressions.,这表明编译类时没有有关变量名称的调试信息。但如果我不注入自己的豆子,一切都会正常。

缺少调试信息的原因可能是什么,以及如何修复它?

它是一个 Grails 插件,为 Grails 2.0.4 开发,使用版本 1.2.7.3 的 spring-security-core 插件、版本 1.1 的 spring-security-acl 插件和 Spring Security 3.0.7.RELEASE。

EDIT:

为了让这个问题更有趣,这是我后来发现的:“丢失”的调试信息实际上是存在的,如果你查看一下.class文件与javap。所以类被正确编译,但 Spring 仍然抱怨......


我解决了这个问题,但是,我不确定为什么我收到的日志中的异常和消息与问题相差甚远。

我犯了一个错误,假设grails-app/conf/spring/resources.groovy可以以与使用 Grails 构建的应用程序类似的方式使用。尽管文档没有明确说明 bean 配置在resources.groovy在这种情况下不起作用,它指出resources.groovy(以及其他一些文件)将默认从打包中排除。

它没有解释运行测试时的奇怪行为,但它不是这种配置的好地方。

将 Spring Security 配置从resources.groovy通过以下方式进入插件描述符:

class MyOwnGrailsPlugin {

  /* ... some stuff ... */

  def doWithSpring = {
    /* ... some spring stuff ... */

    xmlns security:'http://www.springframework.org/schema/security'

    security.'global-method-security'('pre-post-annotations': 'enabled') {
      security.'expression-handler'(ref: 'expressionHandler')
    }

    expressionHandler(my.package.plugin.security.expression.CustomMethodSecurityExpressionHandler)
  }
}

一切正常并且测试通过。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

注入修改后的 Spring 安全表达式语言实现后,“需要调试符号信息 (...)” 的相关文章

随机推荐

  • 批处理文件 - 完整路径中的父文件夹

    我试图从包含完整路径 文件名 带空格 的变量中提取 Parent Folder 名称 我见过一些类似的线程 但我似乎无法成功适应我的方法 echo off setlocal enabledelayedexpansion set File1
  • Maven 构建未过滤 Intellij 中的属性

    我遇到一个问题 当我从 Intellij 15 0 2 运行 Maven 构建时 Maven 资源插件不会将我的属性过滤到我的文件中 当我跑步时它确实有效mvn compile从 Windows 命令行 我的插件配置是
  • 为什么/如何注册表项在 Regedit 中隐藏但在 PowerShell 中可见?

    我正在使用新的属性模式 并且习惯在注册或取消注册新版本时检查注册表 检查这一点的主要地点之一是HKEY LOCAL MACHINE SOFTWARE MICROSOFT WINDOWS CURRENTVERSION PROPERTYSYST
  • Jackson 不使用 JSON 根元素

    我使用 JAX RS Jersey 来使用 Web 服务请求 并使用 Jackson 来转换 JSON 数据 Path public class JAXRSRestController Path jsonRequest POST Consu
  • 如何在 Flutter 中点击 ListView 中的另一个项目后取消选择已选择的项目?

    我想允许用户从列表中仅选择一个选项 如果他一个接一个地选择 则只有最后一个选项应被视为已选择 在当前代码中 用户可以从列表中选择我想要避免的多个选项 尝试过的代码 class MyApp extends StatelessWidget ov
  • VBA 如果 <其中任何一个> = <值>?

    我对 VBA 相当陌生 我找不到一种简单的方法来测试任何指定变量是否等于指定值 下面的方法似乎可行 但是有更简单的方法吗 If variable1 1 Or variable2 1 Or variable3 1 Or variable4 1
  • c# 向 Windows 用户授予“作为服务登录”权限

    如何向用户授予服务的 LogOnAsService 权限 我需要手动执行此操作 在服务 msc我可以转到该服务 更改密码 设置与之前相同 单击 应用 然后收到一条消息 帐户 postgres 已 授予 作为服务登录 权限 我如何从代码中执行
  • 如何手动重新渲染组件 Angular 5

    有没有办法可以手动重新渲染组件 比如当用户单击按钮时 我看过类似的帖子 但这些帖子都不适合我here https stackoverflow com questions 35105374 how to force a components
  • pytorch错误:CrossEntropyLoss()不支持多目标

    我正在进行一个使用加速度数据来预测某些活动的项目 但我在损失计算上有问题 我在用CrossEntropyLoss for it 数据如下所示 我使用每行的前 4 个数据来预测索引 就像每行的最后一个数据一样 1 84 84 81 4 81
  • python 中 int() 违反直觉的行为

    中明确指出docs https docs python org 3 5 library functions html intint number 是一个地板类型转换 int 1 23 1 当且仅当字符串是整数文字时 int string 返
  • 查询视图时,视图定义中的过滤子句将被忽略

    我有一张桌子SCHEDULES有柱子LDATE and SCHTYPEID If SCHTYPEID 1 那么LDATE包含数字格式的日期 例如 20170918 表示今天的日期 如果SCHTYPEID 2 那么LDATE包含 0 在编写查
  • 在显示来自 viewDidload 的警报之前显示来自应用程序委托的警报

    我正在尝试通过应用程序委托显示推送通知中包含的消息 如 parse com 文档中所述 我遇到的问题是 在我的第一个视图控制器的 viewdidload 方法中 我呈现了一个警告 用户在使用该应用程序之前必须看到该警告 用户从 viewdi
  • 从另一个 C++ 程序编译 C++ 源代码

    我正在制作一个从 Brainfuck 到 C 的编译器 并且我已经完成了所有内容 直到我实际编译程序创建的 cpp 文件 我真的更希望编译器实际上能够生成可执行文件 而不仅仅是 C 的源代码 那么我该如何在 Windows 上执行此操作呢
  • 您使用什么材料来学习 Flex/Air 以及 ColdFusion + BlazeDS/LCDS? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • Nginx / PHP-FPM 504 网关超时

    我正在将 php fpm 与 nginx 一起使用 我有一些脚本可以获取上传的 Excel 工作表并对其进行处理 这是一项长期的工作 但是 执行 60 秒后 我收到 504 Gateway Timeout 错误 php 脚本继续运行直至完成
  • 根据商店营业时间有效确定企业是否营业

    给定时间 例如 目前周二下午 4 24 我希望能够从一组企业中选择当前正在营业的所有企业 我有一周中每一天每个企业的营业时间和休息时间 假设一家企业只能在每小时 00 15 30 45 分钟处开门 关门 我假设每周都有相同的时间表 我最感兴
  • 如何在 for 循环中重用 ES6 javascript 中的生成器?

    我正在尝试编写一个可以接受列表或生成器作为输入的函数 例如这个函数 function x l for let i of l console log i for let i of l console log i 如果我像这样运行它 x 1 2
  • jqGrid:如果主键列被编辑,如何更新行ID

    主键值用作从服务器返回的 json 数据中的行 ID 如果主键值被编辑并保存两次 第二次保存会因jqGrid而出错 再次将原始主键值传递给编辑方法 如果在内联编辑中更改主键值 如何将 jqGrid 行 ID 更新为新的主键值 functio
  • 从 Java/C# 到 C++ 的最佳方式是什么? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 注入修改后的 Spring 安全表达式语言实现后,“需要调试符号信息 (...)”

    我有一个示例类要测试 PreAuthorize注释 看起来或多或少像这样 class BankService PreAuthorize hasCustomRole ROLE CUSTOM or hasRole ROLE EXAMPLE Do