1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| /** * 跳转到猎豹系统 * * @param response * @throws Exception */ @PostMapping(value = "/cheetah", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public String cheetah(@RequestBody SSOVO ssovo, HttpServletRequest request, HttpServletResponse response) throws Exception { try { // 验证license if (!licenseService.isValid()) { LOGGER.error("license is invalid"); return validateTokenError(request, LICENSE_ERROR_MSG); } //解析token Context.Token userToken = Context.getUserInfoFromToken(ssovo.getToken()); if (isNullOrEmpty(userToken.getUserName()) || isNullOrEmpty(userToken.getPassword())) { LOGGER.warn("token is invalid:{}", ssovo.getToken()); return validateTokenError(request); } LOGGER.info("当前单点登录的用户信息为:{}", JSON.toJSONString(userToken)); //验证内置用户是否存在,不存在则创建 SSOUserVO user = ssoService.checkUser(userToken.getUserName(), Context.getCmsContext()); if (user != null) { // 执行登录 user.setPassword(userToken.getPassword()); return ssoLogin(request, response, user); } //异常时跳转到登录页 return validateTokenError(request); } catch (Exception e) { LOGGER.error("sso登录失败:{}", e.getMessage()); return validateTokenError(request); } }
private String validateTokenError(HttpServletRequest request) { return validateError(request, SSO_VERIFICATION_ERROR_MSG); }
private String validateTokenError(HttpServletRequest request, String msg) { return validateError(request, msg); }
private String validateError(HttpServletRequest request, String msg) { HttpSession session = request.getSession(); if (session != null) { //使session失效 session.invalidate(); } SSOErrorVO errorVo = new SSOErrorVO(SSO_VERIFICATION_ERROR, msg); return JSON.toJSONString(errorVo); } /** * 执行登录 * * @param request * @param response * @param userToken * @return * @throws IOException * @throws ServletException */ private String ssoLogin(HttpServletRequest request, HttpServletResponse response, SSOUserVO userToken) throws IOException, ServletException { try { //登录 UsernamePasswordAuthenticationToken authReq = new UsernamePasswordAuthenticationToken(userToken.getUserName(), userToken.getPassword()); authReq.setDetails(new WebAuthenticationDetails(request)); Authentication auth = authenticationManagerBean.authenticate(authReq); SecurityContextHolder.getContext().setAuthentication(auth); HttpSession session = request.getSession(true); // 永不超时 session.setMaxInactiveInterval(-1); //TODO 静态导入 session.setAttribute(SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext()); baymaxLoginSuccessHandler.onAuthenticationSuccess(request, response, auth); } catch (AuthenticationException failed) { LOGGER.warn( "sso: InternalAuthenticationServiceException occurred while trying to authenticate the user.", failed); SecurityContextHolder.clearContext(); baymaxAuthenticationFailureHandler.onAuthenticationFailure(request, response, failed); validateTokenError(request); }
return null; }
/** * 根据用户名,获取用户的token * * @param userName * @param response * @return */ @RequestMapping(value = "/getToken/{userName}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public String getToken(@PathVariable(value = "userName", required = false) String userName, HttpServletResponse response) {
try { return Context.createToken(userName, PasswordUtil.getPlaintextPwd()); } catch (Exception e) { LOGGER.error("获取token失败:{}", e.getMessage()); formatErrorResponse(response, HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); return null; } }
private void formatErrorResponse(HttpServletResponse response, int httpCode, String errorMsg) { response.setStatus(httpCode); response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); try (PrintWriter out = response.getWriter();) { String errorMsgVo = JSON.toJSONString(ImmutableMap.of("code", SSO_GET_TOKEN_ERROR, "message", errorMsg)); out.write(errorMsgVo); out.flush(); } catch (IOException ex) { LOGGER.warn("get token :{}", ex.getMessage()); } }
|