Explanations:
I would like to use the @Query annotation with @Scheduled together, but I have not found a way to do this.
The problem is that you need to authenticate in order to use @Query in order to be able to use the current session in the request. But in the method launched with @Scheduled , there is no authentication. Here is an example of a stack trace to show the problem:
java.lang.IllegalArgumentException: Authentication object cannot be null at org.springframework.security.access.expression.SecurityExpressionRoot.<init>(SecurityExpressionRoot.java:61) at org.springframework.security.data.repository.query.SecurityEvaluationContextExtension$1.<init>(SecurityEvaluationContextExtension.java:108) at org.springframework.security.data.repository.query.SecurityEvaluationContextExtension.getRootObject(SecurityEvaluationContextExtension.java:108) at org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider$EvaluationContextExtensionAdapter.<init>(ExtensionAwareEvaluationContextProvider.java:463) at org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider.toAdapters(ExtensionAwareEvaluationContextProvider.java:210) at org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider.access$000(ExtensionAwareEvaluationContextProvider.java:59) at org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider$ExtensionAwarePropertyAccessor.<init>(ExtensionAwareEvaluationContextProvider.java:235) at org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider.getEvaluationContext(ExtensionAwareEvaluationContextProvider.java:107) at org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider.getEvaluationContext(ExtensionAwareEvaluationContextProvider.java:59) at org.springframework.data.couchbase.repository.query.StringN1qlBasedQuery.parseSpel(StringN1qlBasedQuery.java:237) at org.springframework.data.couchbase.repository.query.StringN1qlBasedQuery.getStatement(StringN1qlBasedQuery.java:293) at org.springframework.data.couchbase.repository.query.AbstractN1qlBasedQuery.execute(AbstractN1qlBasedQuery.java:94) ...
This mechanism is automatically configured in Spring Boot with org.springframework.boot.autoconfigure.security.SecurityDataConfiguration to be able to request data for an authenticated user (via SpEL).
What I use:
- Spring Download v1.5.7.RELEASE
- Spring Boot Configuration Using
@EnableAutoConfiguration
What I want:
I do not want my @Query methods to @Query able to use the SecurityEvaluationContextExtension provided by Spring Boot because I do not use it in my project (no entity is connected to the connected user).
To do this, I want to disable SecurityEvaluationContextExtension with automatic configuration in SecurityDataConfiguration .
What I tried:
- override the
SecurityEvaluationContextExtension bean in the user configuration class to return a null bean (instead of new SecurityEvaluationContextExtension() ), but I am not allowed to do this. - exclude the
SecurityDataConfiguration configuration, but it is not defined as automatic configuration, it is included in the SecurityAutoConfiguration class:- I'm not sure that I can only exclude
SecurityDataConfiguration , as this is not an AutoConfiguration class. - I'm not sure if it is a good idea to exclude this entire
SecurityAutoConfiguration class from the automatic configuration (as well as the other security configuration).
Hippo source share