提交 b766122e 编写于 作者: J Jason Song 提交者: GitHub

Merge pull request #757 from lepdou/user_type

add principal type check when get user info
......@@ -4,15 +4,28 @@ 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;
import org.springframework.security.core.userdetails.UserDetails;
import java.security.Principal;
public class SpringSecurityUserInfoHolder implements UserInfoHolder {
@Override
public UserInfo getUser() {
UserInfo userInfo = new UserInfo();
String userId = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
userInfo.setUserId(userId);
userInfo.setUserId(getCurrentUsername());
return userInfo;
}
private String getCurrentUsername() {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {
return ((UserDetails) principal).getUsername();
}
if (principal instanceof Principal) {
return ((Principal) principal).getName();
}
return String.valueOf(principal);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册