wyz 1 månad sedan
förälder
incheckning
773a9a6ed3

+ 50 - 0
src/main/java/org/springblade/modules/auth/enums/UserEnum.java

@@ -0,0 +1,50 @@
+/*
+ *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ *
+ *  Redistributions of source code must retain the above copyright notice,
+ *  this list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright
+ *  notice, this list of conditions and the following disclaimer in the
+ *  documentation and/or other materials provided with the distribution.
+ *  Neither the name of the dreamlu.net developer nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ *  Author: Chill 庄骞 (smallchill@163.com)
+ */
+package org.springblade.modules.auth.enums;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 用户类型枚举
+ *
+ * @author Chill
+ */
+@Getter
+@AllArgsConstructor
+public enum UserEnum {
+
+	/**
+	 * web
+	 */
+	WEB("web", 1),
+
+	/**
+	 * app
+	 */
+	APP("app", 2),
+
+	/**
+	 * other
+	 */
+	OTHER("other", 3),
+	;
+
+	final String name;
+	final int category;
+
+}

+ 193 - 0
src/main/java/org/springblade/modules/auth/utils/TokenUtil.java

@@ -25,14 +25,23 @@
  */
 package org.springblade.modules.auth.utils;
 
+import jakarta.servlet.http.HttpServletResponse;
 import org.springblade.common.cache.SysCache;
+import org.springblade.core.launch.constant.TokenConstant;
 import org.springblade.core.oauth2.provider.OAuth2Request;
+import org.springblade.core.oauth2.service.OAuth2Client;
 import org.springblade.core.oauth2.service.OAuth2User;
 import org.springblade.core.oauth2.service.impl.OAuth2UserDetail;
+import org.springblade.core.oauth2.utils.OAuth2Util;
+import org.springblade.core.secure.TokenInfo;
+import org.springblade.core.tool.support.Kv;
 import org.springblade.core.tool.utils.Func;
 import org.springblade.modules.system.pojo.entity.User;
 import org.springblade.modules.system.pojo.entity.UserInfo;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * 认证工具类
  *
@@ -83,4 +92,188 @@ public class TokenUtil {
 		return userDetail;
 	}
 
