8

我为我的 Web 应用程序完全配置了 SPRING METHOD 安全性。(启用 PRE/POST 注释)。

但是最近我遇到了一个奇怪的问题。总结如下:

  1. POJOS总结

    // User Class
    public class User {
        int id;
        String name;
        // getters and setters
    }
    
    // Group Class
    public class Group {
        int id;
        String name;
        // getters and setters
    }
    
    // GroupMembership class
    public class GroupMembership {
        private int id;
        private User user;
        private Group group;
        // getters and setters
    }
    
  2. 方法上的 PreAuthorise 过滤器。

    @PreAuthorize("canIEditGroupProfile(#membership.group.id)")
    public int updateGroupMembership(GroupMembership membership)
        throws GroupsServiceException;
    

在传递完全填充的GroupMembership对象(存在正确的用户和组组合)时,安全过滤器会引发以下异常:

errorMessage: "Failed to evaluate expression
    canIEditGroupProfile(#membership.group.id)'"

在深入研究异常时:

发现原因是:

org.springframework.expression.spel.SpelEvaluationException:
    EL1007E:(pos 33): Field or property 'group' cannot be found on null

请提供指向相同地址的指针。

4

4 回答 4

6

getter/setters seems fine... also no case of null.

However a interesting observation; this one gives me an error:

@PreAuthorize("canIEditGroupProfile(#membership.group.id)")
public int updateGroupMembership(GroupMembership membership)
    throws GroupsServiceException; 

This works fine:

@PreAuthorize("canIEditGroupProfile(#groupmembership.group.id)")
public int updateGroupMembership(GroupMembership groupmembership)
    throws GroupsServiceException;

Further I observed, the parameter name was mismatching in case of first (i.e Service and ServiceImpl both had different parameter names).

Now maintaining the uniformity, the issue seems to be fixed.

于 2014-04-14T08:12:29.590 回答
2

我在我的 Spring Boot 应用程序中遇到了同样的问题。事实证明,正如上面评论中提到的那样,我在没有调试符号信息的情况下进行编译。我想说我可以通过两种方式解决这个问题:

1.(我最喜欢的):只需将其包含在您的 pom.xml --> 插件中

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
       <compilerArgument>-parameters</compilerArgument>
       <testCompilerArgument>-parameters</testCompilerArgument>
    </configuration>
</plugin>
  1. 如果您使用 Java 1.8 和 Eclipse 作为 IDE,请转到您的项目属性 --> Java 编译 --> 选中“存储有关方法参数的信息(可通过反射使用)”。

我发现这个链接真的很有趣,可以了解更多关于这个问题的信息。

希望能帮助到你!

于 2017-01-21T10:30:13.640 回答
0

正如@zeroflagL 所问:您是否在没有调试信息的情况下进行编译?这可能与带有 Ehcache 的 spring @Cacheable、spel find null for valid object带有 SpEL 键的 Spring @Cacheable 的问题相同:始终评估为 null<debug>false</debug> - 检查您的 POM(或 Eclipse 配置或其他)以进行调试配置,例如maven-compiler-plugin. _

于 2014-06-20T07:29:48.660 回答
0

我遇到了同样的问题,发现要检查授权的对象的名称在接口和实现中必须相同。

例如,如果您的界面中有此方法:

@PreAuthorize("hasPermission(#foo, 'UPDATE')")
public void testMethod(MyObject foo);

您应该在实现中具有以下内容:

public void testMethod(MyObject foo) { ... your code here... }

我希望这有帮助。

于 2019-12-18T16:55:35.597 回答