https://github.com/hndrs/jwt-auth-spring-boot-starter
Spring-Boot-Starter that helps consuming JWT Identities in spring restcontrollers
https://github.com/hndrs/jwt-auth-spring-boot-starter
authentication jwk jwt jwt-authentication kotlin-spring-boot spring-boot spring-boot-starter
Last synced: 7 months ago
JSON representation
Spring-Boot-Starter that helps consuming JWT Identities in spring restcontrollers
- Host: GitHub
- URL: https://github.com/hndrs/jwt-auth-spring-boot-starter
- Owner: hndrs
- License: mit
- Created: 2021-03-08T21:00:07.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-03-06T18:06:37.000Z (over 2 years ago)
- Last Synced: 2025-01-29T17:24:31.927Z (8 months ago)
- Topics: authentication, jwk, jwt, jwt-authentication, kotlin-spring-boot, spring-boot, spring-boot-starter
- Language: Kotlin
- Homepage:
- Size: 108 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://search.maven.org/artifact/io.hndrs/jwt-auth-spring-boot-starter)
[](https://sonarcloud.io/dashboard?id=hndrs_jwt-auth-spring-boot-starter)
[]()
[](https://github.com/sponsors/marvinschramm)# Getting Started
Add the following dependency to the build file
```kotlin
dependencies {
...
implementation("io.hndrs:jwt-auth-spring-boot-starter:1.0.0")
...
}
```#### Configuration
Adding the issuer and the jwks path for the verification to the ```application.properties```
```properties
hndrs.jwt.key-store-path=https://domain.auth0.com/.well-known/jwks.json
```#### Controller
To inject the claimSet into a ```RestController``` method just use the ```@Identity``` annotation on the parameter
```kotlin
@GetMapping("/user")
fun getUser(@Identity claimSet: Map): Map {
// do something with the user claimSet
return claimSet
}```
#### RequestTokenResolver
By default the jwt token will be resolved from the ```Authorization``` Header in the following
format ```Bearer ```. To resolve the token from another header or in a different format a bean implementing
the [RequestTokenResolver](src/main/kotlin/io/hndrs/jwt/RequestTokenResolver.kt)
interface can be used.Resolving token from Header ```x-custom-header: Token ```
```kotlin
@Bean
fun requestTokenResolver(): RequestTokenResolver {
return object : RequestTokenResolver {override fun tokenHeaderName(): String {
return "x-custom-header"
}override fun tokenResolver(headerValue: String?): String {
if (headerValue == null) {
throw UnauthorizedIdentityException("${tokenHeaderName()} Header not present")
}
if (!headerValue.startsWith("Token ")) {
throw UnauthorizedIdentityException("Token is not present")
}return headerValue.replace("Token ", "")
}
}
}```
#### ClaimSetTransformer
By default the claimSet is represented as a ```Map``` to enrich or transform the map into a typed object a
bean implementing the [ClaimSetTransformer](src/main/kotlin/io/hndrs/jwt/ClaimSetTransformer.kt)
interface can be used.> Transforming claimSet to a CustomUser object
```kotlin
data class CustomUser(val id: String, val name: String, val email: String)@Bean
fun claimSetTransformer(): ClaimSetTransformer {
return object : ClaimSetTransformer {
override fun transform(claimSet: Map): Any {
return CustomUser(
claimSet["sub"] as String,
claimSet["name"] as String,
claimSet["email"] as String,
)
}
}
}// transformed object
@GetMapping("/user")
fun getUser(@Identity user: CustomUser): CustomUser {
// do something with the user claimSet
return user
}
```> Loading a user object
```kotlin
interface UserRepository : MongoRepository
@Component
class UserLoadingClaimSetTransformer(
private val userRepository: UserRepository
) : ClaimSetTransformer {
override fun transform(claimSet: Map): Any {
return userRepository.findById(claimSet["sub"] as String)
}
}// transformed object
@GetMapping("/user")
fun getUser(@Identity user: DatabaseUser): CustomUser {
// do something with the user claimSet
return user
}
```