From 9b4433fddf5b401edb0aace8a404ac733b122702 Mon Sep 17 00:00:00 2001 From: 田源 <tianyuan@vci-tech.com> Date: 星期四, 03 四月 2025 14:35:02 +0800 Subject: [PATCH] 添加非密字段显示 --- Source/BladeX-Tool/blade-starter-auth/src/main/java/org/springblade/core/secure/utils/AuthUtil.java | 449 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 449 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-starter-auth/src/main/java/org/springblade/core/secure/utils/AuthUtil.java b/Source/BladeX-Tool/blade-starter-auth/src/main/java/org/springblade/core/secure/utils/AuthUtil.java new file mode 100644 index 0000000..58d9a9b --- /dev/null +++ b/Source/BladeX-Tool/blade-starter-auth/src/main/java/org/springblade/core/secure/utils/AuthUtil.java @@ -0,0 +1,449 @@ +/* + * 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.core.secure.utils; + +import io.jsonwebtoken.Claims; +import org.springblade.core.jwt.JwtUtil; +import org.springblade.core.jwt.props.JwtProperties; +import org.springblade.core.launch.constant.TokenConstant; +import org.springblade.core.secure.BladeUser; +import org.springblade.core.tool.constant.RoleConstant; +import org.springblade.core.tool.support.Kv; +import org.springblade.core.tool.utils.*; + +import javax.servlet.http.HttpServletRequest; +import java.util.Map; +import java.util.Objects; + +/** + * Auth宸ュ叿绫� + * + * @author Chill + */ +public class AuthUtil { + private static final String BLADE_USER_REQUEST_ATTR = "_BLADE_USER_REQUEST_ATTR_"; + + private final static String HEADER = TokenConstant.HEADER; + private final static String ACCOUNT = TokenConstant.ACCOUNT; + private final static String USER_NAME = TokenConstant.USER_NAME; + private final static String NICK_NAME = TokenConstant.NICK_NAME; + private final static String USER_ID = TokenConstant.USER_ID; + private final static String DEPT_ID = TokenConstant.DEPT_ID; + private final static String POST_ID = TokenConstant.POST_ID; + private final static String ROLE_ID = TokenConstant.ROLE_ID; + private final static String ROLE_NAME = TokenConstant.ROLE_NAME; + private final static String TENANT_ID = TokenConstant.TENANT_ID; + private final static String OAUTH_ID = TokenConstant.OAUTH_ID; + private final static String CLIENT_ID = TokenConstant.CLIENT_ID; + private final static String DETAIL = TokenConstant.DETAIL; + + private static JwtProperties jwtProperties; + + /** + * 鑾峰彇閰嶇疆绫� + * + * @return jwtProperties + */ + private static JwtProperties getJwtProperties() { + if (jwtProperties == null) { + jwtProperties = SpringUtil.getBean(JwtProperties.class); + } + return jwtProperties; + } + + /** + * 鑾峰彇鐢ㄦ埛淇℃伅 + * + * @return BladeUser + */ + public static BladeUser getUser() { + HttpServletRequest request = WebUtil.getRequest(); + if (request == null) { + return null; + } + // 浼樺厛浠� request 涓幏鍙� + Object bladeUser = request.getAttribute(BLADE_USER_REQUEST_ATTR); + if (bladeUser == null) { + bladeUser = getUser(request); + if (bladeUser != null) { + // 璁剧疆鍒� request 涓� + request.setAttribute(BLADE_USER_REQUEST_ATTR, bladeUser); + } + } + return (BladeUser) bladeUser; + } + + /** + * 鑾峰彇鐢ㄦ埛淇℃伅 + * + * @param request request + * @return BladeUser + */ + @SuppressWarnings("unchecked") + public static BladeUser getUser(HttpServletRequest request) { + Claims claims = getClaims(request); + if (claims == null) { + return null; + } + String clientId = Func.toStr(claims.get(AuthUtil.CLIENT_ID)); + Long userId = Func.toLong(claims.get(AuthUtil.USER_ID)); + String tenantId = Func.toStr(claims.get(AuthUtil.TENANT_ID)); + String oauthId = Func.toStr(claims.get(AuthUtil.OAUTH_ID)); + String deptId = Func.toStrWithEmpty(claims.get(AuthUtil.DEPT_ID), StringPool.MINUS_ONE); + String postId = Func.toStrWithEmpty(claims.get(AuthUtil.POST_ID), StringPool.MINUS_ONE); + String roleId = Func.toStrWithEmpty(claims.get(AuthUtil.ROLE_ID), StringPool.MINUS_ONE); + String account = Func.toStr(claims.get(AuthUtil.ACCOUNT)); + String roleName = Func.toStr(claims.get(AuthUtil.ROLE_NAME)); + String userName = Func.toStr(claims.get(AuthUtil.USER_NAME)); + String nickName = Func.toStr(claims.get(AuthUtil.NICK_NAME)); + String tenantName = Func.toStr(claims.get("tenantName")); + String email = Func.toStr(claims.get("email")); + String deptName = Func.toStr(claims.get("deptName")); + String secretGrade = Func.toStr(claims.get("secretGrade")); + Kv detail = Kv.create().setAll((Map<? extends String, ?>) claims.get(AuthUtil.DETAIL)); + BladeUser bladeUser = new BladeUser(); + bladeUser.setClientId(clientId); + bladeUser.setUserId(userId); + bladeUser.setTenantId(tenantId); + bladeUser.setOauthId(oauthId); + bladeUser.setAccount(account); + bladeUser.setDeptId(deptId); + bladeUser.setPostId(postId); + bladeUser.setRoleId(roleId); + bladeUser.setRoleName(roleName); + bladeUser.setUserName(userName); + bladeUser.setNickName(nickName); + detail.put("tenantName",tenantName); + detail.put("deptName",deptName); + detail.put("email",email); + detail.put("secretGrade",secretGrade); + bladeUser.setDetail(detail); + return bladeUser; + } + + /** + * 鏄惁涓鸿秴绠� + * + * @return boolean + */ + public static boolean isAdministrator() { + return StringUtil.containsAny(getUserRole(), RoleConstant.ADMINISTRATOR); + } + + /** + * 鏄惁涓虹鐞嗗憳 + * + * @return boolean + */ + public static boolean isAdmin() { + return StringUtil.containsAny(getUserRole(), RoleConstant.ADMIN); + } + + /** + * 鑾峰彇鐢ㄦ埛id + * + * @return userId + */ + public static Long getUserId() { + BladeUser user = getUser(); + return (null == user) ? -1 : user.getUserId(); + } + + /** + * 鑾峰彇鐢ㄦ埛id + * + * @param request request + * @return userId + */ + public static Long getUserId(HttpServletRequest request) { + BladeUser user = getUser(request); + return (null == user) ? -1 : user.getUserId(); + } + + /** + * 鑾峰彇鐢ㄦ埛璐﹀彿 + * + * @return userAccount + */ + public static String getUserAccount() { + BladeUser user = getUser(); + return (null == user) ? StringPool.EMPTY : user.getAccount(); + } + + /** + * 鑾峰彇鐢ㄦ埛璐﹀彿 + * + * @param request request + * @return userAccount + */ + public static String getUserAccount(HttpServletRequest request) { + BladeUser user = getUser(request); + return (null == user) ? StringPool.EMPTY : user.getAccount(); + } + + /** + * 鑾峰彇鐢ㄦ埛鍚� + * + * @return userName + */ + public static String getUserName() { + BladeUser user = getUser(); + return (null == user) ? StringPool.EMPTY : user.getUserName(); + } + + /** + * 鑾峰彇鐢ㄦ埛鍚� + * + * @param request request + * @return userName + */ + public static String getUserName(HttpServletRequest request) { + BladeUser user = getUser(request); + return (null == user) ? StringPool.EMPTY : user.getUserName(); + } + + /** + * 鑾峰彇鏄电О + * + * @return userName + */ + public static String getNickName() { + BladeUser user = getUser(); + return (null == user) ? StringPool.EMPTY : user.getNickName(); + } + + /** + * 鑾峰彇鏄电О + * + * @param request request + * @return userName + */ + public static String getNickName(HttpServletRequest request) { + BladeUser user = getUser(request); + return (null == user) ? StringPool.EMPTY : user.getNickName(); + } + + /** + * 鑾峰彇鐢ㄦ埛閮ㄩ棬 + * + * @return userName + */ + public static String getDeptId() { + BladeUser user = getUser(); + return (null == user) ? StringPool.EMPTY : user.getDeptId(); + } + + /** + * 鑾峰彇鐢ㄦ埛閮ㄩ棬 + * + * @param request request + * @return userName + */ + public static String getDeptId(HttpServletRequest request) { + BladeUser user = getUser(request); + return (null == user) ? StringPool.EMPTY : user.getDeptId(); + } + + /** + * 鑾峰彇鐢ㄦ埛宀椾綅 + * + * @return userName + */ + public static String getPostId() { + BladeUser user = getUser(); + return (null == user) ? StringPool.EMPTY : user.getPostId(); + } + + /** + * 鑾峰彇鐢ㄦ埛宀椾綅 + * + * @param request request + * @return userName + */ + public static String getPostId(HttpServletRequest request) { + BladeUser user = getUser(request); + return (null == user) ? StringPool.EMPTY : user.getPostId(); + } + + /** + * 鑾峰彇鐢ㄦ埛瑙掕壊 + * + * @return userName + */ + public static String getUserRole() { + BladeUser user = getUser(); + return (null == user) ? StringPool.EMPTY : user.getRoleName(); + } + + /** + * 鑾峰彇鐢ㄨ鑹� + * + * @param request request + * @return userName + */ + public static String getUserRole(HttpServletRequest request) { + BladeUser user = getUser(request); + return (null == user) ? StringPool.EMPTY : user.getRoleName(); + } + + /** + * 鑾峰彇绉熸埛ID + * + * @return tenantId + */ + public static String getTenantId() { + BladeUser user = getUser(); + return (null == user) ? StringPool.EMPTY : user.getTenantId(); + } + + /** + * 鑾峰彇绉熸埛ID + * + * @param request request + * @return tenantId + */ + public static String getTenantId(HttpServletRequest request) { + BladeUser user = getUser(request); + return (null == user) ? StringPool.EMPTY : user.getTenantId(); + } + + /** + * 鑾峰彇绗笁鏂硅璇両D + * + * @return tenantId + */ + public static String getOauthId() { + BladeUser user = getUser(); + return (null == user) ? StringPool.EMPTY : user.getOauthId(); + } + + /** + * 鑾峰彇绗笁鏂硅璇両D + * + * @param request request + * @return tenantId + */ + public static String getOauthId(HttpServletRequest request) { + BladeUser user = getUser(request); + return (null == user) ? StringPool.EMPTY : user.getOauthId(); + } + + /** + * 鑾峰彇瀹㈡埛绔痠d + * + * @return clientId + */ + public static String getClientId() { + BladeUser user = getUser(); + return (null == user) ? StringPool.EMPTY : user.getClientId(); + } + + /** + * 鑾峰彇瀹㈡埛绔痠d + * + * @param request request + * @return clientId + */ + public static String getClientId(HttpServletRequest request) { + BladeUser user = getUser(request); + return (null == user) ? StringPool.EMPTY : user.getClientId(); + } + + /** + * 鑾峰彇鐢ㄦ埛璇︽儏 + * + * @return clientId + */ + public static Kv getDetail() { + BladeUser user = getUser(); + return (null == user) ? Kv.create() : user.getDetail(); + } + + /** + * 鑾峰彇鐢ㄦ埛璇︽儏 + * + * @param request request + * @return clientId + */ + public static Kv getDetail(HttpServletRequest request) { + BladeUser user = getUser(request); + return (null == user) ? Kv.create() : user.getDetail(); + } + + /** + * 鑾峰彇Claims + * + * @param request request + * @return Claims + */ + public static Claims getClaims(HttpServletRequest request) { + String auth = request.getHeader(AuthUtil.HEADER); + Claims claims = null; + String token; + // 鑾峰彇 Token 鍙傛暟 + if (StringUtil.isNotBlank(auth)) { + token = JwtUtil.getToken(auth); + } else { + String parameter = request.getParameter(AuthUtil.HEADER); + token = JwtUtil.getToken(parameter); + } + // 鑾峰彇 Token 鍊� + if (StringUtil.isNotBlank(token)) { + claims = AuthUtil.parseJWT(token); + } + // 鍒ゆ柇 Token 鐘舵�� + if (ObjectUtil.isNotEmpty(claims) && getJwtProperties().getState()) { + String tenantId = Func.toStr(claims.get(AuthUtil.TENANT_ID)); + String userId = Func.toStr(claims.get(AuthUtil.USER_ID)); + String accessToken = JwtUtil.getAccessToken(tenantId, userId, token); + if (!token.equalsIgnoreCase(accessToken)) { + return null; + } + } + return claims; + } + + /** + * 鑾峰彇璇锋眰澶� + * + * @return header + */ + public static String getHeader() { + return getHeader(Objects.requireNonNull(WebUtil.getRequest())); + } + + /** + * 鑾峰彇璇锋眰澶� + * + * @param request request + * @return header + */ + public static String getHeader(HttpServletRequest request) { + return request.getHeader(HEADER); + } + + /** + * 瑙f瀽jsonWebToken + * + * @param jsonWebToken jsonWebToken + * @return Claims + */ + public static Claims parseJWT(String jsonWebToken) { + return JwtUtil.parseJWT(jsonWebToken); + } + +} -- Gitblit v1.9.3