Integration between Springsecurity and Springdata to use Spel

Asked

Viewed 51 times

1

Good afternoon

I am creating a routine where I need to use Spel to access data from the logged-in user (Authetication) directly in Springdata @Query, I followed the Springdata tutorial to perform the configuration:

Springdata with Spel

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

Implementation of extension support:

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 the object

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

Dependency Maven

<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 into the application

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 performed some debugs and found that Spring invoka the method securityExtension(), before the login process has been completed, due to this happens to Nullpointerexception.

Someone has already solved this problem ?

Thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.