+	public static Kv createAuthInfo(UserInfo userInfo) {
+		Kv authInfo = Kv.create();
+		User user = userInfo.getUser();
+		OAuth2Request request = OAuth2Request.create().buildArgs();
+		OAuth2User oUser = TokenUtil.convertUser(userInfo, request);
+		String clientId = request.getClientId();
+		String clientSecret = request.getClientSecret();
+		OAuth2Client oAuth2Client = new OAuth2Client() {
+			@Override
+			public String getClientId() {
+				return clientId;
+			}
+
+			@Override
+			public String getClientSecret() {
+				return clientSecret;
+			}
+
+			@Override
+			public String getResourceIds() {
+				return null;
+			}
+
+			@Override
+			public String getScope() {
+				return null;
+			}
+
+			@Override
+			public String getAuthorizedGrantTypes() {
+				return null;
+			}
+
+			@Override
+			public String getWebServerRedirectUri() {
+				return null;
+			}
+
+			@Override
+			public String getAuthorities() {
+				return null;
+			}
+
+			@Override
+			public Integer getAccessTokenValidity() {
+				return null;
+			}
+
+			@Override
+			public Integer getRefreshTokenValidity() {
+				return null;
+			}
+
+			@Override
+			public String getAdditionalInformation() {
+				return null;
+			}
+
+			@Override
+			public String getAutoapprove() {
+				return null;
+			}
+		};
+		oUser.setClient(oAuth2Client);
+		//设置jwt参数
+		Map<String, Object> param = new HashMap<>(16);
+		param.put(TokenConstant.TOKEN_TYPE, TokenConstant.ACCESS_TOKEN);
+		param.put(TokenConstant.TENANT_ID, user.getTenantId());
+		param.put(TokenConstant.USER_ID, Func.toStr(user.getId()));
+		param.put(TokenConstant.DEPT_ID, user.getDeptId());
+		param.put(TokenConstant.POST_ID, user.getPostId());
+		param.put(TokenConstant.ROLE_ID, user.getRoleId());
+		param.put(TokenConstant.OAUTH_ID, userInfo.getOauthId());
+		param.put(TokenConstant.ACCOUNT, user.getAccount());
+		param.put(TokenConstant.USER_NAME, user.getAccount());
+		param.put(TokenConstant.NICK_NAME, user.getName());
+		param.put(TokenConstant.REAL_NAME, user.getRealName());
+		param.put(TokenConstant.ROLE_NAME, Func.join(userInfo.getRoles()));
+		param.put(TokenConstant.DETAIL, userInfo.getDetail());
+
+		//拼装accessToken
+		try {
+			TokenInfo accessToken = OAuth2Util.createAccessToken(oUser);
+			//TokenInfo accessToken = SecureUtil.createJWT(param, "audience", "issuser", TokenConstant.ACCESS_TOKEN);
+			//返回accessToken
+			return authInfo.set(TokenConstant.TENANT_ID, user.getTenantId())
+					.set(TokenConstant.USER_ID, Func.toStr(user.getId()))
+					.set(TokenConstant.DEPT_ID, user.getDeptId())
+					.set(TokenConstant.POST_ID, user.getPostId())
+					.set(TokenConstant.ROLE_ID, user.getRoleId())
+					.set(TokenConstant.OAUTH_ID, userInfo.getOauthId())
+					.set(TokenConstant.ACCOUNT, user.getAccount())
+					.set(TokenConstant.USER_NAME, user.getAccount())
+					.set(TokenConstant.NICK_NAME, user.getName())
+					.set(TokenConstant.REAL_NAME, user.getRealName())
+					.set(TokenConstant.ROLE_NAME, Func.join(userInfo.getRoles()))
+					.set(TokenConstant.AVATAR, Func.toStr(user.getAvatar(), TokenConstant.DEFAULT_AVATAR))
+					.set(TokenConstant.ACCESS_TOKEN, accessToken.getToken())
+					.set(TokenConstant.REFRESH_TOKEN, createRefreshToken(userInfo).getToken())
+					.set(TokenConstant.TOKEN_TYPE, TokenConstant.BEARER)
+					.set(TokenConstant.EXPIRES_IN, accessToken.getExpire())
+					.set(TokenConstant.DETAIL, userInfo.getDetail())
+					.set(TokenConstant.LICENSE, TokenConstant.LICENSE_NAME);
+		} catch (Exception ex) {
+			return authInfo.set("error_code", HttpServletResponse.SC_UNAUTHORIZED).set("error_description", ex.getMessage());
+		}
+	}
+
+	/**
+	 * 创建refreshToken
+	 *
+	 * @param userInfo 用户信息
+	 * @return refreshToken
+	 */
+	private static TokenInfo createRefreshToken(UserInfo userInfo) {
+		User user = userInfo.getUser();
+		OAuth2Request request = OAuth2Request.create().buildArgs();
+		OAuth2User oUser = TokenUtil.convertUser(userInfo, request);
+		String clientId = request.getClientId();
+		String clientSecret = request.getClientSecret();
+		OAuth2Client oAuth2Client = new OAuth2Client() {
+			@Override
+			public String getClientId() {
+				return clientId;
+			}
+
+			@Override
+			public String getClientSecret() {
+				return clientSecret;
+			}
+
+			@Override
+			public String getResourceIds() {
+				return null;
+			}
+
+			@Override
+			public String getScope() {
+				return null;
+			}
+
+			@Override
+			public String getAuthorizedGrantTypes() {
+				return null;
+			}
+
+			@Override
+			public String getWebServerRedirectUri() {
+				return null;
+			}
+
+			@Override
+			public String getAuthorities() {
+				return null;
+			}
+
+			@Override
+			public Integer getAccessTokenValidity() {
+				return null;
+			}
+
+			@Override
+			public Integer getRefreshTokenValidity() {
+				return null;
+			}
+
+			@Override
+			public String getAdditionalInformation() {
+				return null;
+			}
+
+			@Override
+			public String getAutoapprove() {
+				return null;
+			}
+		};
+		oUser.setClient(oAuth2Client);
+		Map<String, Object> param = new HashMap<>(16);
+		param.put(TokenConstant.TOKEN_TYPE, TokenConstant.REFRESH_TOKEN);
+		param.put(TokenConstant.USER_ID, Func.toStr(user.getId()));
+		param.put(TokenConstant.DEPT_ID, Func.toStr(user.getDeptId()));
+		param.put(TokenConstant.ROLE_ID, Func.toStr(user.getRoleId()));
+		return OAuth2Util.createAccessToken(oUser);
+	}
 }

