Search code examples
springspring-bootoauthaccess-token

How to increase the expiry date of access token + Spring boot + OAuth?


We have a restful API developed on spring-boot V1.5.7 and it is secured by OAuth with "password" grant type. We are using only access token, the refresh token is not being used. The validity of access token is set to 15 mins. Initially, we hit the token endpoint and get the token and consuming the services. Though the services are being consumed very frequently the access token is getting expired after 15 mins. What we are expected to do is, when the services are not being called for 15 mins only then the token should be expired.

Can anyone please help me on this?


Solution

  • Looks, First we need to know
    Why we used access token?
    Access token is used for accessing protected resource. It has a validity periods say for example 1min, 10min etc. After that time, token becomes invalid. To get a new valid token you should use refresh token.Though you can get a completely new token using your username and password. Even if you invoke any api within the expiry time though, the token invalid after the expiry time. If you don't invoke any api within the expiry time, token becomes also invalid. This is expected behavior.

    Why this is expected?
    Suppose you get an access token from server and access protected resource from server with access token. Somehow man in the middle get the token by sniffing packet. Then intruders can get easily access the resource as you can and as much time he want's. So technically we can say that, your account is being hacked.
    To prevent this attack, you should define a token validity periods that would be suit for your use case. So this is more secure than previous.
    I would strongly recommended that allow refresh token for your system.

    However You can also configure your system to reuse the token. This link may be a help.