|
@@ -1,27 +1,41 @@
|
|
|
package com.github.zuihou.oauth.controller;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.bean.copier.CopyOptions;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.github.zuihou.authority.dao.auth.StationUserLoginInfoMapper;
|
|
|
import com.github.zuihou.authority.dao.core.StationMapper;
|
|
|
import com.github.zuihou.authority.dto.auth.LoginParamDTO;
|
|
|
import com.github.zuihou.authority.entity.auth.Role;
|
|
|
import com.github.zuihou.authority.entity.auth.StationUserLoginInfo;
|
|
|
+import com.github.zuihou.authority.entity.auth.User;
|
|
|
+import com.github.zuihou.authority.entity.auth.UserToken;
|
|
|
+import com.github.zuihou.authority.event.LoginEvent;
|
|
|
+import com.github.zuihou.authority.event.model.LoginStatusDTO;
|
|
|
import com.github.zuihou.authority.service.auth.RoleService;
|
|
|
+import com.github.zuihou.authority.service.auth.UserService;
|
|
|
import com.github.zuihou.base.R;
|
|
|
import com.github.zuihou.common.constant.BizConstant;
|
|
|
import com.github.zuihou.context.BaseContextHandler;
|
|
|
import com.github.zuihou.database.mybatis.conditions.Wraps;
|
|
|
import com.github.zuihou.exception.BizException;
|
|
|
+import com.github.zuihou.exception.code.ExceptionCode;
|
|
|
import com.github.zuihou.jwt.TokenUtil;
|
|
|
import com.github.zuihou.jwt.model.AuthInfo;
|
|
|
+import com.github.zuihou.jwt.model.JwtUserInfo;
|
|
|
import com.github.zuihou.jwt.utils.JwtUtil;
|
|
|
import com.github.zuihou.oauth.granter.TokenGranter;
|
|
|
import com.github.zuihou.oauth.granter.TokenGranterBuilder;
|
|
|
import com.github.zuihou.oauth.service.AdminUiService;
|
|
|
import com.github.zuihou.oauth.service.ValidateCodeService;
|
|
|
+import com.github.zuihou.oauth.utils.TimeUtils;
|
|
|
import com.github.zuihou.tenant.entity.Productionresource;
|
|
|
+import com.github.zuihou.utils.BeanPlusUtil;
|
|
|
+import com.github.zuihou.utils.DateUtils;
|
|
|
+import com.github.zuihou.utils.SpringUtils;
|
|
|
+import com.github.zuihou.utils.StrHelper;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.AllArgsConstructor;
|
|
@@ -34,7 +48,9 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.github.zuihou.utils.BizAssert.isFalse;
|
|
@@ -65,6 +81,9 @@ public class OauthController {
|
|
|
@Autowired
|
|
|
private StationUserLoginInfoMapper stationUserLoginInfoMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ protected UserService userService;
|
|
|
+
|
|
|
/**
|
|
|
* 租户登录 zuihou-ui 系统
|
|
|
*
|
|
@@ -106,6 +125,65 @@ public class OauthController {
|
|
|
return R.success(u);
|
|
|
}
|
|
|
|
|
|
+ @ApiOperation(value = "获取认证token,仅账户密码", notes = "外部调用")
|
|
|
+ @PostMapping(value = "/getToken")
|
|
|
+ public R<UserInfo> getToken(@Validated @RequestBody LoginParamDTO login) throws BizException {
|
|
|
+
|
|
|
+ if (StrHelper.isAnyBlank(login.getAccount(), login.getPassword())) {
|
|
|
+ return R.fail("请输入用户名或密码");
|
|
|
+ }
|
|
|
+ BaseContextHandler.setTenant("0000");
|
|
|
+ login.setTenant("0000");
|
|
|
+
|
|
|
+ //验证登录
|
|
|
+ User user = this.userService.getByAccount(login.getAccount());
|
|
|
+ // 密码错误
|
|
|
+ String passwordMd5 = cn.hutool.crypto.SecureUtil.md5(login.getPassword());
|
|
|
+
|
|
|
+ if (user == null) {
|
|
|
+ return R.fail(ExceptionCode.JWT_USER_INVALID);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!user.getPassword().equalsIgnoreCase(passwordMd5)) {
|
|
|
+ String msg = "用户名或密码错误!";
|
|
|
+ // 密码错误事件
|
|
|
+ SpringUtils.publishEvent(new LoginEvent(LoginStatusDTO.pwdError(user.getId(), msg)));
|
|
|
+ return R.fail(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!user.getStatus()) {
|
|
|
+ String msg = "用户被禁用,请联系管理员!";
|
|
|
+ SpringUtils.publishEvent(new LoginEvent(LoginStatusDTO.fail(user.getId(), msg)));
|
|
|
+ return R.fail(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ JwtUserInfo userInfo = new JwtUserInfo(user.getId(), user.getAccount(), user.getName());
|
|
|
+ AuthInfo authInfo = tokenUtil.createAuthInfo(userInfo, null);
|
|
|
+ authInfo.setAvatar(user.getAvatar());
|
|
|
+ authInfo.setWorkDescribe(user.getWorkDescribe());
|
|
|
+
|
|
|
+ UserToken userToken = new UserToken();
|
|
|
+ Map<String, String> fieldMapping = new HashMap<>();
|
|
|
+ fieldMapping.put("userId", "createUser");
|
|
|
+ BeanPlusUtil.copyProperties(authInfo, userToken, CopyOptions.create().setFieldMapping(fieldMapping));
|
|
|
+ userToken.setClientId("zuihou_ui");
|
|
|
+ userToken.setExpireTime(DateUtils.date2LocalDateTime(authInfo.getExpiration()));
|
|
|
+
|
|
|
+ //成功登录事件
|
|
|
+ SpringUtils.publishEvent(new LoginEvent(LoginStatusDTO.success(user.getId(), userToken)));
|
|
|
+
|
|
|
+ UserInfo u = new UserInfo();
|
|
|
+ BeanUtil.copyProperties(authInfo, u);
|
|
|
+ //获取角色
|
|
|
+ List<Role> roleList= roleService.findRoleByUserId(u.getUserId());
|
|
|
+ if(CollectionUtil.isNotEmpty(roleList)){
|
|
|
+ String roles = roleList.stream().map(e -> e.getName()).collect(Collectors.joining(","));
|
|
|
+ u.setRoles(roles);
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.success(u);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 验证验证码
|
|
|
*
|