title: Accessing data in principal in spring
body: <p>My web application needs users login in via Facebook, then Facebook will return user's information like: Facebook id, username, email...</p>
  
  <p>Configuration: </p>
  
  <pre><code>@SpringBootApplication
  @EnableOAuth2Sso
  @RestController
  public class SocialApplication extends WebSecurityConfigurerAdapter {
  
      @RequestMapping("/user")
      public Principal user(Principal principal) {
          return principal;
      }
  
      public static void main(String[] args) {
          SpringApplication.run(SocialApplication.class, args);
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .csrf().disable()
                  .antMatcher("/**")
                  .authorizeRequests()
                  .antMatchers("/", "/login/**", "/webjars/**")
                  .permitAll()
                  .anyRequest()
                  .authenticated()
                  .and()
                  .logout().
                  logoutSuccessUrl("/")
                  .permitAll()
              ;
      }
  
      @Override
      public void configure(WebSecurity web) throws Exception {
          web.ignoring().antMatchers("/resources/**");
          web.ignoring().antMatchers("/static/**");
      }
  }
  </code></pre>
  
  <p>WebController:</p>
  
  <pre><code>@Controller
  public class WebController {
      @RequestMapping( value = {"/", "/index"})
      public String index(){
          return "index";
      }
  }
  </code></pre>
  
  <p>JavaScript front end let user login via Facebook:</p>
  
  <pre><code>&lt;body&gt;
          &lt;div class="container unauthenticated"&gt;
              With Facebook: &lt;a href="/login"&gt;click here&lt;/a&gt;
          &lt;/div&gt;
          &lt;div class="container authenticated" style="display:none"&gt;
              Logged in as: &lt;span id="user"&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;div&gt;
              &lt;button id="btnLogout" onClick="logout()" class="btn btn-primary"&gt;Logout&lt;/button&gt;
          &lt;/div&gt;
  
          &lt;script type="text/javascript"&gt;
  
              $("#btnLogout").hide();
  
              $.get("/user", function(data) {
                  $("#user").html(data.userAuthentication.details.name);
                  $(".unauthenticated").hide()
                  $(".authenticated").show()
                  $("#btnLogout").show();
              });
  
              var logout = function() {
                  $.post("/logout", function() {
                      $("#user").html('');
                      $(".unauthenticated").show();
                      $(".authenticated").hide();
                      $("#btnLogout").hide();
                  })
  
                  return true;
              }
          &lt;/script&gt;
      &lt;/body&gt;
  </code></pre>
  
  <p>After user login via Facebook if the web app is accessed to url: <code>/user</code>, it will return retsult:</p>
  
  <pre><code>{
      "authorities": [
          {
              "authority": "ROLE_USER"
          }
      ],
      "details": {
          "remoteAddress": "0:0:0:0:0:0:0:1",
          "sessionId": "C7C446FC3169FFDDD68ADA8497F19CFC",
          "tokenValue": "EAASZB5Q8S4WYBAGHGhX0XzdGfRIAhm1fMZBScqNYcueCEIBcc3ZCQgfjrFsDRk1LioAFEk8XeXZAxFEKHGpE9LJ83hvNJcRZACzvCx2h14z2eCPAnLJ00soZCNw5276QZCJmD5Kg6xK7MrrIZAm5PKIj40fHIPVO33W25fZBrcBdg0ZCZBYo2gXhD2ZC",
          "tokenType": "bearer",
          "decodedDetails": null
      },
      "authenticated": true,
      "userAuthentication": {
          "authorities": [
              {
                  "authority": "ROLE_USER"
              }
          ],
          "details": {
              "email": "deng.bunthai@yahoo.com",
              "name": "BunThai Deng",
              "id": "1676389552422181"
          },
          "authenticated": true,
          "principal": "1676389552422181",
          "credentials": "N/A",
          "name": "1676389552422181"
      },
      "principal": "1676389552422181",
      "clientOnly": false,
      "oauth2Request": {
          "clientId": "1335790916526438",
          "scope": [],
          "requestParameters": {},
          "resourceIds": [],
          "authorities": [],
          "approved": true,
          "refresh": false,
          "redirectUri": null,
          "responseTypes": [],
          "extensions": {},
          "refreshTokenRequest": null,
          "grantType": null
      },
      "credentials": "",
      "name": "1676389552422181"
  }
  </code></pre>
  
  <p>The question is how can I access to get email in the principal or session??</p>