title: When the Browser Refreshes the authentication token is cleared from the session storage - Springboot + Angular 9
body: <p>I am currently developing an application using Springboot microservices + Angular 9. I am using session storage to store my basic HTTP auth token. If I make changes in the Angular application the browser refreshes and the page doesn't log out on the browser + basic auth token is also cleared.</p>
  <p>How should I maintain the state of the token even if the browser refreshes?</p>
  <p>The token should get cleared when the user logs out from the application - This is my condition.</p>
  <p><em>NOTE:-</em> <em>I have used Spring Security in API gateway</em></p>
  <p>Find the below code snippet for the reference:</p>
  <pre><code>  export class AuthenticationService {
    USER_NAME_SESSION_ATTRIBUTE_NAME = 'authenticatedUser'
  
    public username: String;
    public password: String;
    
    constructor(private http: HttpClient) { }
  
    authenticationService(username: String, password: String) {
      return this.http.get('http://localhost:8765',
        { 
          headers: 
          { 
            authorization: this.createBasicAuthToken(username, password) },responseType : &quot;text&quot;  
       }).pipe(map((res) =&gt; {
                this.username = username;
                this.password = password;
                this.registerSuccessfulLogin(username, password);
        }));
    }
  
    createBasicAuthToken(username: String, password: String) {
      return 'Basic ' + window.btoa(username + &quot;:&quot; + password)
    }
  
    registerSuccessfulLogin(username, password) {
      sessionStorage.setItem(this.USER_NAME_SESSION_ATTRIBUTE_NAME, username)
    }
  
    logout() {
      sessionStorage.removeItem(this.USER_NAME_SESSION_ATTRIBUTE_NAME);
      this.username = null;
      this.password = null;
    }
  
    isUserLoggedIn() {
      let user = sessionStorage.getItem(this.USER_NAME_SESSION_ATTRIBUTE_NAME)
      if (user === null) return false
      return true
    }
  
    getLoggedInUserName() {
      let user = sessionStorage.getItem(this.USER_NAME_SESSION_ATTRIBUTE_NAME)
      if (user === null) return ''
      return user
    }
  }
  </code></pre>