|
|
@@ -7,10 +7,13 @@ import com.usoftchina.saas.account.dto.AccountCopyDTO;
|
|
|
import com.usoftchina.saas.account.dto.AccountDTO;
|
|
|
import com.usoftchina.saas.account.dto.AccountUpdateDTO;
|
|
|
import com.usoftchina.saas.account.dto.CompanyBaseDTO;
|
|
|
+import com.usoftchina.saas.auth.common.cookie.CookieHelper;
|
|
|
import com.usoftchina.saas.auth.common.cookie.CookieInfo;
|
|
|
+import com.usoftchina.saas.auth.common.cookie.CookieUtils;
|
|
|
import com.usoftchina.saas.auth.common.jwt.JwtHelper;
|
|
|
import com.usoftchina.saas.auth.common.jwt.JwtInfo;
|
|
|
import com.usoftchina.saas.auth.common.jwt.JwtToken;
|
|
|
+import com.usoftchina.saas.auth.config.CookieConfig;
|
|
|
import com.usoftchina.saas.auth.dto.AuthDTO;
|
|
|
import com.usoftchina.saas.auth.dto.AuthorizeLogDTO;
|
|
|
import com.usoftchina.saas.auth.dto.TokenDTO;
|
|
|
@@ -30,6 +33,9 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
@@ -45,6 +51,7 @@ import java.util.List;
|
|
|
*/
|
|
|
@RestController
|
|
|
@RequestMapping
|
|
|
+@EnableConfigurationProperties(CookieConfig.class)
|
|
|
public class AuthController {
|
|
|
|
|
|
@Autowired
|
|
|
@@ -65,6 +72,9 @@ public class AuthController {
|
|
|
@Value("${auth.max-errors:5}")
|
|
|
private int maxErrors;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private CookieConfig cookieConfig;
|
|
|
+
|
|
|
@Autowired
|
|
|
private AuthorizeLogService authorizeLogService;
|
|
|
|
|
|
@@ -151,6 +161,53 @@ public class AuthController {
|
|
|
return Result.success(new AuthDTO(tokenDTO, accountDTO));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 使用账户中心登录cookie信息产生token登录
|
|
|
+ *
|
|
|
+ * @param info
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/sso/authorize")
|
|
|
+ public Result<AuthDTO> ssoAuthorize(HttpServletRequest request, HttpServletResponse response, CookieInfo info) throws IOException{
|
|
|
+ if (null != info && null != info.getMobile()) {
|
|
|
+ AccountDTO accountDTO = null;
|
|
|
+ Result<AccountDTO> result = accountApi.getAccount(info.getMobile());
|
|
|
+ if (!result.isSuccess()) {
|
|
|
+ if (ExceptionCode.USER_NOT_EXIST.getCode() == result.getCode()) {
|
|
|
+ // 新用户,自动注册
|
|
|
+ accountDTO = createAccountByCookieInfo(info);
|
|
|
+ } else {
|
|
|
+ return Result.error(result);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ accountDTO = result.getData();
|
|
|
+ // 检测uu是否正确
|
|
|
+ if (null == accountDTO.getUu() || !info.getUserUU().equals(accountDTO.getUu())) {
|
|
|
+ accountDTO.setUu(info.getUserUU());
|
|
|
+ Result updateResult = accountApi.update(BeanMapper.map(accountDTO, AccountUpdateDTO.class));
|
|
|
+ if (!updateResult.isSuccess()) {
|
|
|
+ return Result.error(updateResult);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // TODO
|
|
|
+ String appId = "trade-app";
|
|
|
+ // 登录日志
|
|
|
+ authorizeLogService.save(AuthorizeLog.from(request)
|
|
|
+ .setAccountId(accountDTO.getId())
|
|
|
+ .setAppId(appId).build());
|
|
|
+ Long companyId = null;
|
|
|
+ if (!CollectionUtils.isEmpty(accountDTO.getCompanies())) {
|
|
|
+ companyId = accountDTO.getCompanies().get(0).getId();
|
|
|
+ }
|
|
|
+ JwtInfo jwtInfo = new JwtInfo(appId, companyId, accountDTO.getId(), accountDTO.getUsername(), accountDTO.getRealname());
|
|
|
+ JwtToken jwtToken = JwtHelper.generateToken(jwtInfo, privateKeyPath, expire);
|
|
|
+ TokenDTO tokenDTO = BeanMapper.map(jwtToken, TokenDTO.class);
|
|
|
+ return Result.success(new AuthDTO(tokenDTO, accountDTO));
|
|
|
+ }
|
|
|
+ return Result.error(ExceptionCode.COOKIE_ILLEGAL_ARGUMENT);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 账户中心登录时jsonp回调
|
|
|
*
|
|
|
@@ -261,8 +318,18 @@ public class AuthController {
|
|
|
* @return
|
|
|
*/
|
|
|
@GetMapping("/info")
|
|
|
- public Result<AuthDTO> getInfo(HttpServletRequest request) {
|
|
|
+ public Result<AuthDTO> getInfo(HttpServletRequest request, HttpServletResponse response) throws IOException{
|
|
|
String token = request.getHeader(authHeader);
|
|
|
+ if (StringUtils.isEmpty(token)) {
|
|
|
+ // 解析cookie获取身份
|
|
|
+ CookieInfo info = CookieHelper.geInfoFromRequest(request,
|
|
|
+ cookieConfig.getName(), cookieConfig.getSecretKey());
|
|
|
+ if (null != info) {
|
|
|
+ return ssoAuthorize(request, response, info);
|
|
|
+ } else {
|
|
|
+ return Result.error(ExceptionCode.JWT_ILLEGAL_ARGUMENT);
|
|
|
+ }
|
|
|
+ }
|
|
|
JwtInfo infoFromToken = JwtHelper.getInfoFromToken(token, publicKeyPath);
|
|
|
Result<AccountDTO> result = accountApi.getAccount(infoFromToken.getUserName());
|
|
|
if (result.isSuccess()) {
|