Search code examples
springspring-bootspring-securityspring-el

Registering method in custom SecurityExpressionOperations as Spring SpEL function


I have the following implementation of MethodSecurityExpressionOperations

public class CustomMethodSecurityExpressionRoot extends SecurityExpressionRoot implements MethodSecurityExpressionOperations {
        private Object filterObject;
        private Object returnObject;

        CustomMethodSecurityExpressionRoot(Authentication authentication) {
          super(authentication);
        }

        public boolean isTeamMember(Job job) {
          //very interesting logic
        }

        @Override
        public Object getFilterObject() {
          return this.filterObject;
        }

        @Override
        public Object getReturnObject() {
          return this.returnObject;
        }

        @Override
        public Object getThis() {
          return this;
        }

        @Override
        public void setFilterObject(Object obj) {
          this.filterObject = obj;
        }

        @Override
        public void setReturnObject(Object obj) {
          this.returnObject = obj;
        }
    }

As you can see I have custom defined method called isTeamMember. This method is successfully evaluated by the following pre authorize annotation: @PreAuthorize("isTeamMember(#job)") but unfortunately it is not resolved as the Spring SpEL function.

see the warning

Is there any Spring Bootish automagic way to register isTeamMember as SPeL function?


Solution

  • I'd say this is IntelliJ IDEA concern and fully unrelated to the Function support in SpEL:

    You can extend SpEL by registering user defined functions that can be called within the expression string.

    There is nothing to do from the SpEL, Spring Framework or Spring Boot side since all you need is there already via your CustomMethodSecurityExpressionRoot.

    What you have in your WARN is just a pointer that the IDEA knowledge about root Object of the SpEL evaluation context is out of your custom CustomMethodSecurityExpressionRoot scope.

    You may do like this #this.isTeamMember(#obj) or #root.isTeamMember(#obj) to have the same result, but again: there is nothing about fucntions. Don't confuse yourself.