123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package com.ghsc.partybuild.controller;
- import com.ghsc.partybuild.AppConfig;
- import com.ghsc.partybuild.model.CfUsers;
- import com.ghsc.partybuild.service.LogService;
- import com.ghsc.partybuild.service.RoleService;
- import com.ghsc.partybuild.service.UserService;
- import com.ghsc.partybuild.shiro.JwtUtils;
- import com.ghsc.partybuild.util.RemoteHelper;
- import com.ghsc.partybuild.util.StringUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.FileInputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @Slf4j
- @Controller
- @RequestMapping(value = "/")
- public class AppController {
- private final Logger logger;
- @Autowired
- private AppConfig appConfig;
- @Autowired
- private UserService userService;
- @Autowired
- private JwtUtils jwtUtils;
- @Autowired
- private RoleService roleService;
- @Autowired
- private LogService logService;
- @Autowired
- RemoteHelper remoteHelper;
- @Autowired
- private StringUtils stringUtils;
- public AppController() {
- logger = LoggerFactory.getLogger(this.getClass());
- }
- @GetMapping("")
- public String app(HttpServletRequest request, HttpServletResponse response, String ticket) {
- if (appConfig.isPortalLogin && !stringUtils.IsNullOrEmpty(ticket)) {
- if (portalLogin(request, response, ticket)) {
- return "redirect:/app/main/index.html#!/portalLogin";
- }
- }
- return "redirect:/app/main/index.html";
- }
- @GetMapping("web")
- public String web(HttpServletRequest request, HttpServletResponse response, String ticket) {
- if (appConfig.isPortalLogin && !stringUtils.IsNullOrEmpty(ticket)) {
- if (portalLogin(request, response, ticket)) {
- return "redirect:/app/main/index.html#!/portalLogin";
- }
- }
- return "redirect:/app/main/index.html";
- }
- @GetMapping("mobile")
- public String mobile(String routePath, String code) throws UnsupportedEncodingException {
- log.info("mobile:routePath=" + routePath + ",code=" + code);
- if (!stringUtils.IsNullOrEmpty(routePath))
- return "redirect:/mobile/index.html/#/index?routePath=" + URLEncoder.encode(routePath, "UTF-8") + "&code=" + code;
- return "redirect:/mobile/index.html/#/index?code=" + (!stringUtils.IsNullOrEmpty(code) ? code : "");
- }
- /**
- * 单点登录,验证ticket
- *
- * @param request
- * @param response
- * @param ticket
- * @return
- */
- public Boolean portalLogin(HttpServletRequest request, HttpServletResponse response, String ticket) {
- Boolean result = false;
- try {
- String validateUrl = appConfig.portal_oauthUrl + "/lyuapServer/serviceValidate";
- logger.info("JXCasLogin,ticket:" + ticket);
- Map<String, String> mapParams = new HashMap<>();
- mapParams.put("ticket", ticket);
- mapParams.put("service", appConfig.portal_webUrl);
- FileInputStream streamCer = new FileInputStream(appConfig.certPath + "/_.gzws.edu.cn.crt");
- String reqData = remoteHelper.SSLGet(mapParams, validateUrl, "UTF-8", streamCer);
- //String reqData="<cas:authenticationSuccess><cas:user>2001001www</cas:user><cas:attributes>...</cas:attributes></cas:authenticationSuccess>";
- logger.info("PortalLogin,reqData:" + reqData);
- int i = reqData.indexOf("<cas:user>");
- int j = reqData.indexOf("</cas:user>");
- String userId = reqData.substring(i + "<cas:user>".length(), j);
- /*String userId = "測試賬號";*/
- logger.info("PortalLogin,userId:" + userId);
- CfUsers User = userService.getUserByKey(userId);
- if (org.apache.commons.lang3.StringUtils.isBlank(User.getUserid())) {
- User = userService.getUserByOAName(userId);
- }
- if (User != null && org.apache.commons.lang3.StringUtils.isNotBlank(User.getUsername())) {
- Cookie cookie = new Cookie(jwtUtils.getTokenName(), jwtUtils.generateToken(User.getUserid()));
- cookie.setHttpOnly(true);
- cookie.setPath("/");
- response.addCookie(cookie);
- List<HashMap<String, Object>> roleList = this.roleService.getRoleByUserName(User.getUsername());
- if (roleList == null || roleList.isEmpty()) {
- this.roleService.insertUserRole(User.getUsername(), "0b45886a-a8db-4f85-af76-61a8ea7c1dab");
- }
- userService.loginForceStatu(User.getUserid(), 0);
- logService.log("用户单点登录", User.getUserid(), "PortalLogin");
- result = true;
- } else {
- logger.info("PortalLogin,单点登陆失败!");
- }
- } catch (Exception ex) {
- logger.error("单点登陆异常:" + ex);
- }
- return result;
- }
- }
|