|
@@ -1,6 +1,9 @@
|
|
|
package com.persagy.account.controller;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
@@ -21,14 +24,8 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
-import com.persagy.common.constant.SaasCommonConstant;
|
|
|
-import com.persagy.common.enums.ResponseCode;
|
|
|
-import com.persagy.common.exception.BusinessException;
|
|
|
-import com.persagy.common.utils.ResponseResult;
|
|
|
-import com.persagy.common.utils.ResponseResultUtil;
|
|
|
-import com.persagy.common.utils.StringUtil;
|
|
|
-import com.persagy.log.core.util.RequestUtil;
|
|
|
import com.persagy.account.config.ApplicationProperties;
|
|
|
+import com.persagy.account.manage.SaasAuthHandler;
|
|
|
import com.persagy.account.manage.SaasRoleMenuHandler;
|
|
|
import com.persagy.account.pojo.dto.SaasAccount;
|
|
|
import com.persagy.account.pojo.dto.SaasGroup;
|
|
@@ -38,6 +35,13 @@ import com.persagy.account.pojo.vo.account.SaasAccountQueryVO;
|
|
|
import com.persagy.account.pojo.vo.menu.SaasMenuQueryVO;
|
|
|
import com.persagy.account.service.ISaasAccountService;
|
|
|
import com.persagy.account.service.ISaasGroupService;
|
|
|
+import com.persagy.common.constant.SaasCommonConstant;
|
|
|
+import com.persagy.common.enums.ResponseCode;
|
|
|
+import com.persagy.common.exception.BusinessException;
|
|
|
+import com.persagy.common.utils.ResponseResult;
|
|
|
+import com.persagy.common.utils.ResponseResultUtil;
|
|
|
+import com.persagy.common.utils.StringUtil;
|
|
|
+import com.persagy.log.core.util.RequestUtil;
|
|
|
import com.persagy.security.constant.CipherConstans;
|
|
|
import com.persagy.security.util.BouncycastleCipher;
|
|
|
import com.persagy.security.util.SecureAES;
|
|
@@ -62,6 +66,12 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
@RestController
|
|
|
public class SaasLoginController {
|
|
|
|
|
|
+ private static final String HTTP_PREFIX = "http://";
|
|
|
+ private static final String HTTPS_PREFIX = "https://";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SaasAuthHandler saasAuthHandler;
|
|
|
+
|
|
|
@Autowired
|
|
|
private ApplicationProperties properties;
|
|
|
|
|
@@ -83,12 +93,14 @@ public class SaasLoginController {
|
|
|
/**
|
|
|
* 运维系统账号登录
|
|
|
*
|
|
|
- * @throws UnsupportedEncodingException
|
|
|
* @throws NumberFormatException
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
@ApiOperation(value = "运维系统账号登录")
|
|
|
@RequestMapping(value = "saas/login", method = RequestMethod.POST)
|
|
|
- public ResponseResult saasAccountLogin(@RequestBody @Validated SaasAccountLoginVO loginVO, HttpServletRequest request, HttpServletResponse response) throws NumberFormatException, UnsupportedEncodingException {
|
|
|
+ public ResponseResult saasAccountLogin(@RequestBody @Validated SaasAccountLoginVO loginVO, HttpServletRequest request, HttpServletResponse response) throws NumberFormatException, IOException {
|
|
|
+ String redirectUrl = this.validSaasSSO(loginVO);
|
|
|
+
|
|
|
LambdaQueryWrapper<SaasAccount> queryWrapper = new SaasAccount.Builder().createQueryWrapper().usernameEq(loginVO.getUsername()).passwordEq(loginVO.getPassword()).builderQueryWrapper();
|
|
|
List<SaasAccount> accounts = this.saasAccountService.list(queryWrapper);
|
|
|
if (CollectionUtil.isEmpty(accounts) || accounts.size() > 1) {
|
|
@@ -138,6 +150,14 @@ public class SaasLoginController {
|
|
|
}
|
|
|
account.setAppId(loginVO.getAppId());
|
|
|
String token = this.setRedisToken(account, loginVO.getAppId(), loginVO.isRemember(), false);
|
|
|
+ // 判断是否需要重定向
|
|
|
+ if (StringUtil.isNotBlank(redirectUrl)) {
|
|
|
+ String code = this.saasAuthHandler.getCode(loginVO.getClientId(), loginVO.getRedirectUrl());
|
|
|
+ redirectUrl = redirectUrl + "&code=" + code;
|
|
|
+ Map<String, String> temp = new HashMap<String, String>();
|
|
|
+ temp.put("redirectUrl", redirectUrl);
|
|
|
+ return ResponseResultUtil.successResult(temp);
|
|
|
+ }
|
|
|
response.setHeader(CipherConstans.TOKEN_HEADER_TOKEN, token);
|
|
|
return ResponseResultUtil.successResult(account);
|
|
|
}
|
|
@@ -223,6 +243,39 @@ public class SaasLoginController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 验证是否需要重定向
|
|
|
+ * @param loginVO
|
|
|
+ * @return 为空时,说明不需要
|
|
|
+ * @throws UnsupportedEncodingException
|
|
|
+ */
|
|
|
+ private String validSaasSSO(SaasAccountLoginVO loginVO) throws UnsupportedEncodingException {
|
|
|
+ String state = loginVO.getState();
|
|
|
+ String clientId = loginVO.getClientId();
|
|
|
+ String redirectUrl = loginVO.getRedirectUrl();
|
|
|
+ if (StringUtil.isAllBlank(state, clientId, redirectUrl)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (StringUtil.isBlank(clientId)) {
|
|
|
+ throw new BusinessException(ResponseCode.A0402.getCode(), "客户端ID,不可为空");
|
|
|
+ }
|
|
|
+ if (StringUtil.isBlank(redirectUrl)) {
|
|
|
+ throw new BusinessException(ResponseCode.A0402.getCode(), "重定向URL,不可为空");
|
|
|
+ }
|
|
|
+ // 验证clientId,是否存在
|
|
|
+ // 验证redirectUrl的有效性
|
|
|
+ if (redirectUrl.indexOf(HTTP_PREFIX) >= 0 || redirectUrl.indexOf(HTTPS_PREFIX) >= 0) {
|
|
|
+ redirectUrl = URLEncoder.encode(redirectUrl, StandardCharsets.UTF_8.name());
|
|
|
+ if (redirectUrl.indexOf("?") > 0) {
|
|
|
+ redirectUrl = redirectUrl + "&clientId=" + clientId + "&state=" + state;
|
|
|
+ } else {
|
|
|
+ redirectUrl = redirectUrl + "?clientId=" + clientId + "&state=" + state;
|
|
|
+ }
|
|
|
+ return redirectUrl;
|
|
|
+ }
|
|
|
+ throw new BusinessException(ResponseCode.A0402.getCode(), "重定向URL,格式错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 获取此账号的菜单权限树
|
|
|
*
|
|
|
* @param account
|