How to generate and authorized java spring controller using JWT
-
I am a starter plan user and want to implement JWT using client credentials flow in my Java Spring Boot Application. I generated the license in the fusion auth portal and set up the auth docker instance. I am getting the below error when generating the JWT token using client_id and secret.
{ "error": "not_licensed", "error_description": "You must enter a valid license Id in order to use the Entity Management features of FusionAuth, which include the Client Credentials Grant.", "error_reason": "not_licensed" }
Please help me in forming the correct token generation call and then use the same token to authorize my controller.
curl --location --request POST 'http://<base_url>:9011/oauth2/token?grant_type=client_credentials&client_id=<>&client_secret=<>&redirect_uri=<url>' \ --data ''
-
It looks like you haven't entered the license Id on your instance. You can do so by following the steps outlined here:
-
@dan Thanks. I am able to add the license now.
-
@dan I have a Java Rest API. How to authenticate the APIs using generated tokens? What are the steps?
We need to pass the token in the API header as Authorization: Bearer<token> But what is the process so that I can validate the endpoint with the valid token if the token is invalid or does not have the required roles or scope then I should get 401 else I should be able to access the API successfully.
-
@shyamsundar-k said in How to generate and authorized java spring controller using JWT:
We need to pass the token in the API header as Authorization: Bearer<token> But what is the process so that I can validate the endpoint with the valid token if the token is invalid or does not have the required roles or scope then I should get 401 else I should be able to access the API successfully.
Once you have a token in your API, you can validate it in two different ways. But it's worth noting that to validate the token, you must validate the signature and then the claims.
First option: use a library to validate the signature. Most languages have options. For java, you can use fusionauth-jwt, the readme has sample code.
Second option: use the validate API. You could use the FusionAuth client library to make this call if you'd like.
The first means you have to pick a library. The second means you have to make a network call.
Either way, after you validate the signature, you need to check the claims (issuer, audience, expiration, custom claims) to make sure they are what you expect.
Here's more about how to consume a JWT.