grpc-ecosystem / grpc-ecosystem/grpc-spring
An Authentication object was not found in the SecurityContext
- Dominant language
- Java
- Stars
- 3.7k
- Forks
- 858
- PR merge metrics
- No merged PRs in 30d
Description
Hi.
I trying to use annotation @security checks, my config:
```kotlin
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, proxyTargetClass = true)
class Configuration {
@Bean
fun grpcAuthenticationReader(projectRepository: ProjectRepository): GrpcAuthenticationReader {
return ApiTokenAuthenticationReader(projectRepository)
}
@GrpcGlobalServerInterceptor
fun securityContextCoroutineContextServerInterceptor(): ServerInterceptor {
return object : CoroutineContextServerInterceptor() {
override fun coroutineContext(call: ServerCall<*, *>, headers: Metadata): CoroutineContext {
return Dispatchers.Default + SecurityCoroutineContext()
}
}
}
}
```
Also added `SecurityCoroutineContext` from here https://github.com/yidongnan/grpc-spring-boot-starter/issues/462#issuecomment-921140929
Service
```kotlin
@GrpcService
class FileService() : FileServiceGrpcKt.FileServiceCoroutineImplBase() {
@Secured("ROLE_PROJECT")
override suspend fun getInfo(request: FileInfoRequest): FileInfoResponse {
....
}
}
```
But error occurs
> An Authentication object was not found in the SecurityContext
The method `ApiTokenAuthenticationReader.readAuthentication` was not called, therefore I suspect that problem not in my reader.
**The question**
1. what did i miss?
2. it's possible to get Principal inside a rpc method?
**Stacktraces and logs**
```
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:336) ~[spring-security-core-5.6.1.jar:5.6.1]
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:200) ~[spring-security-core-5.6.1.jar:5.6.1]
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:58) ~[spring-security-core-5.6.1.jar:5.6.1]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.14.jar:5.3.14]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753) ~[spring-aop-5.3.14.jar:5.3.14]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:698) ~[spring-aop-5.3.14.jar:5.3.14]
at biz.m.f.grpc.FileService$$EnhancerBySpringCGLIB$$9b66f240.getInfo() ~[281ddc99-6f03-48c8-ac53-fe4834233627/:na]
at f.api.FileServiceGrpcKt$FileServiceCoroutineImplBase$bindService$1.invoke(FileServiceOuterClassGrpcKt.kt:153) ~[281ddc99-6f03-48c8-ac53-fe4834233627/:na]
at f.api.FileServiceGrpcKt$FileServiceCoroutineImplBase$bindService$1.invoke(FileServiceOuterClassGrpcKt.kt:153) ~[281ddc99-6f03-48c8-ac53-fe4834233627/:na]
at io.grpc.kotlin.ServerCalls$unaryServerMethodDefinition$2$$special$$inlined$map$1$2.emit(Collect.kt:139) ~[grpc-kotlin-stub-1.2.0.jar:na]
at kotlinx.coroutines.flow.internal.SafeCollectorKt$emitFun$1.invoke(SafeCollector.kt:15) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
at kotlinx.coroutines.flow.internal.SafeCollectorKt$emitFun$1.invoke(SafeCollector.kt:15) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:77) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:59) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
at io.grpc.kotlin.HelpersKt$singleOrStatusFlow$1$invokeSuspend$$inlined$collect$1.emit(Collect.kt:139) ~[grpc-kotlin-stub-1.2.0.jar:na]
at kotlinx.coroutines.flow.internal.SafeCollectorKt$emitFun$1.invoke(SafeCollector.kt:15) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
at kotlinx.coroutines.flow.internal.SafeCollectorKt$emitFun$1.invoke(SafeCollector.kt:15) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:77) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:59) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
at io.grpc.kotlin.ServerCalls$serverCallListener$requests$1.invokeSuspend(ServerCalls.kt:227) ~[grpc-kotlin-stub-1.2.0.jar:na]
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) ~[kotlin-stdlib-1.6.10.jar:1.6.10-release-923(1.6.10)]
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) ~[kotlinx-coroutines-core-jvm-1.5.2.jar:na]
```
**The application's environment**
* Spring (boot): 2.6
* grpc-java: 1.43
* grpc-spring-boot-starter: latest
Contributor guide
Assessment
This issue has not been assessed yet.