Giter Club home page Giter Club logo

waa-lab's Introduction

Rahul Niraula-(614604)

Token Refresh Mechanism.

I Went with the following approach::

  1. User Sends both Access Token and Refresh Token in every request.
  2. The filter in the backend performs the following check.
    • if both the tokens are present in the header
      • if access token is expired and refresh token is still valid
        • extract username from refresh token and obtain userdetails using the username.
        • Generate new access token and refresh token
        • Set the user to the authentication context.
        • Attach both of the tokens to response header
        • In the client side, set up an interceptor to extract accessToken and refreshToken and send the updated tokens thereafter.

This way, the client just needs to send both of the tokens in the header and intercept the response to check if there is any updated token sent by the server.

Implementation Logic.

        if(authorizationHeader!=null && authorizationHeader.startsWith("Bearer ")){
            token=authorizationHeader.substring(7);
            try{
                email= jwtUtil.getUsernameFromToken(token);
                System.out.println("Auth User email "+email+"--"+token);
            }catch (ExpiredJwtException e){
                System.out.println("JWT is expired now try refresh token");
                String isRefreshToken=request.getHeader("isRefreshToken");
                if(isRefreshToken!=null){
                    try{
                        email= jwtUtil.getUsernameFromToken(isRefreshToken);
                        System.out.println("In Refresh part");
                        var userDetails=userDetailsService.loadUserByUsername(email);
                        token= jwtUtil.generateToken(userDetails);
                        var refreshToken=jwtUtil.generateRefreshToken(userDetails.getUsername());
                        response.setHeader("isRefreshHeader",refreshToken);
                        response.setHeader("accessToken",token);
                    }catch (ExpiredJwtException ex){
                        System.out.println("Refresh token is also expired"+ex.getMessage());
                    }
                }
            }
        }

waa-lab's People

Contributors

rahulniraula avatar

Watchers

James Cloos 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.