From 517b804dbd6bf49c0fe631476df551ef7722f0a9 Mon Sep 17 00:00:00 2001 From: sxu <602087911@qq.com> Date: Fri, 14 Mar 2025 17:09:40 +0800 Subject: [PATCH] get token --- .../auth/oauth/controller/AuthController.java | 50 ++++++----- .../core/auth/oauth/util/OAuthUtil.java | 41 +++++++++ .../core/auth/user/dto/UserLoginDTO.java | 88 +++++++++++++++++++ 3 files changed, 156 insertions(+), 23 deletions(-) create mode 100644 bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/auth/oauth/util/OAuthUtil.java create mode 100644 bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/auth/user/dto/UserLoginDTO.java diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/auth/oauth/controller/AuthController.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/auth/oauth/controller/AuthController.java index c2d0cadc..bfa05e43 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/auth/oauth/controller/AuthController.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/auth/oauth/controller/AuthController.java @@ -8,8 +8,13 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.bonus.canteen.core.auth.oauth.util.OAuthUtil; +import com.bonus.canteen.core.auth.user.dto.UserLoginDTO; +import com.bonus.canteen.core.config.AccessToken; import com.bonus.canteen.core.config.SecureProperties; import com.bonus.canteen.core.config.SmUtils; +import com.bonus.canteen.core.config.WebContext; +import com.bonus.canteen.core.customer.model.PigxUser; import com.bonus.canteen.core.device.mq.MacMessageService; import com.bonus.common.core.web.domain.AjaxResult; import com.google.common.base.Joiner; @@ -90,29 +95,28 @@ public class AuthController { // // return LeResponse.succ(object); // } -// -// @PostMapping({"/token"}) -// @RequiresGuest -// @ApiOperation("设备登陆接口登陆") -// public LeResponse login(@RequestParam String content) { -// MgrUserLoginDTO loginDTO = (MgrUserLoginDTO)JSON.parseObject(content, MgrUserLoginDTO.class); -// -// JSONObject object; -// try { -// PigxUser user = this.mgrUserService.login(loginDTO); -// object = JSON.parseObject(JSON.toJSONString(user)); -// object.put("user_id", user.getId()); -// object.put("merchant_id", user.getMerchantId()); -// if (WebContext.get().getAccessToken().isPresent()) { -// object.put("securityTokenSign", OAuthUtil.responseSetSecurityTokenSign(user.getMerchantId(), ((AccessToken)WebContext.get().getAccessToken().get()).getId())); -// } -// } catch (LeCheckedException var5) { -// return LeResponse.fail(var5.getMessage()); -// } -// -// return LeResponse.succ(object); -// } -// + + @PostMapping({"/token"}) + @ApiOperation("设备登陆接口登陆") + public AjaxResult login(@RequestParam String content) { + UserLoginDTO loginDTO = (UserLoginDTO)JSON.parseObject(content, UserLoginDTO.class); + + JSONObject object; + try { + PigxUser user = new PigxUser(); //this.mgrUserService.login(loginDTO); + object = JSON.parseObject(JSON.toJSONString(user)); + object.put("user_id", user.getId()); + object.put("merchant_id", user.getMerchantId()); + if (WebContext.get().getAccessToken().isPresent()) { + object.put("securityTokenSign", OAuthUtil.responseSetSecurityTokenSign(user.getMerchantId(), ((AccessToken)WebContext.get().getAccessToken().get()).getId())); + } + } catch (Exception var5) { + return AjaxResult.error(var5.getMessage()); + } + + return AjaxResult.success(object); + } + // @DeleteMapping({"/logOut"}) // @RequiresAuthentication // @ApiOperation("退出登陆") diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/auth/oauth/util/OAuthUtil.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/auth/oauth/util/OAuthUtil.java new file mode 100644 index 00000000..1b206cf2 --- /dev/null +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/auth/oauth/util/OAuthUtil.java @@ -0,0 +1,41 @@ +package com.bonus.canteen.core.auth.oauth.util; + +import cn.hutool.core.util.ObjectUtil; +import com.bonus.canteen.core.common.utils.SpringContextHolder; +import com.bonus.canteen.core.config.SecureProperties; +import com.bonus.canteen.core.config.SmUtils; +import com.bonus.canteen.core.config.WebContext; +import org.springframework.context.annotation.Lazy; + +import javax.servlet.http.HttpServletRequest; +import java.util.TreeMap; + +public class OAuthUtil { + private static final String HEADER_SECURITY_TENANT_ID = "X-Security-Tenant-Id"; + private static final String HEADER_SECURITY_TOKEN = "X-Security-Token"; + @Lazy + private static final SecureProperties secureProperties = (SecureProperties) SpringContextHolder.getBean(SecureProperties.class); + + private OAuthUtil() { + } + + public static String responseSetSecurityTokenSign(Long merchantId, String token) { + TreeMap needSignMap = new TreeMap(); + String securityTokenSign = null; + if (WebContext.get().getRequest().isPresent()) { + String encryptKey = ((HttpServletRequest)WebContext.get().getRequest().get()).getHeader(secureProperties.getSecurity().getServerEncryptedClientKeyHeaderName()); + if (ObjectUtil.isNotNull(encryptKey)) { + if (ObjectUtil.isNotNull(merchantId)) { + needSignMap.put("X-Security-Tenant-Id", String.valueOf(merchantId)); + } + + needSignMap.put("X-Security-Token", token); + String serverEncryptedClientKey = ((HttpServletRequest) WebContext.get().getRequest().get()).getHeader(secureProperties.getSecurity().getServerEncryptedClientKeyHeaderName()); + String clientKey = SmUtils.decryptBySm4WithServerKey(serverEncryptedClientKey); + securityTokenSign = SmUtils.signAuthTokenBySm3(needSignMap, clientKey); + } + } + + return securityTokenSign; + } +} diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/auth/user/dto/UserLoginDTO.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/auth/user/dto/UserLoginDTO.java new file mode 100644 index 00000000..d696f0ae --- /dev/null +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/auth/user/dto/UserLoginDTO.java @@ -0,0 +1,88 @@ +package com.bonus.canteen.core.auth.user.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import javax.validation.constraints.NotBlank; + +import java.io.Serializable; + +@ApiModel("登录入参") +public class UserLoginDTO implements Serializable { + @ApiModelProperty("用户名") + private @NotBlank( + message = "用户名不能为空" +) String username; + @ApiModelProperty("密码") + private @NotBlank( + message = "密码不能为空" +) String password; + private String scope; + private String grant_type; + private String role; + private String openid; + + public String getUsername() { + return this.username; + } + + public String getPassword() { + return this.password; + } + + public String getScope() { + return this.scope; + } + + public String getGrant_type() { + return this.grant_type; + } + + public String getRole() { + return this.role; + } + + public String getOpenid() { + return this.openid; + } + + public void setUsername(final String username) { + this.username = username; + } + + public void setPassword(final String password) { + this.password = password; + } + + public void setScope(final String scope) { + this.scope = scope; + } + + public void setGrant_type(final String grant_type) { + this.grant_type = grant_type; + } + + public void setRole(final String role) { + this.role = role; + } + + public void setOpenid(final String openid) { + this.openid = openid; + } + + public String toString() { + String var10000 = this.getUsername(); + return "MgrUserLoginDTO(username=" + var10000 + ", password=" + this.getPassword() + ", scope=" + this.getScope() + ", grant_type=" + this.getGrant_type() + ", role=" + this.getRole() + ", openid=" + this.getOpenid() + ")"; + } + + public UserLoginDTO(final String username, final String password, final String scope, final String grant_type, final String role, final String openid) { + this.username = username; + this.password = password; + this.scope = scope; + this.grant_type = grant_type; + this.role = role; + this.openid = openid; + } + + public UserLoginDTO() { + } +}