【问题标题】:spring security not working on preAuthorizespring security 不能在 preAuthorize 上工作
【发布时间】:2015-10-12 09:15:43
【问题描述】:

配置 Spring Security 似乎是一门黑暗的艺术!

我正在尝试实现方法级别的安全性。我已经尽一切努力让这个方法触发它的 preAuthorize。

谁能解释为什么第一种方法可以正常工作并评估任何反对它的表达式,但第二种方法不行。在日志中,我没有看到对该方法的安全框架的任何调用(我已经重新启动服务器可能 20 次,并且尝试了各种版本,在父方法上有和没有任何 preAuthorize)...

这很有挑战性,我没有任何信息可以参考,也没有 SPEL 表达式的调试功能...我似乎处于这个无处可去的试错循环中!

第二种方法肯定是输入的,因为我可以调试它,在日志中看到输出。

 @PreAuthorize("hasAnyRole('ROLE_REGISTERED_CUSTOMER', 'ROLE_ADMIN')")
    public void updateAddress(CustomerAddress customerAddress) {

        logger.entry();

        geocodingService.geoCodeAddress(customerAddress);

        if (customerAddress.getId() != null)
        {
            CustomerAddress oldAddress = customerAddressRepository.findForReference(customerAddress.getId());
            updateExistingAddress(customerAddress, oldAddress);
            customerSearchService.reindexCustomer(oldAddress.getCustomer(), true);
        }else
        {
            updateNewAddress(customerAddress);
            customerSearchService.reindexCustomer(customerAddress.getCustomer(), true);
        }
        logger.exit();
    }

    /**
     * A secured method for updating the existing address.
     * @param newAddress the new address
     * @param oldAddress the existing database address
     */
    @PreAuthorize("denyAll")
    public void updateExistingAddress(CustomerAddress newAddress, CustomerAddress oldAddress)
    {
        logger.entry();

        logger.debug("Updating an existing address.");


        oldAddress.mergeEdit(newAddress);
        saveAddress(oldAddress);

        logger.exit();
    }

【问题讨论】:

  • 为什么会失败?这是一个内部方法调用,因此绕过了您拥有的任何代理行为。如果从外部调用它将起作用...
  • denyAll 只是一个测试。我刚刚也想通了:代理...我将不得不重构这么多代码!哇,这将需要数周的工作才能解决:(

标签: spring-security


【解决方案1】:

正如@M.Deinum 所说,代理确实会通过内部方法调用来调用。如果你想要这种支持,你需要使用 AspectJ。您可以在示例中的xmljava config 中找到使用AspectJ 的示例。

【讨论】:

  • 没关系,我只是重构代码,将数据库对象从Controller传递给方法。干杯。
猜你喜欢
  • 2023-02-04
  • 1970-01-01
  • 2015-12-09
  • 1970-01-01
  • 2015-08-12
  • 2020-10-15
  • 2013-10-13
  • 2020-07-25
  • 2016-04-16
相关资源
最近更新 更多