CommonPageController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package com.yihu.hos.common;
  2. import com.fasterxml.jackson.core.type.TypeReference;
  3. import com.yihu.ehr.agModel.app.AppFeatureModel;
  4. import com.yihu.hos.common.constants.ContextAttributes;
  5. import com.yihu.hos.core.http.HTTPResponse;
  6. import com.yihu.hos.core.http.HttpClientKit;
  7. import com.yihu.hos.remoteManage.service.RemoteShellService;
  8. import com.yihu.hos.system.model.SystemUser;
  9. import com.yihu.hos.tenant.model.TenantSession;
  10. import com.yihu.hos.tenant.service.AuthenticateService;
  11. import com.yihu.hos.web.framework.model.AccessToken;
  12. import com.yihu.hos.web.framework.model.Envelop;
  13. import com.yihu.hos.web.framework.model.EnvelopExt;
  14. import com.yihu.hos.web.framework.util.controller.BaseController;
  15. import io.swagger.annotations.ApiParam;
  16. import org.apache.commons.io.IOUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  20. import org.springframework.security.core.Authentication;
  21. import org.springframework.security.core.GrantedAuthority;
  22. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  23. import org.springframework.security.core.context.SecurityContextHolder;
  24. import org.springframework.stereotype.Controller;
  25. import org.springframework.ui.Model;
  26. import org.springframework.util.StringUtils;
  27. import org.springframework.web.bind.annotation.*;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpServletResponse;
  30. import javax.servlet.http.HttpSession;
  31. import java.io.IOException;
  32. import java.io.PrintWriter;
  33. import java.util.*;
  34. /**
  35. * 数据采集配置页面
  36. * Created by hzp on 2015/8/12.
  37. */
  38. @RequestMapping("/")
  39. @Controller("commonPageController")
  40. public class CommonPageController extends BaseController {
  41. @Value("${spring.administrators}")
  42. private String saasAdmin;
  43. @Value("${spring.clientId}")
  44. private String clientId;
  45. @Value("${service-gateway.adminUrl}")
  46. public String adminUrl;
  47. @Value("${service-gateway.portalUrl}")
  48. public String portalUrl;
  49. @Autowired
  50. private AuthenticateService authenticateService;
  51. private RemoteShellService remoteShellService;
  52. /*
  53. 登录页面
  54. */
  55. @RequestMapping("loginPage")
  56. public String login(Model model, HttpServletRequest request, HttpServletResponse response) {
  57. HttpSession session = request.getSession();
  58. try {
  59. boolean auth = authenticateService.auth(session, saasAdmin);
  60. if (!auth) {
  61. //授权失败
  62. response.setCharacterEncoding("UTF-8");
  63. response.setHeader("Content-type", "text/html;charset=UTF-8");
  64. PrintWriter out = null;
  65. try {
  66. out = response.getWriter();
  67. out.print("<script>alert('登录地址有误-用户授权失败!');</script>");
  68. response.sendRedirect("/esb/loginPage");
  69. out.flush();
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. } finally {
  73. IOUtils.closeQuietly(out);
  74. }
  75. } else {
  76. remoteShellService.start();
  77. }
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. }
  81. model.addAttribute("contentPage", "/common/login");
  82. return "pageView";
  83. }
  84. /**
  85. * 租户登录页面
  86. *
  87. * @param model
  88. * @param tenantName
  89. * @param request
  90. * @return
  91. */
  92. @RequestMapping("{tenantName}/loginPage")
  93. public String tenantLogin(Model model,
  94. @PathVariable(name = "tenantName") String tenantName,
  95. HttpServletRequest request, HttpServletResponse response) throws IOException {
  96. HttpSession session = request.getSession();
  97. boolean auth = false;
  98. try {
  99. auth = authenticateService.auth(session, tenantName);
  100. System.out.println("auth: " + auth);
  101. if (!auth) {
  102. //授权失败,切换回管理平台数据库
  103. auth = authenticateService.auth(session, saasAdmin);
  104. response.setCharacterEncoding("UTF-8");
  105. response.setHeader("Content-type", "text/html;charset=UTF-8");
  106. PrintWriter out = null;
  107. try {
  108. out = response.getWriter();
  109. out.print("<script>alert('请求地址不存在!');</script>");
  110. response.sendRedirect("/esb/loginPage");
  111. out.flush();
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. } finally {
  115. IOUtils.closeQuietly(out);
  116. }
  117. } else {
  118. remoteShellService.start();
  119. }
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. }
  123. model.addAttribute("contentPage", "/common/login");
  124. return "pageView";
  125. }
  126. /*
  127. 首页页面
  128. */
  129. @RequestMapping("indexPage")
  130. public String index(HttpServletRequest request, Model model) {
  131. HttpSession session = request.getSession();
  132. SystemUser user = (SystemUser) session.getAttribute("userInfo");
  133. TenantSession tenantSession = (TenantSession) session.getAttribute(ContextAttributes.TENANT_SESSION);
  134. model.addAttribute("userName", user.getUserName());
  135. model.addAttribute("tenant", tenantSession.getTenant());
  136. //获取菜单
  137. String menu = "[{id: 1, text: '任务管理',icon:'${staticRoot}/images/index/menu2_icon.png'},\n" +
  138. " {id: 11, pid: 1, text: '任务跟踪', url: '${contextRoot}/datacollect/trackJob',targetType:'1'},\n" +
  139. " {id: 12, pid: 1, text: '任务补采', url: '${contextRoot}/datacollect/repeatDatacollect'},\n" +
  140. " {id: 13, pid: 1, text: '任务配置', url: '${contextRoot}/datacollect/configJob'},\n" +
  141. " {id: 2, text: '标准管理',icon:'${staticRoot}/images/index/menu3_icon.png'},\n" +
  142. " {id: 21, pid: 2, text: '集成标准', url: '${contextRoot}/integration/initial/standard'},\n" +
  143. " {id: 22, pid: 2, text: '应用标准', url: '${contextRoot}/integration/initial/application'},\n" +
  144. " {id: 23, pid: 2, text: '适配方案', url: '${contextRoot}/adapterPlan/initial'},\n" +
  145. " {id: 3, text: '资源管理',icon:'${staticRoot}/images/index/menu4_icon.png'},\n" +
  146. " {id: 31, pid: 3, text: '资源注册', url: '${contextRoot}/resource/resource/initial'},\n" +
  147. " {id: 32, pid: 3, text: '资源浏览', url: '${contextRoot}/resource/resourcePage'},\n" +
  148. " {id: 34, pid: 3, text: '资源分类', url: '${contextRoot}/resource/rsCategory/initial'},\n" +
  149. " {id: 35, pid: 3, text: '业务资源', url: '${contextRoot}/resourceRest/initial'},\n" +
  150. " {id: 4, text: '维度管理',icon:'${staticRoot}/images/index/menu5_icon.png'},\n" +
  151. " {id: 41, pid: 4, text: '维度配置', url: '${contextRoot}/dimension/dimension'},\n" +
  152. " {id: 42, pid: 4, text: '维度类别配置', url: '${contextRoot}/dimension/dimensioncatetory'},\n" +
  153. " {id: 9, text: '系统配置',icon:'${staticRoot}/images/index/menu6_icon.png'},\n" +
  154. " {id: 91, pid: 9, text: '机构配置', url: '${contextRoot}/org/initial'},\n" +
  155. " {id: 92, pid: 9, text: '数据源配置', url: '${contextRoot}/datasource/configSources'},\n" +
  156. " {id: 93, pid: 9, text: '菜单配置', url: '${contextRoot}/menu/initial'},\n" +
  157. " {id: 100, pid: 9, text: '菜单按钮配置', url: '${contextRoot}/menu/menuAction/initial'},\n" +
  158. " {id: 94, pid: 9, text: '用户管理', url: '${contextRoot}/user/initial'},\n" +
  159. " {id: 95, pid: 9, text: '角色管理', url: '${contextRoot}/role/initial'},\n" +
  160. " {id: 96, pid: 9, text: '权限管理', url: '${contextRoot}/authority/initial'},\n" +
  161. " {id: 97, pid: 9, text: '字典管理', url: '${contextRoot}/dict/initial' },\n" +
  162. " {id: 98, pid: 9, text: '系统参数', url: '${contextRoot}/param/initial'},\n" +
  163. " {id: 99, pid: 9, text: '<spring:message code=\"title.app.manage\"/>', url: '${contextRoot}/app/initial'}]";
  164. model.addAttribute("menu", menu);
  165. model.addAttribute("contentPage", "/common/index");
  166. return "pageView";
  167. }
  168. /**
  169. * oauth2 自动登录后首页
  170. *
  171. * @param tenantName 租户名称
  172. * @param request
  173. * @param model
  174. * @return
  175. */
  176. @RequestMapping("{tenantName}/oauth2/index")
  177. public String tenantIndex(
  178. @PathVariable(name = "tenantName") String tenantName,
  179. HttpServletRequest request, Model model) {
  180. HttpSession session = request.getSession();
  181. boolean auth = false;
  182. String clientId = request.getParameter(ContextAttributes.CLIENTID);
  183. String token = request.getParameter(ContextAttributes.ACCESSTOKEN);
  184. String loginName = request.getParameter(ContextAttributes.LOGIN_NAME);
  185. //TODO 根据token和clientId 获取用户信息
  186. SystemUser userInfo = new SystemUser();
  187. userInfo.setLoginCode(loginName);
  188. userInfo.setUserName("管理员");
  189. session.setAttribute("userInfo", userInfo);
  190. try {
  191. auth = authenticateService.auth(session, tenantName);
  192. if (!auth) {
  193. model.addAttribute("contentPage", "/common/tokenValidFail");
  194. return "pageView";
  195. }
  196. } catch (Exception e) {
  197. e.printStackTrace();
  198. }
  199. model.addAttribute("userName", "admin");
  200. model.addAttribute("tenant", tenantName);
  201. //获取菜单
  202. String menu = "[{id: 1, text: '任务管理',icon:'${staticRoot}/images/index/menu2_icon.png'},\n" +
  203. " {id: 11, pid: 1, text: '任务跟踪', url: '${contextRoot}/datacollect/trackJob',targetType:'1'},\n" +
  204. " {id: 12, pid: 1, text: '任务补采', url: '${contextRoot}/datacollect/repeatDatacollect'},\n" +
  205. " {id: 13, pid: 1, text: '任务配置', url: '${contextRoot}/datacollect/configJob'},\n" +
  206. " {id: 2, text: '标准管理',icon:'${staticRoot}/images/index/menu3_icon.png'},\n" +
  207. " {id: 21, pid: 2, text: '集成标准', url: '${contextRoot}/integration/initial/standard'},\n" +
  208. " {id: 22, pid: 2, text: '应用标准', url: '${contextRoot}/integration/initial/application'},\n" +
  209. " {id: 23, pid: 2, text: '适配方案', url: '${contextRoot}/adapterPlan/initial'},\n" +
  210. " {id: 3, text: '资源管理',icon:'${staticRoot}/images/index/menu4_icon.png'},\n" +
  211. " {id: 31, pid: 3, text: '资源注册', url: '${contextRoot}/resource/resource/initial'},\n" +
  212. " {id: 32, pid: 3, text: '资源浏览', url: '${contextRoot}/resource/resourcePage'},\n" +
  213. " {id: 34, pid: 3, text: '资源分类', url: '${contextRoot}/resource/rsCategory/initial'},\n" +
  214. " {id: 35, pid: 3, text: '业务资源', url: '${contextRoot}/resourceRest/initial'},\n" +
  215. " {id: 4, text: '维度管理',icon:'${staticRoot}/images/index/menu5_icon.png'},\n" +
  216. " {id: 41, pid: 4, text: '维度配置', url: '${contextRoot}/dimension/dimension'},\n" +
  217. " {id: 42, pid: 4, text: '维度类别配置', url: '${contextRoot}/dimension/dimensioncatetory'},\n" +
  218. " {id: 9, text: '系统配置',icon:'${staticRoot}/images/index/menu6_icon.png'},\n" +
  219. " {id: 91, pid: 9, text: '机构配置', url: '${contextRoot}/org/initial'},\n" +
  220. " {id: 92, pid: 9, text: '数据源配置', url: '${contextRoot}/datasource/configSources'},\n" +
  221. " {id: 93, pid: 9, text: '菜单配置', url: '${contextRoot}/menu/initial'},\n" +
  222. " {id: 100, pid: 9, text: '菜单按钮配置', url: '${contextRoot}/menu/menuAction/initial'},\n" +
  223. " {id: 94, pid: 9, text: '用户管理', url: '${contextRoot}/user/initial'},\n" +
  224. " {id: 95, pid: 9, text: '角色管理', url: '${contextRoot}/role/initial'},\n" +
  225. " {id: 96, pid: 9, text: '权限管理', url: '${contextRoot}/authority/initial'},\n" +
  226. " {id: 97, pid: 9, text: '字典管理', url: '${contextRoot}/dict/initial' },\n" +
  227. " {id: 98, pid: 9, text: '系统参数', url: '${contextRoot}/param/initial'},\n" +
  228. " {id: 99, pid: 9, text: '<spring:message code=\"title.app.manage\"/>', url: '${contextRoot}/app/initial'}]";
  229. model.addAttribute("menu", menu);
  230. model.addAttribute("contentPage", "/common/index");
  231. return "pageView";
  232. }
  233. /**
  234. * oauth2 自动登陆中间页
  235. *
  236. * @param model
  237. * @param tenantName
  238. * @return
  239. */
  240. @RequestMapping(value = "{tenantName}/oauth2/signin")
  241. public String signin(Model model,
  242. @PathVariable(name = "tenantName") String tenantName) {
  243. model.addAttribute("tenantName", tenantName);
  244. model.addAttribute("contentPage", "common/signin");
  245. model.addAttribute("successFlg", true);
  246. return "pageView";
  247. }
  248. /*
  249. * oauth2 自动登录
  250. */
  251. @RequestMapping(value = "oauth2/autoLogin", method = RequestMethod.POST)
  252. @ResponseBody
  253. public Envelop autoLogin(Model model,
  254. HttpServletRequest request,
  255. @ApiParam(name = "token")
  256. @RequestParam String token) throws Exception {
  257. Envelop result = new Envelop();
  258. try {
  259. Map<String, String> params = new HashMap<>();
  260. params.put("clientId", clientId);
  261. params.put("accessToken", token);
  262. HTTPResponse response = HttpClientKit.post(portalUrl + "/oauth/validToken", params);
  263. if (response.getStatusCode() != 200) {
  264. System.out.println("获取 token 请求失败!");
  265. return null;
  266. }
  267. Map map = objectMapper.readValue(response.getBody(), Map.class);
  268. if ((Boolean) map.get("successFlg")) {
  269. AccessToken accessToken = objectMapper.readValue(objectMapper.writeValueAsString(map.get("data")), AccessToken.class);
  270. String loginName = accessToken.getUser();
  271. //验证通过。赋值session中的用户信息
  272. HTTPResponse userResponse = HttpClientKit.get(adminUrl + "/users/" + loginName, params);
  273. result = (Envelop) this.objectMapper.readValue(userResponse.getBody(), Envelop.class);
  274. Collection<GrantedAuthority> gas = new ArrayList<>();
  275. if (result.isSuccessFlg()) {
  276. String ex = this.objectMapper.writeValueAsString(result.getObj());
  277. Map userMap = objectMapper.readValue(ex, Map.class);
  278. //TODO 设置当前登录用户
  279. String userId = userMap.get("id").toString();
  280. List<AppFeatureModel> features = getUserFeatures(userId);
  281. if (features != null) {
  282. for (int i = 0; i < features.size(); i++) {
  283. String url = features.get(i).getUrl();
  284. if (!StringUtils.isEmpty(url))
  285. gas.add(new SimpleGrantedAuthority(url));
  286. }
  287. }
  288. } else {
  289. return failed(result.getErrorMsg());
  290. }
  291. //生成认证token
  292. Authentication AuthenticationToken = new UsernamePasswordAuthenticationToken(loginName, "", gas);
  293. //将信息存放到SecurityContext
  294. SecurityContextHolder.getContext().setAuthentication(AuthenticationToken);
  295. return success(accessToken);
  296. } else {
  297. String msg = String.valueOf(map.get("message"));
  298. return failed(msg);
  299. }
  300. } catch (Exception e) {
  301. e.printStackTrace();
  302. return failed(e.getMessage());
  303. }
  304. }
  305. /*
  306. 系统主页
  307. */
  308. @RequestMapping("homePage")
  309. public String home(Model model) {
  310. model.addAttribute("contentPage", "/common/home");
  311. return "partView";
  312. }
  313. private List<AppFeatureModel> getUserFeatures(String userId) throws Exception {
  314. Map<String, String> params = new HashMap<>();
  315. params.put("user_id", userId);
  316. HTTPResponse resultStr = HttpClientKit.get(adminUrl + "/roles/user/features", params);
  317. EnvelopExt<AppFeatureModel> envelop =
  318. (EnvelopExt<AppFeatureModel>) objectMapper.readValue(resultStr.getBody(), new TypeReference<EnvelopExt<AppFeatureModel>>() {
  319. });
  320. if (envelop.isSuccessFlg()) {
  321. return envelop.getDetailModelList();
  322. }
  323. throw new Exception(resultStr.getBody());
  324. }
  325. @Autowired
  326. public void setRemoteShellService(RemoteShellService remoteShellService) {
  327. this.remoteShellService = remoteShellService;
  328. }
  329. }