|
|
@@ -5,27 +5,38 @@ import com.usoftchina.saas.account.dto.AccountDTO;
|
|
|
import com.usoftchina.saas.auth.common.jwt.JwtHelper;
|
|
|
import com.usoftchina.saas.auth.common.jwt.JwtInfo;
|
|
|
import com.usoftchina.saas.auth.common.jwt.TokenVO;
|
|
|
+import com.usoftchina.saas.auth.dto.AuthDTO;
|
|
|
import com.usoftchina.saas.auth.dto.TokenDTO;
|
|
|
import com.usoftchina.saas.base.Result;
|
|
|
+import com.usoftchina.saas.exception.ExceptionCode;
|
|
|
import com.usoftchina.saas.utils.BeanMapper;
|
|
|
+import com.usoftchina.saas.utils.CollectionUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
/**
|
|
|
* @author yingp
|
|
|
* @date 2018/10/2
|
|
|
*/
|
|
|
@RestController
|
|
|
-@RequestMapping(path = "/api/auth")
|
|
|
+@RequestMapping
|
|
|
public class AuthController {
|
|
|
|
|
|
@Autowired
|
|
|
private AccountApi accountApi;
|
|
|
|
|
|
+ @Value("${auth.public-key}")
|
|
|
+ private String publicKeyPath;
|
|
|
+
|
|
|
@Value("${auth.private-key}")
|
|
|
private String privateKeyPath;
|
|
|
|
|
|
+ @Value("${auth.header:Authorization}")
|
|
|
+ private String authHeader;
|
|
|
+
|
|
|
@Value("${auth.expire:18000}")
|
|
|
private int expire;
|
|
|
|
|
|
@@ -36,17 +47,59 @@ public class AuthController {
|
|
|
* @param password
|
|
|
* @return
|
|
|
*/
|
|
|
- @PostMapping
|
|
|
- public Result<TokenDTO> authorize(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {
|
|
|
+ @PostMapping("/authorize")
|
|
|
+ public Result<AuthDTO> authorize(@RequestParam String username, @RequestParam String password) {
|
|
|
Result<AccountDTO> result = accountApi.validByUsernameAndPwd(username, password);
|
|
|
if (result.isSuccess()) {
|
|
|
AccountDTO accountDTO = result.getData();
|
|
|
+ Long companyId = null;
|
|
|
+ if (!CollectionUtils.isEmpty(accountDTO.getCompanies())) {
|
|
|
+ companyId = accountDTO.getCompanies().get(0).getId();
|
|
|
+ }
|
|
|
// TODO
|
|
|
- JwtInfo info = new JwtInfo(null, null, accountDTO.getId(), accountDTO.getUsername());
|
|
|
+ String appId = "trade-app";
|
|
|
+
|
|
|
+ JwtInfo info = new JwtInfo(appId, companyId, accountDTO.getId(), accountDTO.getUsername());
|
|
|
TokenVO tokenVO = JwtHelper.generateToken(info, privateKeyPath, expire);
|
|
|
TokenDTO tokenDTO = BeanMapper.map(tokenVO, TokenDTO.class);
|
|
|
- return Result.success(tokenDTO);
|
|
|
+ return Result.success(new AuthDTO(tokenDTO, accountDTO.getCompanies()));
|
|
|
}
|
|
|
return Result.error(result.getCode(), result.getMessage());
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 切换公司
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @param companyId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/switch/company")
|
|
|
+ public Result<TokenDTO> switchCompany(HttpServletRequest request, @RequestParam Long companyId) {
|
|
|
+ String token = request.getHeader(authHeader);
|
|
|
+ JwtInfo infoFromToken = JwtHelper.getInfoFromToken(token, publicKeyPath);
|
|
|
+ if (isCompanyAvailable(infoFromToken, companyId)) {
|
|
|
+ JwtInfo info = new JwtInfo(infoFromToken.getAppId(), companyId, infoFromToken.getUserId(),
|
|
|
+ infoFromToken.getUserName());
|
|
|
+ TokenVO tokenVO = JwtHelper.generateToken(info, privateKeyPath, expire);
|
|
|
+ return Result.success(BeanMapper.map(tokenVO, TokenDTO.class));
|
|
|
+ }
|
|
|
+ return Result.error(ExceptionCode.COMPANY_NOT_BIND);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 指定公司是否可用:
|
|
|
+ * 公司是否存在 + 是否已绑定
|
|
|
+ *
|
|
|
+ * @param infoFromToken
|
|
|
+ * @param companyId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private boolean isCompanyAvailable(JwtInfo infoFromToken, Long companyId) {
|
|
|
+ Result<AccountDTO> accountDTO = accountApi.getAccount(infoFromToken.getUserName());
|
|
|
+ if (!CollectionUtils.isEmpty(accountDTO.getData().getCompanies())) {
|
|
|
+ return accountDTO.getData().getCompanies().stream().anyMatch(cmp -> cmp.getId().equals(companyId));
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|