package com.ctrip.framework.apollo.portal.spi.springsecurity;

import com.ctrip.framework.apollo.portal.entity.bo.UserInfo;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

public class SpringSecurityUserInfoHolder implements UserInfoHolder {

  @Override
  public UserInfo getUser() {
    UserInfo userInfo = new UserInfo();
    String userId = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
    userInfo.setUserId(userId);
    return userInfo;
  }
}

@@ -308,16 +308,56 @@
 ils.User
+Details;%0A%0Aimport java.security.Principal
 ;%0A%0Apubli
@@ -512,150 +512,410 @@
     
-String userId = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();%0A    userInfo.setUserId(userId
+userInfo.setUserId(getCurrentUsername());%0A    return userInfo;%0A  %7D%0A%0A  private String getCurrentUsername() %7B%0A    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();%0A    if (principal instanceof UserDetails) %7B%0A      return ((UserDetails) principal).getUsername();%0A    %7D%0A    if (principal instanceof Principal) %7B%0A      return ((Principal) principal).getName(
 );%0A
+    %7D%0A
     
@@ -921,24 +921,42 @@
  return 
-userInfo
+String.valueOf(principal)
 ;%0A  %7D%0A
+%0A
 %7D%0A

add principal type check when get user info