UserUtil.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package com.huaxu.util;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.huaxu.model.LoginUser;
  5. import com.huaxu.model.ProgramItem;
  6. import org.springframework.security.authentication.AnonymousAuthenticationToken;
  7. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  8. import org.springframework.security.core.Authentication;
  9. import org.springframework.security.core.context.SecurityContextHolder;
  10. import org.springframework.security.oauth2.provider.OAuth2Authentication;
  11. import java.util.LinkedHashMap;
  12. import java.util.List;
  13. public class UserUtil {
  14. /**
  15. *获取当前用户
  16. */
  17. public static LoginUser getCurrentUser() {
  18. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  19. if (authentication != null) {
  20. if (authentication instanceof AnonymousAuthenticationToken) {
  21. return new LoginUser(null);
  22. }
  23. if (authentication instanceof UsernamePasswordAuthenticationToken) {
  24. return (LoginUser) authentication.getPrincipal();
  25. }
  26. if(authentication instanceof OAuth2Authentication){
  27. OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
  28. if(oAuth2Authentication.getPrincipal() instanceof LoginUser){
  29. return (LoginUser) oAuth2Authentication.getPrincipal();
  30. }else {
  31. LinkedHashMap<String,Object> details = (LinkedHashMap<String,Object>) oAuth2Authentication.getUserAuthentication().getDetails();
  32. LinkedHashMap<String,Object> principalMap = (LinkedHashMap<String, Object>) details.get("principal");
  33. JSONObject userJson = (JSONObject) JSONObject.toJSON(principalMap);
  34. LoginUser loginUser = new LoginUser(null);
  35. loginUser.setId((Integer) userJson.get("id"));
  36. loginUser.setUsername((String) userJson.get("username"));
  37. //loginUser.setName((String) userJson.get("name"));
  38. loginUser.setPhoneNumber((String) userJson.get("phoneNumber"));
  39. loginUser.setType((String) userJson.get("type"));
  40. //loginUser.setName(user.getUsername());
  41. loginUser.setCompanyId(userJson.getInteger("companyId"));
  42. loginUser.setDepartmentId(userJson.getInteger("departmentId"));
  43. loginUser.setTenantId(userJson.getString("tenantId"));
  44. JSONArray programItemListArray = userJson.getJSONArray("programItemList");
  45. loginUser.setPermissonType(userJson.getInteger("permissonType"));
  46. JSONArray appIds = userJson.getJSONArray("appIds");
  47. if (programItemListArray != null) {
  48. List<ProgramItem> collection =
  49. JSONObject.parseArray(programItemListArray.toJSONString(), ProgramItem.class);
  50. loginUser.setProgramItemList(collection);
  51. }
  52. if(appIds!=null){
  53. List<String> appIdString = JSONObject.parseArray(appIds.toJSONString(), String.class);
  54. loginUser.setAppIds(appIdString);
  55. }
  56. return loginUser;
  57. }
  58. }
  59. }
  60. return new LoginUser(null);
  61. }
  62. }