Class BearerAuthenticationReader
java.lang.Object
net.devh.boot.grpc.server.security.authentication.BearerAuthenticationReader
- All Implemented Interfaces:
GrpcAuthenticationReader
Spring-Security has several token-based
AuthenticationProvider implementations (e.g. in
spring-security-web/oauth2 or spring-security-oauth2-resource-server), so you need to provide a Function that
wraps the extracted token in a Authentication object supported by your AuthenticationProvider.-
Constructor Summary
ConstructorsConstructorDescriptionBearerAuthenticationReader(Function<String, Authentication> tokenWrapper) Creates a new BearerAuthenticationReader with the given wrapper function. -
Method Summary
Modifier and TypeMethodDescriptionreadAuthentication(ServerCall<?, ?> call, Metadata headers) Tries to read theAuthenticationinformation from the given call and metadata.
-
Constructor Details
-
BearerAuthenticationReader
Creates a new BearerAuthenticationReader with the given wrapper function.Example-Usage:
For spring-security-web:
For spring-security-oauth2-resource-server:new BearerAuthenticationReader(token -> new PreAuthenticatedAuthenticationToken(token, null))new BearerAuthenticationReader(token -> new BearerTokenAuthenticationToken(token))- Parameters:
tokenWrapper- The function used to convert the token (without bearer prefix) into anAuthenticationobject.
-
-
Method Details
-
readAuthentication
Description copied from interface:GrpcAuthenticationReaderTries to read theAuthenticationinformation from the given call and metadata.Note: Implementations are free to throw an
AuthenticationExceptionif no credentials could be found in the call. If an exception is thrown by an implementation then the authentication attempt should be considered as failed and no subsequentGrpcAuthenticationReaders should be called. Additionally, the call will fail asStatus.UNAUTHENTICATED. If the call instead returnsnull, then the call processing will proceed unauthenticated.- Specified by:
readAuthenticationin interfaceGrpcAuthenticationReader- Parameters:
call- The call to get that send the request.headers- The metadata/headers as sent by the client.- Returns:
- The authentication object or null if no authentication is present.
-