Code Monkey home page Code Monkey logo

jwt-auth-spring-boot-starter's Introduction

Maven Central Coverage Supported Java Version Sponsor

Getting Started

Add the following dependency to the build file

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

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

@GetMapping("/user")
fun getUser(@Identity claimSet: Map<String, Any>): Map<String, Any> {
    // 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 <jwt_token>. To resolve the token from another header or in a different format a bean implementing the RequestTokenResolver interface can be used.

Resolving token from Header x-custom-header: Token <jwt_token>

@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<String, Any> to enrich or transform the map into a typed object a bean implementing the ClaimSetTransformer interface can be used.

Transforming claimSet to a CustomUser object

data class CustomUser(val id: String, val name: String, val email: String)

@Bean
fun claimSetTransformer(): ClaimSetTransformer {
    return object : ClaimSetTransformer {
        override fun transform(claimSet: Map<String, Any>): 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

interface UserRepository : MongoRepository<String, DatabaseUser>

@Component
class UserLoadingClaimSetTransformer(
    private val userRepository: UserRepository
) : ClaimSetTransformer {
    override fun transform(claimSet: Map<String, Any>): 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
}

jwt-auth-spring-boot-starter's People

Contributors

dependabot[bot] avatar marvinschramm avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.