title: Why Principal is null after successful auth in Spring REST app?
body: <p>I'm implementing an authentication in my Spring rest app right now, so I send a request from a Electron.js app and Spring handles everything.</p>
  
  <p>So right now if I sign in via browser it saves my object and returns Principal successfully. But if I sign in and try to send a request to return that Principal via Electron.js app, it throws NullException.</p>
  
  <pre class="lang-java prettyprint-override"><code>@GetMapping("/user")
  public String user(Principal principal)
  {
      return principal.getName(); // null via rest app (electron app), not null via browser (authenticated)
  }
  
  @GetMapping("/loginRequest") // this is method that requires user's credentials
  public UserModel login(Principal principal)
  {
     return userDAO.findByUsername(principal.getName()); // signs in on both (browser and rest app), returns json object
  }
  </code></pre>
  
  <p>Configuration looks this way:</p>
  
  <pre class="lang-java prettyprint-override"><code>    @Autowired
      private MongoUserDetailsService userDetailsService;
  
      @Override
      public void configure(HttpSecurity http) throws Exception
      {
          http.cors().and()
                  .csrf().disable()
                  .authorizeRequests()
                  .antMatchers("/loginRequest").authenticated()
                  .and()
                  .httpBasic();
      }
  
      @Override
      public void configure(AuthenticationManagerBuilder builder) throws Exception
      {
          builder.userDetailsService(userDetailsService);
      }
  
      @Bean
      public PasswordEncoder passwordEncoder()
      {
          return new BCryptPasswordEncoder();
      }
  </code></pre>
  
  <p>Request from Electron.js</p>
  
  <pre class="lang-js prettyprint-override"><code>axios(API + 'loginRequest', {
            method: 'GET',
            auth: {
              username: this.username, // Both username &amp; pass are taken from fields
              password: this.password
            }
          })
          .then(response =&gt; {
            this.userObject = response.data;
          }).catch(err =&gt; this.showError('Username or password is invalid', 2));
  </code></pre>
  
  <p>Thank you in advance!</p>