Integration between SpringSecurity and SpringData to use SpEL

1

Good afternoon

I'm creating a routine where I need to use Spel to access Authentication data directly in the SpringData @Query, I followed the SpringData tutorial itself to perform the configuration:

SpringData with SpEL

After following the whole tutorial I created all the necessary settings:

Extension support deployment:

public class ImplEvaluationContextExtension extends EvaluationContextExtensionSupport{

    @Override
    public String getExtensionId(){
        return "security";
    }

    @Override
    public SecurityExpressionRoot getRootObject() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        return new SecurityExpressionRoot(authentication) {};
    }
}

Method creating object

    @Bean
    public EvaluationContextExtension securityExtension(){
        return new ImplEvaluationContextExtension();
    }

Maven dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-data</artifactId>
</dependency>

Error trying to log in to app

aused by: java.lang.IllegalArgumentException: Authentication object cannot be null
at org.springframework.security.access.expression.SecurityExpressionRoot.      (SecurityExpressionRoot.java:61)
at br.com.wts.mobione.conf.security.SecurityEvaluationContextExtension$1. (SecurityEvaluationContextExtension.java:18)
at br.com.wts.mobione.conf.security.SecurityEvaluationContextExtension.getRootObject(SecurityEvaluationContextExtension.java:18)
at br.com.wts.mobione.conf.security.SecurityEvaluationContextExtension.getRootObject(SecurityEvaluationContextExtension.java:1)

I made some debugging and found that Spring invokes the securityExtension () method, before the login process has been completed, because of this the NullPointerException happens.

Has anyone ever solved this problem?

Thank you.

    
asked by anonymous 13.12.2016 / 17:08

0 answers