+ 9 - 0
src/main/java/org/springblade/modules/system/controller/UserController.java

@@ -28,6 +28,7 @@ package org.springblade.modules.system.controller;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
@@ -50,6 +51,7 @@ import org.springblade.core.tenant.annotation.NonDS;
 import org.springblade.core.tool.api.R;
 import org.springblade.core.tool.constant.BladeConstant;
 import org.springblade.core.tool.constant.RoleConstant;
+import org.springblade.core.tool.support.Kv;
 import org.springblade.core.tool.utils.DateUtil;
 import org.springblade.core.tool.utils.StringPool;
 import org.springblade.modules.system.excel.UserExcel;
@@ -339,4 +341,11 @@ public class UserController {
 		return R.status(userService.auditRefuse(userIds));
 	}
 
+	@PostMapping("/wxlogi_qs")
+	@ResponseBody
+	public R user_loginQs(String account) {
+		User user = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getAccount, account));
+		Kv token = userService.getToken(user);
+		return R.data(token);
+	}
 }

+ 6 - 0
src/main/java/org/springblade/modules/system/service/IUserService.java

@@ -30,6 +30,8 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import org.springblade.core.mp.base.BaseService;
 import org.springblade.core.mp.support.Query;
+import org.springblade.core.tool.support.Kv;
+import org.springblade.modules.auth.enums.UserEnum;
 import org.springblade.modules.auth.provider.UserType;
 import org.springblade.modules.system.pojo.entity.User;
 import org.springblade.modules.system.pojo.entity.UserInfo;
@@ -273,4 +275,8 @@ public interface IUserService extends BaseService<User> {
 	 * @return
 	 */
 	boolean auditRefuse(String userIds);
+
+    Kv getToken(User user);
+
+	UserInfo userInfo(String tenantId, String account, String password, UserEnum userEnum);
 }

+ 36 - 0
src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java

@@ -48,7 +48,9 @@ import org.springblade.core.tool.constant.BladeConstant;
 import org.springblade.core.tool.jackson.JsonUtil;
 import org.springblade.core.tool.support.Kv;
 import org.springblade.core.tool.utils.*;
+import org.springblade.modules.auth.enums.UserEnum;
 import org.springblade.modules.auth.provider.UserType;
+import org.springblade.modules.auth.utils.TokenUtil;
 import org.springblade.modules.system.excel.UserExcel;
 import org.springblade.modules.system.mapper.UserMapper;
 import org.springblade.modules.system.pojo.entity.*;
@@ -542,4 +544,38 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
 		return changeStatus(Func.toLongList(userIds), StatusType.DISABLED.getType());
 	}
 
+	@Override
+	public Kv getToken(User user) {
+		// 获取用户信息
+		String tenantId = user.getTenantId();
+		String username = user.getAccount();
+		String password = user.getPassword();
+
+		UserInfo userInfo = null;
+		if (Func.isNoneBlank(user.getAccount(), user.getPassword())) {
+			// 获取租户信息
+//			Tenant tenant = tenantService.getByTenantId(tenantId);
+//			if (TokenUtil.judgeTenant(tenant)) {
+//				throw new ServiceException(TokenUtil.USER_HAS_NO_TENANT_PERMISSION);
+//			}
+			// 获取用户类型
+			String userType = "WEB";
+			// 根据不同用户类型调用对应的接口返回数据,用户可自行拓展
+			if (userType.equals(UserEnum.WEB.getName())) {
+				userInfo = userInfo(tenantId, username, password, UserEnum.WEB);
+			} else if (userType.equals(UserEnum.APP.getName())) {
+				userInfo = userInfo(tenantId, username, password, UserEnum.APP);
+			} else {
+				userInfo = userInfo(tenantId, username, password, UserEnum.OTHER);
+			}
+		}
+		Kv authInfo = TokenUtil.createAuthInfo(userInfo);
+		return authInfo;
+	}
+
+	@Override
+	public UserInfo userInfo(String tenantId, String account, String password, UserEnum userEnum) {
+		User user = baseMapper.getUser(tenantId, account);
+		return buildUserInfo(user);
+	}
 }