From 9a289e2f9e7da9b07517f4a229a2b132bf4551c4 Mon Sep 17 00:00:00 2001 From: Dftre <3066417822@qq.com> Date: Mon, 10 Jun 2024 03:42:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=88=B6=E5=AE=9A=E8=A7=84?= =?UTF-8?q?=E5=88=99=EF=BC=8C=E5=8E=9F=E6=9D=A5=E5=8F=ABoauth=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=B8=8D=E5=90=88=E9=80=82=EF=BC=8C=E5=BA=94=E8=AF=A5?= =?UTF-8?q?=E5=8F=ABauth=E6=A8=A1=E5=9D=97=EF=BC=8C=E9=87=8C=E9=9D=A2?= =?UTF-8?q?=E5=88=86=E4=B8=BAtfa=E5=8F=8C=E5=9B=A0=E7=B4=A0=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=E5=92=8Coauth2.0=E7=AC=AC=E4=B8=89=E6=96=B9=E8=AE=A4?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/支付系统订单状态图.drawio | 14 +- doc/邮箱或短信验证码登录注册重置流程图.drawio | 260 +++++++++++++++ pom.xml | 4 +- ruoyi-admin/pom.xml | 2 +- .../controller/monitor/CacheController.java | 2 + ...ication-oauth.yml => application-auth.yml} | 7 +- .../src/main/resources/application.yml | 2 +- {ruoyi-oauth => ruoyi-auth}/pom.xml | 18 +- .../ruoyi-auth-common}/pom.xml | 4 +- .../controller/OauthUserController.java | 6 +- .../ruoyi/auth}/common/domain/OauthUser.java | 2 +- .../common/enums/OauthVerificationUse.java | 53 ++++ .../auth}/common/mapper/OauthUserMapper.java | 4 +- .../common/service/IOauthUserService.java | 4 +- .../service/OauthVerificationCodeService.java | 6 +- .../service/impl/OauthUserServiceImpl.java | 8 +- .../auth/common/utils/RandomCodeUtil.java | 28 ++ .../mapper/common/OauthUserMapper.xml | 2 +- .../ruoyi-auth-starter}/pom.xml | 10 +- .../ruoyi-oauth-justauth/pom.xml | 4 +- .../controller/SysAuthController.java | 4 +- .../ruoyi/oauth/justauth/utils/AuthUtils.java | 0 .../ruoyi-oauth-wx/pom.xml | 4 +- .../oauth/wx/constant/WxMiniAppConstant.java | 0 .../oauth/wx/constant/WxPubConstant.java | 0 .../wx/controller/WxLoginController.java | 4 +- .../wx/service/Impl/WxLoginServiceImpl.java | 11 +- .../oauth/wx/service/WxLoginService.java | 2 +- .../ruoyi-tfa-email}/pom.xml | 6 +- .../ruoyi/tfa/email/config/EmailConfig.java | 18 ++ .../email/controller/MailAuthController.java | 32 ++ .../ruoyi/tfa/email/service/IMailService.java | 6 + .../email/service/impl/MailServiceImpl.java | 156 +++++++++ .../com/ruoyi/tfa/email/utils/EmailUtil.java | 20 ++ .../ruoyi-tfa-phone}/pom.xml | 6 +- .../ruoyi/tfa/phone/constant/DySmsConfig.java | 12 +- .../phone/controller/DySmsAuthController.java | 37 +++ .../ruoyi/tfa}/phone/enums/DySmsTemplate.java | 2 +- .../ruoyi/tfa/phone/service/DySmsService.java | 13 + .../phone/service/Impl/DySmsServiceImpl.java | 166 ++++++++++ .../com/ruoyi/tfa}/phone/utils/DySmsUtil.java | 28 +- .../ruoyi/common/constant/CacheConstants.java | 13 +- .../common/core/domain/model/LoginBody.java | 35 ++ .../core/domain/model/RegisterBody.java | 3 + .../common/enums/OauthVerificationUse.java | 37 --- .../oauth/common/utils/HttpClientUtil.java | 298 ------------------ .../email/controller/MailController.java | 19 -- .../oauth/email/service/IMailService.java | 7 - .../email/service/impl/MailServiceImpl.java | 153 --------- .../phone/controller/DySmsController.java | 153 --------- .../oauth/phone/service/DySmsService.java | 14 - .../phone/service/Impl/DySmsServiceImpl.java | 183 ----------- 52 files changed, 923 insertions(+), 959 deletions(-) create mode 100644 doc/邮箱或短信验证码登录注册重置流程图.drawio rename ruoyi-admin/src/main/resources/{application-oauth.yml => application-auth.yml} (81%) rename {ruoyi-oauth => ruoyi-auth}/pom.xml (86%) rename {ruoyi-oauth/ruoyi-oauth-common => ruoyi-auth/ruoyi-auth-common}/pom.xml (90%) rename {ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth => ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth}/common/controller/OauthUserController.java (96%) rename {ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth => ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth}/common/domain/OauthUser.java (99%) create mode 100644 ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/enums/OauthVerificationUse.java rename {ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth => ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth}/common/mapper/OauthUserMapper.java (96%) rename {ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth => ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth}/common/service/IOauthUserService.java (96%) rename {ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth => ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth}/common/service/OauthVerificationCodeService.java (53%) rename {ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth => ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth}/common/service/impl/OauthUserServiceImpl.java (94%) create mode 100644 ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/utils/RandomCodeUtil.java rename {ruoyi-oauth/ruoyi-oauth-common => ruoyi-auth/ruoyi-auth-common}/src/main/resources/mapper/common/OauthUserMapper.xml (99%) rename {ruoyi-oauth/ruoyi-oauth-starter => ruoyi-auth/ruoyi-auth-starter}/pom.xml (83%) rename {ruoyi-oauth => ruoyi-auth}/ruoyi-oauth-justauth/pom.xml (90%) rename {ruoyi-oauth => ruoyi-auth}/ruoyi-oauth-justauth/src/main/java/com/ruoyi/oauth/justauth/controller/SysAuthController.java (98%) rename {ruoyi-oauth => ruoyi-auth}/ruoyi-oauth-justauth/src/main/java/com/ruoyi/oauth/justauth/utils/AuthUtils.java (100%) rename {ruoyi-oauth => ruoyi-auth}/ruoyi-oauth-wx/pom.xml (86%) rename {ruoyi-oauth => ruoyi-auth}/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxMiniAppConstant.java (100%) rename {ruoyi-oauth => ruoyi-auth}/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxPubConstant.java (100%) rename {ruoyi-oauth => ruoyi-auth}/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/controller/WxLoginController.java (96%) rename {ruoyi-oauth => ruoyi-auth}/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/Impl/WxLoginServiceImpl.java (94%) rename {ruoyi-oauth => ruoyi-auth}/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/WxLoginService.java (85%) rename {ruoyi-oauth/ruoyi-oauth-email => ruoyi-auth/ruoyi-tfa-email}/pom.xml (88%) create mode 100644 ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/config/EmailConfig.java create mode 100644 ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/controller/MailAuthController.java create mode 100644 ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/service/IMailService.java create mode 100644 ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/service/impl/MailServiceImpl.java create mode 100644 ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/utils/EmailUtil.java rename {ruoyi-oauth/ruoyi-oauth-phone => ruoyi-auth/ruoyi-tfa-phone}/pom.xml (83%) rename ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/constant/DySmsConstant.java => ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/constant/DySmsConfig.java (72%) create mode 100644 ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/controller/DySmsAuthController.java rename {ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth => ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa}/phone/enums/DySmsTemplate.java (97%) create mode 100644 ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/service/DySmsService.java create mode 100644 ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/service/Impl/DySmsServiceImpl.java rename {ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth => ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa}/phone/utils/DySmsUtil.java (71%) delete mode 100644 ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/enums/OauthVerificationUse.java delete mode 100644 ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/utils/HttpClientUtil.java delete mode 100644 ruoyi-oauth/ruoyi-oauth-email/src/main/java/com/ruoyi/oauth/email/controller/MailController.java delete mode 100644 ruoyi-oauth/ruoyi-oauth-email/src/main/java/com/ruoyi/oauth/email/service/IMailService.java delete mode 100644 ruoyi-oauth/ruoyi-oauth-email/src/main/java/com/ruoyi/oauth/email/service/impl/MailServiceImpl.java delete mode 100644 ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/controller/DySmsController.java delete mode 100644 ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/service/DySmsService.java delete mode 100644 ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/service/Impl/DySmsServiceImpl.java diff --git a/doc/支付系统订单状态图.drawio b/doc/支付系统订单状态图.drawio index 38be5c9..ce41fa8 100644 --- a/doc/支付系统订单状态图.drawio +++ b/doc/支付系统订单状态图.drawio @@ -1,6 +1,6 @@ - + @@ -179,7 +179,7 @@ - + @@ -189,29 +189,29 @@ - + - + - + - + - + diff --git a/doc/邮箱或短信验证码登录注册重置流程图.drawio b/doc/邮箱或短信验证码登录注册重置流程图.drawio new file mode 100644 index 0000000..7e623e9 --- /dev/null +++ b/doc/邮箱或短信验证码登录注册重置流程图.drawio @@ -0,0 +1,260 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 297e43b..2e088cc 100644 --- a/pom.xml +++ b/pom.xml @@ -251,7 +251,7 @@ com.ruoyi - ruoyi-oauth-starter + ruoyi-auth-starter ${ruoyi.version} @@ -290,7 +290,7 @@ ruoyi-framework ruoyi-system ruoyi-common - ruoyi-oauth + ruoyi-auth ruoyi-pay ruoyi-middleware ruoyi-plugins diff --git a/ruoyi-admin/pom.xml b/ruoyi-admin/pom.xml index 4929f8b..1d36343 100644 --- a/ruoyi-admin/pom.xml +++ b/ruoyi-admin/pom.xml @@ -38,7 +38,7 @@ com.ruoyi - ruoyi-oauth-starter + ruoyi-auth-starter diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java index 969e421..a67a509 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java @@ -42,6 +42,8 @@ public class CacheController caches.add(new SysCache(CacheConstants.SYS_CONFIG_KEY, "配置信息")); caches.add(new SysCache(CacheConstants.SYS_DICT_KEY, "数据字典")); caches.add(new SysCache(CacheConstants.CAPTCHA_CODE_KEY, "验证码")); + caches.add(new SysCache(CacheConstants.PHONE_CODES, "短信验证码")); + caches.add(new SysCache(CacheConstants.EMAIL_CODES, "邮箱验证码")); caches.add(new SysCache(CacheConstants.REPEAT_SUBMIT_KEY, "防重提交")); caches.add(new SysCache(CacheConstants.RATE_LIMIT_KEY, "限流处理")); caches.add(new SysCache(CacheConstants.PWD_ERR_CNT_KEY, "密码错误次数")); diff --git a/ruoyi-admin/src/main/resources/application-oauth.yml b/ruoyi-admin/src/main/resources/application-auth.yml similarity index 81% rename from ruoyi-admin/src/main/resources/application-oauth.yml rename to ruoyi-admin/src/main/resources/application-auth.yml index 1049064..8669e17 100644 --- a/ruoyi-admin/src/main/resources/application-oauth.yml +++ b/ruoyi-admin/src/main/resources/application-auth.yml @@ -1,5 +1,5 @@ # 请输入自己的appid和appsecret -oauth: +oauth: wx: miniapp: open: true @@ -12,15 +12,18 @@ oauth: appSecret: appSecret url: https://api.weixin.qq.com/sns/oauth2/access_token +tfa: phone: dysms: + # 阿里云短信 AccessKey ID appId: appId + # 阿里云短信 AccessKey Secret appSecret: appSecret spring: mail: host: smtp.qq.com # 邮箱地址 - username: username + username: email # 授权码 password: password diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 7cb7c8a..085340b 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -65,7 +65,7 @@ spring: # 国际化资源文件路径 basename: i18n/messages profiles: - active: druid,mybatis,oauth,pay,middleware + active: druid,mybatis,auth,pay,middleware # 文件上传 servlet: multipart: diff --git a/ruoyi-oauth/pom.xml b/ruoyi-auth/pom.xml similarity index 86% rename from ruoyi-oauth/pom.xml rename to ruoyi-auth/pom.xml index 7b60558..1091c5b 100644 --- a/ruoyi-oauth/pom.xml +++ b/ruoyi-auth/pom.xml @@ -8,7 +8,7 @@ 4.0.0 - ruoyi-oauth + ruoyi-auth 1.16.6 @@ -24,7 +24,7 @@ com.ruoyi - ruoyi-oauth-common + ruoyi-auth-common ${ruoyi.version} @@ -66,21 +66,21 @@ com.ruoyi - ruoyi-oauth-phone + ruoyi-tfa-phone ${ruoyi.version} com.ruoyi - ruoyi-oauth-email + ruoyi-tfa-email ${ruoyi.version} com.ruoyi - ruoyi-oauth-starter + ruoyi-auth-starter ${ruoyi.version} @@ -88,12 +88,12 @@ - ruoyi-oauth-common + ruoyi-auth-common ruoyi-oauth-justauth ruoyi-oauth-wx - ruoyi-oauth-phone - ruoyi-oauth-email - ruoyi-oauth-starter + ruoyi-tfa-phone + ruoyi-tfa-email + ruoyi-auth-starter pom \ No newline at end of file diff --git a/ruoyi-oauth/ruoyi-oauth-common/pom.xml b/ruoyi-auth/ruoyi-auth-common/pom.xml similarity index 90% rename from ruoyi-oauth/ruoyi-oauth-common/pom.xml rename to ruoyi-auth/ruoyi-auth-common/pom.xml index 37b7001..db0109d 100644 --- a/ruoyi-oauth/ruoyi-oauth-common/pom.xml +++ b/ruoyi-auth/ruoyi-auth-common/pom.xml @@ -2,13 +2,13 @@ - ruoyi-oauth + ruoyi-auth com.ruoyi 3.8.7.3.4 4.0.0 - ruoyi-oauth-common + ruoyi-auth-common system系统模块 diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/controller/OauthUserController.java b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/controller/OauthUserController.java similarity index 96% rename from ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/controller/OauthUserController.java rename to ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/controller/OauthUserController.java index 36a224c..f482345 100644 --- a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/controller/OauthUserController.java +++ b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/controller/OauthUserController.java @@ -1,4 +1,4 @@ -package com.ruoyi.oauth.common.controller; +package com.ruoyi.auth.common.controller; import java.util.List; @@ -13,14 +13,14 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import com.ruoyi.auth.common.domain.OauthUser; +import com.ruoyi.auth.common.service.IOauthUserService; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.utils.poi.ExcelUtil; -import com.ruoyi.oauth.common.domain.OauthUser; -import com.ruoyi.oauth.common.service.IOauthUserService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/domain/OauthUser.java b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/domain/OauthUser.java similarity index 99% rename from ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/domain/OauthUser.java rename to ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/domain/OauthUser.java index 355da7c..650b514 100644 --- a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/domain/OauthUser.java +++ b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/domain/OauthUser.java @@ -1,4 +1,4 @@ -package com.ruoyi.oauth.common.domain; +package com.ruoyi.auth.common.domain; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; diff --git a/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/enums/OauthVerificationUse.java b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/enums/OauthVerificationUse.java new file mode 100644 index 0000000..14a52fa --- /dev/null +++ b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/enums/OauthVerificationUse.java @@ -0,0 +1,53 @@ +package com.ruoyi.auth.common.enums; + +public enum OauthVerificationUse { + + LOGIN("登录", "login"), + REGISTER("注册", "register"), + DISABLE("禁用", "disable"), + RESET("重置", "reset"), + BIND("绑定", "bind"), + OTHER("其他", "other"); + + private String name; + private String value; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public static OauthVerificationUse getByValue(String value) { + for (OauthVerificationUse use : OauthVerificationUse.values()) { + if (use.getValue().equals(value)) { + return use; + } + } + return null; + } + + public static OauthVerificationUse getByName(String name) { + for (OauthVerificationUse use : OauthVerificationUse.values()) { + if (use.getName().equals(name)) { + return use; + } + } + return null; + } + + private OauthVerificationUse(String name, String value) { + this.name = name; + this.value = value; + } +} diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/mapper/OauthUserMapper.java b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/mapper/OauthUserMapper.java similarity index 96% rename from ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/mapper/OauthUserMapper.java rename to ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/mapper/OauthUserMapper.java index e6c160f..656630e 100644 --- a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/mapper/OauthUserMapper.java +++ b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/mapper/OauthUserMapper.java @@ -1,10 +1,10 @@ -package com.ruoyi.oauth.common.mapper; +package com.ruoyi.auth.common.mapper; import java.util.List; import org.apache.ibatis.annotations.Param; -import com.ruoyi.oauth.common.domain.OauthUser; +import com.ruoyi.auth.common.domain.OauthUser; /** * 第三方认证Mapper接口 diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/IOauthUserService.java b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/service/IOauthUserService.java similarity index 96% rename from ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/IOauthUserService.java rename to ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/service/IOauthUserService.java index 7cb7b6a..bc01edf 100644 --- a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/IOauthUserService.java +++ b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/service/IOauthUserService.java @@ -1,9 +1,9 @@ -package com.ruoyi.oauth.common.service; +package com.ruoyi.auth.common.service; import java.util.List; +import com.ruoyi.auth.common.domain.OauthUser; import com.ruoyi.common.core.domain.entity.SysUser; -import com.ruoyi.oauth.common.domain.OauthUser; /** * 第三方认证Service接口 diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/OauthVerificationCodeService.java b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/service/OauthVerificationCodeService.java similarity index 53% rename from ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/OauthVerificationCodeService.java rename to ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/service/OauthVerificationCodeService.java index acef303..8fa22f0 100644 --- a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/OauthVerificationCodeService.java +++ b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/service/OauthVerificationCodeService.java @@ -1,6 +1,6 @@ -package com.ruoyi.oauth.common.service; +package com.ruoyi.auth.common.service; -import com.ruoyi.oauth.common.enums.OauthVerificationUse; +import com.ruoyi.auth.common.enums.OauthVerificationUse; /** * code认证方式接口 @@ -10,6 +10,6 @@ import com.ruoyi.oauth.common.enums.OauthVerificationUse; */ public interface OauthVerificationCodeService { public boolean sendCode(String o, String code,OauthVerificationUse use) throws Exception; - public String checkCode(String o, String code,OauthVerificationUse use) throws Exception; + public boolean checkCode(String o, String code,OauthVerificationUse use) throws Exception; } diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/impl/OauthUserServiceImpl.java b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/service/impl/OauthUserServiceImpl.java similarity index 94% rename from ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/impl/OauthUserServiceImpl.java rename to ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/service/impl/OauthUserServiceImpl.java index e08df55..4d4ebc0 100644 --- a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/impl/OauthUserServiceImpl.java +++ b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/service/impl/OauthUserServiceImpl.java @@ -1,14 +1,14 @@ -package com.ruoyi.oauth.common.service.impl; +package com.ruoyi.auth.common.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import com.ruoyi.auth.common.domain.OauthUser; +import com.ruoyi.auth.common.mapper.OauthUserMapper; +import com.ruoyi.auth.common.service.IOauthUserService; import com.ruoyi.common.core.domain.entity.SysUser; -import com.ruoyi.oauth.common.domain.OauthUser; -import com.ruoyi.oauth.common.mapper.OauthUserMapper; -import com.ruoyi.oauth.common.service.IOauthUserService; import com.ruoyi.system.mapper.SysUserMapper; /** diff --git a/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/utils/RandomCodeUtil.java b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/utils/RandomCodeUtil.java new file mode 100644 index 0000000..1156581 --- /dev/null +++ b/ruoyi-auth/ruoyi-auth-common/src/main/java/com/ruoyi/auth/common/utils/RandomCodeUtil.java @@ -0,0 +1,28 @@ +package com.ruoyi.auth.common.utils; + +import java.util.Random; + +public class RandomCodeUtil { + + public static String randomString(String characters, int length) { + StringBuilder result = new StringBuilder(); + Random random = new Random(); + + for (int i = 0; i < length; i++) { + int index = random.nextInt(characters.length()); + result.append(characters.charAt(index)); + } + + return result.toString(); + } + + public static String numberCode(int length) { + String characters = "0123456789"; + return randomString(characters, length); + } + + public static String code(int length) { + String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + return randomString(characters, length); + } +} diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/resources/mapper/common/OauthUserMapper.xml b/ruoyi-auth/ruoyi-auth-common/src/main/resources/mapper/common/OauthUserMapper.xml similarity index 99% rename from ruoyi-oauth/ruoyi-oauth-common/src/main/resources/mapper/common/OauthUserMapper.xml rename to ruoyi-auth/ruoyi-auth-common/src/main/resources/mapper/common/OauthUserMapper.xml index 31f37fd..4e7e851 100644 --- a/ruoyi-oauth/ruoyi-oauth-common/src/main/resources/mapper/common/OauthUserMapper.xml +++ b/ruoyi-auth/ruoyi-auth-common/src/main/resources/mapper/common/OauthUserMapper.xml @@ -2,7 +2,7 @@ - + diff --git a/ruoyi-oauth/ruoyi-oauth-starter/pom.xml b/ruoyi-auth/ruoyi-auth-starter/pom.xml similarity index 83% rename from ruoyi-oauth/ruoyi-oauth-starter/pom.xml rename to ruoyi-auth/ruoyi-auth-starter/pom.xml index 0b41811..dd1a0c5 100644 --- a/ruoyi-oauth/ruoyi-oauth-starter/pom.xml +++ b/ruoyi-auth/ruoyi-auth-starter/pom.xml @@ -3,13 +3,13 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - ruoyi-oauth + ruoyi-auth com.ruoyi 3.8.7.3.4 4.0.0 - ruoyi-oauth-starter + ruoyi-auth-starter 第三方认证模块 @@ -19,7 +19,7 @@ com.ruoyi - ruoyi-oauth-common + ruoyi-auth-common @@ -37,13 +37,13 @@ com.ruoyi - ruoyi-oauth-phone + ruoyi-tfa-phone com.ruoyi - ruoyi-oauth-email + ruoyi-tfa-email diff --git a/ruoyi-oauth/ruoyi-oauth-justauth/pom.xml b/ruoyi-auth/ruoyi-oauth-justauth/pom.xml similarity index 90% rename from ruoyi-oauth/ruoyi-oauth-justauth/pom.xml rename to ruoyi-auth/ruoyi-oauth-justauth/pom.xml index c298a84..63297cf 100644 --- a/ruoyi-oauth/ruoyi-oauth-justauth/pom.xml +++ b/ruoyi-auth/ruoyi-oauth-justauth/pom.xml @@ -2,7 +2,7 @@ - ruoyi-oauth + ruoyi-auth com.ruoyi 3.8.7.3.4 @@ -19,7 +19,7 @@ com.ruoyi - ruoyi-oauth-common + ruoyi-auth-common diff --git a/ruoyi-oauth/ruoyi-oauth-justauth/src/main/java/com/ruoyi/oauth/justauth/controller/SysAuthController.java b/ruoyi-auth/ruoyi-oauth-justauth/src/main/java/com/ruoyi/oauth/justauth/controller/SysAuthController.java similarity index 98% rename from ruoyi-oauth/ruoyi-oauth-justauth/src/main/java/com/ruoyi/oauth/justauth/controller/SysAuthController.java rename to ruoyi-auth/ruoyi-oauth-justauth/src/main/java/com/ruoyi/oauth/justauth/controller/SysAuthController.java index e006a80..8652222 100644 --- a/ruoyi-oauth/ruoyi-oauth-justauth/src/main/java/com/ruoyi/oauth/justauth/controller/SysAuthController.java +++ b/ruoyi-auth/ruoyi-oauth-justauth/src/main/java/com/ruoyi/oauth/justauth/controller/SysAuthController.java @@ -25,8 +25,8 @@ import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.framework.web.service.SysPermissionService; import com.ruoyi.framework.web.service.TokenService; -import com.ruoyi.oauth.common.domain.OauthUser; -import com.ruoyi.oauth.common.service.IOauthUserService; +import com.ruoyi.auth.common.domain.OauthUser; +import com.ruoyi.auth.common.service.IOauthUserService; import com.ruoyi.oauth.justauth.utils.AuthUtils; import com.ruoyi.system.service.ISysUserService; diff --git a/ruoyi-oauth/ruoyi-oauth-justauth/src/main/java/com/ruoyi/oauth/justauth/utils/AuthUtils.java b/ruoyi-auth/ruoyi-oauth-justauth/src/main/java/com/ruoyi/oauth/justauth/utils/AuthUtils.java similarity index 100% rename from ruoyi-oauth/ruoyi-oauth-justauth/src/main/java/com/ruoyi/oauth/justauth/utils/AuthUtils.java rename to ruoyi-auth/ruoyi-oauth-justauth/src/main/java/com/ruoyi/oauth/justauth/utils/AuthUtils.java diff --git a/ruoyi-oauth/ruoyi-oauth-wx/pom.xml b/ruoyi-auth/ruoyi-oauth-wx/pom.xml similarity index 86% rename from ruoyi-oauth/ruoyi-oauth-wx/pom.xml rename to ruoyi-auth/ruoyi-oauth-wx/pom.xml index 0309b64..d0add5e 100644 --- a/ruoyi-oauth/ruoyi-oauth-wx/pom.xml +++ b/ruoyi-auth/ruoyi-oauth-wx/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - ruoyi-oauth + ruoyi-auth com.ruoyi 3.8.7.3.4 @@ -20,7 +20,7 @@ com.ruoyi - ruoyi-oauth-common + ruoyi-auth-common diff --git a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxMiniAppConstant.java b/ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxMiniAppConstant.java similarity index 100% rename from ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxMiniAppConstant.java rename to ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxMiniAppConstant.java diff --git a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxPubConstant.java b/ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxPubConstant.java similarity index 100% rename from ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxPubConstant.java rename to ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxPubConstant.java diff --git a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/controller/WxLoginController.java b/ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/controller/WxLoginController.java similarity index 96% rename from ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/controller/WxLoginController.java rename to ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/controller/WxLoginController.java index ca1c6b2..cd0265b 100644 --- a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/controller/WxLoginController.java +++ b/ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/controller/WxLoginController.java @@ -11,8 +11,8 @@ import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.utils.StringUtils; -import com.ruoyi.oauth.common.domain.OauthUser; -import com.ruoyi.oauth.common.service.IOauthUserService; +import com.ruoyi.auth.common.domain.OauthUser; +import com.ruoyi.auth.common.service.IOauthUserService; import com.ruoyi.oauth.wx.constant.WxMiniAppConstant; import com.ruoyi.oauth.wx.constant.WxPubConstant; import com.ruoyi.oauth.wx.service.Impl.WxLoginServiceImpl; diff --git a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/Impl/WxLoginServiceImpl.java b/ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/Impl/WxLoginServiceImpl.java similarity index 94% rename from ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/Impl/WxLoginServiceImpl.java rename to ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/Impl/WxLoginServiceImpl.java index 5b5dfb8..c1a5094 100644 --- a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/Impl/WxLoginServiceImpl.java +++ b/ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/Impl/WxLoginServiceImpl.java @@ -12,11 +12,11 @@ import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.http.HttpClientUtil; import com.ruoyi.framework.web.service.TokenService; import com.ruoyi.framework.web.service.UserDetailsServiceImpl; -import com.ruoyi.oauth.common.domain.OauthUser; -import com.ruoyi.oauth.common.service.IOauthUserService; -import com.ruoyi.oauth.common.utils.HttpClientUtil; +import com.ruoyi.auth.common.domain.OauthUser; +import com.ruoyi.auth.common.service.IOauthUserService; import com.ruoyi.oauth.wx.constant.WxMiniAppConstant; import com.ruoyi.oauth.wx.constant.WxPubConstant; import com.ruoyi.oauth.wx.service.WxLoginService; @@ -29,9 +29,6 @@ public class WxLoginServiceImpl implements WxLoginService { @Autowired private WxPubConstant wxH5Constant; - - @Autowired - private HttpClientUtil httpClientUtil; @Autowired private TokenService tokenService; @Autowired @@ -44,7 +41,7 @@ public class WxLoginServiceImpl implements WxLoginService { public Map doAuth(String url, String appid, String secret, String code) { String getMessageUrl = url + "?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code"; - String result = httpClientUtil.sendHttpGet(getMessageUrl); + String result = HttpClientUtil.sendHttpGet(getMessageUrl); JSONObject jsonObject = JSON.parseObject(result); if (jsonObject.containsKey("openid")) { String openid = jsonObject.getString("openid"); diff --git a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/WxLoginService.java b/ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/WxLoginService.java similarity index 85% rename from ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/WxLoginService.java rename to ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/WxLoginService.java index c4f3ccf..2171880 100644 --- a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/WxLoginService.java +++ b/ruoyi-auth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/WxLoginService.java @@ -1,6 +1,6 @@ package com.ruoyi.oauth.wx.service; -import com.ruoyi.oauth.common.domain.OauthUser; +import com.ruoyi.auth.common.domain.OauthUser; public interface WxLoginService { public String doLoginMiniApp(String code); diff --git a/ruoyi-oauth/ruoyi-oauth-email/pom.xml b/ruoyi-auth/ruoyi-tfa-email/pom.xml similarity index 88% rename from ruoyi-oauth/ruoyi-oauth-email/pom.xml rename to ruoyi-auth/ruoyi-tfa-email/pom.xml index 7d4d10e..16904d5 100644 --- a/ruoyi-oauth/ruoyi-oauth-email/pom.xml +++ b/ruoyi-auth/ruoyi-tfa-email/pom.xml @@ -2,13 +2,13 @@ - ruoyi-oauth + ruoyi-auth com.ruoyi 3.8.7.3.4 4.0.0 - ruoyi-oauth-email + ruoyi-tfa-email 邮箱认证模块 @@ -19,7 +19,7 @@ com.ruoyi - ruoyi-oauth-common + ruoyi-auth-common org.springframework.boot diff --git a/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/config/EmailConfig.java b/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/config/EmailConfig.java new file mode 100644 index 0000000..46babbc --- /dev/null +++ b/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/config/EmailConfig.java @@ -0,0 +1,18 @@ +package com.ruoyi.tfa.email.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class EmailConfig { + @Value("${spring.mail.username}") + private String from; + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } +} diff --git a/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/controller/MailAuthController.java b/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/controller/MailAuthController.java new file mode 100644 index 0000000..bf6e98a --- /dev/null +++ b/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/controller/MailAuthController.java @@ -0,0 +1,32 @@ +package com.ruoyi.tfa.email.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.core.domain.model.LoginBody; +import com.ruoyi.tfa.email.service.impl.MailServiceImpl; + +@RestController +@RequestMapping("/auth/mail") +public class MailAuthController extends BaseController { + + @Autowired + MailServiceImpl serviceImpl; + + @PostMapping("/send/bind") // 发送验证码 + public AjaxResult send(@RequestBody LoginBody loginBody) { + serviceImpl.doBind(loginBody); + return success(); + } + + @PostMapping("/verify/bind") // 发送验证码 + public AjaxResult verify(@RequestBody LoginBody loginBody) { + serviceImpl.doBindVerify(loginBody); + return success(); + } +} diff --git a/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/service/IMailService.java b/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/service/IMailService.java new file mode 100644 index 0000000..7fed0bf --- /dev/null +++ b/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/service/IMailService.java @@ -0,0 +1,6 @@ +package com.ruoyi.tfa.email.service; + +import com.ruoyi.auth.common.service.OauthVerificationCodeService; + +public interface IMailService extends OauthVerificationCodeService { +} diff --git a/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/service/impl/MailServiceImpl.java b/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/service/impl/MailServiceImpl.java new file mode 100644 index 0000000..050904c --- /dev/null +++ b/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/service/impl/MailServiceImpl.java @@ -0,0 +1,156 @@ +package com.ruoyi.tfa.email.service.impl; + +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.ruoyi.auth.common.enums.OauthVerificationUse; +import com.ruoyi.auth.common.utils.RandomCodeUtil; +import com.ruoyi.common.constant.CacheConstants; +import com.ruoyi.common.core.domain.entity.SysUser; +import com.ruoyi.common.core.domain.model.LoginBody; +import com.ruoyi.common.core.domain.model.LoginUser; +import com.ruoyi.common.core.domain.model.RegisterBody; +import com.ruoyi.common.exception.ServiceException; +import com.ruoyi.common.utils.CacheUtils; +import com.ruoyi.common.utils.SecurityUtils; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.framework.web.service.TokenService; +import com.ruoyi.framework.web.service.UserDetailsServiceImpl; +import com.ruoyi.system.service.ISysUserService; +import com.ruoyi.tfa.email.service.IMailService; +import com.ruoyi.tfa.email.utils.EmailUtil; + +@Service +public class MailServiceImpl implements IMailService { + + @Autowired + private ISysUserService userService; + @Autowired + private TokenService tokenService; + @Autowired + private UserDetailsServiceImpl userDetailsServiceImpl; + private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class); + + @Override + public boolean sendCode(String email, String code, OauthVerificationUse use) { + if (CacheUtils.hasKey(CacheConstants.EMAIL_CODES, use.getValue() + email)) { + throw new ServiceException("当前验证码未失效,请在1分钟后再发送短信"); + } + + try { + EmailUtil.sendMessage(email, "验证码邮件", "您收到的验证码是:" + code); + CacheUtils.put(CacheConstants.EMAIL_CODES, use.getValue() + email, code, 10, TimeUnit.MINUTES); + log.info("发送邮箱验证码成功:{ email: " + email + " code:" + code + "}"); + return true; + } catch (Exception e) { + throw new ServiceException("发送手机验证码异常:" + email); + } + } + + @Override + public boolean checkCode(String email, String code, OauthVerificationUse use) { + if (StringUtils.isEmpty(code)) + return false; + String cachedCode = CacheUtils.get(CacheConstants.EMAIL_CODES, use.getValue() + email, String.class); // 从缓存中获取验证码 + CacheUtils.remove(CacheConstants.EMAIL_CODES, use.getValue() + email); + return code.equals(cachedCode); + } + + public void doLogin(LoginBody loginBody, boolean isRegister) { + SysUser sysUser = userService.selectUserByEmail(loginBody.getEmail()); + if (sysUser == null && !isRegister) { + throw new ServiceException("该邮箱未绑定用户"); + } else { + sendCode(loginBody.getEmail(), RandomCodeUtil.numberCode(6), OauthVerificationUse.LOGIN); + } + } + + public String doLoginVerify(LoginBody loginBody, boolean isRegister) { + if (checkCode(loginBody.getEmail(), loginBody.getCode(), OauthVerificationUse.LOGIN)) { + SysUser sysUser = userService.selectUserByEmail(loginBody.getEmail()); + if (sysUser == null) { + if (isRegister) { + sysUser = new SysUser(); + sysUser.setUserName(loginBody.getEmail()); + sysUser.setPassword(SecurityUtils.encryptPassword(RandomCodeUtil.code(16))); + sysUser.setEmail(loginBody.getEmail()); + } else { + throw new ServiceException("该邮箱未绑定用户"); + } + } + LoginUser loginUser = (LoginUser) userDetailsServiceImpl.createLoginUser(sysUser); + return tokenService.createToken(loginUser); + } else { + throw new ServiceException("验证码错误"); + } + } + + public void doRegister(RegisterBody registerBody) { + SysUser sysUser = userService.selectUserByEmail(registerBody.getEmail()); + if (sysUser != null) { + throw new ServiceException("该邮箱已绑定用户"); + } else { + sendCode(registerBody.getEmail(), RandomCodeUtil.numberCode(6), OauthVerificationUse.REGISTER); + } + } + + public void doRegisterVerify(RegisterBody registerBody) { + if (checkCode(registerBody.getEmail(), registerBody.getCode(), OauthVerificationUse.REGISTER)) { + SysUser sysUser = new SysUser(); + sysUser.setUserName(registerBody.getEmail()); + sysUser.setPassword(SecurityUtils.encryptPassword(registerBody.getPassword())); + sysUser.setEmail(registerBody.getEmail()); + } else { + throw new ServiceException("验证码错误"); + } + } + + public void doReset(String email) { + SysUser sysUser = userService.selectUserByEmail(email); + if (sysUser == null) { + throw new ServiceException("该邮箱未绑定用户"); + } else { + sendCode(email, RandomCodeUtil.numberCode(6), OauthVerificationUse.RESET); + } + } + + public int doResetVerify(RegisterBody registerBody) { + if (checkCode(registerBody.getEmail(), registerBody.getCode(), OauthVerificationUse.RESET)) { + SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId()); + sysUser.setEmail(registerBody.getEmail()); + return userService.updateUser(sysUser); + } else { + throw new ServiceException("验证码错误"); + } + } + + public void doBind(LoginBody loginBody) { + SysUser sysUser = userService.selectUserByEmail(loginBody.getEmail()); + if (sysUser != null) { + throw new ServiceException("该邮箱已绑定用户"); + } + sysUser = userService.selectUserById(SecurityUtils.getUserId()); + if (!SecurityUtils.matchesPassword(loginBody.getPassword(), sysUser.getPassword())) { + throw new ServiceException("密码错误"); + } + sendCode(loginBody.getEmail(), RandomCodeUtil.numberCode(6), OauthVerificationUse.BIND); + } + + public void doBindVerify(LoginBody loginBody) { + if (checkCode(loginBody.getEmail(), loginBody.getCode(), OauthVerificationUse.BIND)) { + SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId()); + if (!SecurityUtils.matchesPassword(loginBody.getPassword(), sysUser.getPassword())) { + throw new ServiceException("密码错误"); + } + sysUser.setEmail(loginBody.getEmail()); + userService.updateUser(sysUser); + } else { + throw new ServiceException("验证码错误"); + } + } + +} diff --git a/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/utils/EmailUtil.java b/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/utils/EmailUtil.java new file mode 100644 index 0000000..f1f34d9 --- /dev/null +++ b/ruoyi-auth/ruoyi-tfa-email/src/main/java/com/ruoyi/tfa/email/utils/EmailUtil.java @@ -0,0 +1,20 @@ +package com.ruoyi.tfa.email.utils; + +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSenderImpl; + +import com.ruoyi.common.utils.spring.SpringUtils; +import com.ruoyi.tfa.email.config.EmailConfig; + +public class EmailUtil { + public static void sendMessage(String email, String title, String message) { + EmailConfig emailConfig = SpringUtils.getBean(EmailConfig.class); + JavaMailSenderImpl mailSender = SpringUtils.getBean(JavaMailSenderImpl.class); + SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); + simpleMailMessage.setSubject(title); + simpleMailMessage.setText(message); + simpleMailMessage.setFrom(emailConfig.getFrom()); + simpleMailMessage.setTo(email); + mailSender.send(simpleMailMessage); + } +} diff --git a/ruoyi-oauth/ruoyi-oauth-phone/pom.xml b/ruoyi-auth/ruoyi-tfa-phone/pom.xml similarity index 83% rename from ruoyi-oauth/ruoyi-oauth-phone/pom.xml rename to ruoyi-auth/ruoyi-tfa-phone/pom.xml index b35701c..dd9df67 100644 --- a/ruoyi-oauth/ruoyi-oauth-phone/pom.xml +++ b/ruoyi-auth/ruoyi-tfa-phone/pom.xml @@ -3,13 +3,13 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - ruoyi-oauth + ruoyi-auth com.ruoyi 3.8.7.3.4 4.0.0 - ruoyi-oauth-phone + ruoyi-tfa-phone 手机号认证模块 @@ -20,7 +20,7 @@ com.ruoyi - ruoyi-oauth-common + ruoyi-auth-common diff --git a/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/constant/DySmsConstant.java b/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/constant/DySmsConfig.java similarity index 72% rename from ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/constant/DySmsConstant.java rename to ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/constant/DySmsConfig.java index 3c6e481..058fd1d 100644 --- a/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/constant/DySmsConstant.java +++ b/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/constant/DySmsConfig.java @@ -1,7 +1,7 @@ -package com.ruoyi.oauth.phone.constant; +package com.ruoyi.tfa.phone.constant; import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; +import org.springframework.context.annotation.Configuration; /** * 手机号认证数据 @@ -9,11 +9,11 @@ import org.springframework.stereotype.Component; * @author Dftre * @date 2024-04-16 */ -@Component -public class DySmsConstant { - @Value("${oauth.phone.dysms.appId}") +@Configuration +public class DySmsConfig { + @Value("${tfa.phone.dysms.appId}") private String accessKeyId; - @Value("${oauth.phone.dysms.appSecret}") + @Value("${tfa.phone.dysms.appSecret}") private String accessKeySecret; public String getAccessKeyId() { diff --git a/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/controller/DySmsAuthController.java b/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/controller/DySmsAuthController.java new file mode 100644 index 0000000..b4e56b3 --- /dev/null +++ b/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/controller/DySmsAuthController.java @@ -0,0 +1,37 @@ +package com.ruoyi.tfa.phone.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.core.domain.model.LoginBody; +import com.ruoyi.tfa.phone.service.Impl.DySmsServiceImpl; + +/** + * 手机号认证Controller + * + * @author zlh + * @date 2024-04-16 + */ +@RestController +@RequestMapping("/tfa/phone") +public class DySmsAuthController extends BaseController { + @Autowired + public DySmsServiceImpl dySmsService; + + @PostMapping("/send/bind") // 发送验证码 + public AjaxResult send(@RequestBody LoginBody loginBody) { + dySmsService.doBind(loginBody); + return success(); + } + + @PostMapping("/verify/bind") // 发送验证码 + public AjaxResult verify(@RequestBody LoginBody loginBody) { + dySmsService.doBindVerify(loginBody); + return success(); + } +} diff --git a/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/enums/DySmsTemplate.java b/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/enums/DySmsTemplate.java similarity index 97% rename from ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/enums/DySmsTemplate.java rename to ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/enums/DySmsTemplate.java index 7036ec5..29ec646 100644 --- a/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/enums/DySmsTemplate.java +++ b/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/enums/DySmsTemplate.java @@ -1,4 +1,4 @@ -package com.ruoyi.oauth.phone.enums; +package com.ruoyi.tfa.phone.enums; import com.ruoyi.common.utils.StringUtils; diff --git a/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/service/DySmsService.java b/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/service/DySmsService.java new file mode 100644 index 0000000..27d58da --- /dev/null +++ b/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/service/DySmsService.java @@ -0,0 +1,13 @@ +package com.ruoyi.tfa.phone.service; + +import com.ruoyi.auth.common.service.OauthVerificationCodeService; + +/** + * 手机号认证Servcie + * + * @author zlh + * @date 2024-04-16 + */ +public interface DySmsService extends OauthVerificationCodeService { + +} diff --git a/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/service/Impl/DySmsServiceImpl.java b/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/service/Impl/DySmsServiceImpl.java new file mode 100644 index 0000000..c638652 --- /dev/null +++ b/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/service/Impl/DySmsServiceImpl.java @@ -0,0 +1,166 @@ +package com.ruoyi.tfa.phone.service.Impl; + +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.alibaba.fastjson2.JSONObject; +import com.ruoyi.auth.common.enums.OauthVerificationUse; +import com.ruoyi.auth.common.utils.RandomCodeUtil; +import com.ruoyi.common.constant.CacheConstants; +import com.ruoyi.common.core.domain.entity.SysUser; +import com.ruoyi.common.core.domain.model.LoginBody; +import com.ruoyi.common.core.domain.model.LoginUser; +import com.ruoyi.common.core.domain.model.RegisterBody; +import com.ruoyi.common.exception.ServiceException; +import com.ruoyi.common.utils.CacheUtils; +import com.ruoyi.common.utils.SecurityUtils; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.framework.web.service.TokenService; +import com.ruoyi.framework.web.service.UserDetailsServiceImpl; +import com.ruoyi.system.service.ISysUserService; +import com.ruoyi.tfa.phone.enums.DySmsTemplate; +import com.ruoyi.tfa.phone.service.DySmsService; +import com.ruoyi.tfa.phone.utils.DySmsUtil; + +/** + * 手机号认证Servcie + * + * @author zlh + * @date 2024-04-16 + */ +@Service +public class DySmsServiceImpl implements DySmsService { + + @Autowired + private ISysUserService userService; + @Autowired + private UserDetailsServiceImpl userDetailsServiceImpl; + @Autowired + private TokenService tokenService; + + private static final Logger log = LoggerFactory.getLogger(DySmsServiceImpl.class); + + @Override + public boolean sendCode(String phone, String code, OauthVerificationUse use) { + if (CacheUtils.hasKey(CacheConstants.PHONE_CODES, use.getValue() + phone)) { + throw new ServiceException("当前验证码未失效,请在1分钟后再发送短信"); + } + + try { + JSONObject templateParams = new JSONObject(); + templateParams.put("code", code); + DySmsUtil.sendSms(DySmsTemplate.Test_TEMPLATE_CODE, templateParams, phone); + CacheUtils.put(CacheConstants.PHONE_CODES, use.getValue() + phone, code, 1, TimeUnit.MINUTES); + log.info("发送手机验证码成功:{ phone: " + phone + " code:" + code + "}"); + return true; + } catch (Exception e) { + throw new ServiceException("发送手机验证码异常:" + phone); + } + } + + @Override + public boolean checkCode(String phone, String code, OauthVerificationUse use) { + if (StringUtils.isEmpty(code)) + return false; + String cachedCode = CacheUtils.get(CacheConstants.PHONE_CODES, use.getValue() + phone, String.class); // 从缓存中获取验证码 + CacheUtils.remove(CacheConstants.PHONE_CODES, use.getValue() + phone); + return code.equals(cachedCode); + } + + public void doLogin(LoginBody loginBody, boolean isRegister) { + SysUser sysUser = userService.selectUserByPhone(loginBody.getPhonenumber()); + if (sysUser == null && !isRegister) { + throw new ServiceException("该手机号未绑定用户"); + } else { + sendCode(loginBody.getPhonenumber(), RandomCodeUtil.numberCode(6), OauthVerificationUse.LOGIN); + } + } + + public String doLoginVerify(LoginBody loginBody, boolean isRegister) { + if (checkCode(loginBody.getPhonenumber(), loginBody.getCode(), OauthVerificationUse.LOGIN)) { + SysUser sysUser = userService.selectUserByPhone(loginBody.getPhonenumber()); + if (sysUser == null) { + if (isRegister) { + sysUser = new SysUser(); + sysUser.setUserName(loginBody.getPhonenumber()); + sysUser.setPassword(SecurityUtils.encryptPassword(RandomCodeUtil.code(16))); + sysUser.setPhonenumber(loginBody.getPhonenumber()); + } else { + throw new ServiceException("该手机号未绑定用户"); + } + } + LoginUser loginUser = (LoginUser) userDetailsServiceImpl.createLoginUser(sysUser); + return tokenService.createToken(loginUser); + } else { + throw new ServiceException("验证码错误"); + } + } + + public void doRegister(RegisterBody registerBody) { + SysUser sysUser = userService.selectUserByPhone(registerBody.getPhonenumber()); + if (sysUser != null) { + throw new ServiceException("该手机号已绑定用户"); + } else { + sendCode(registerBody.getPhonenumber(), RandomCodeUtil.numberCode(6), OauthVerificationUse.REGISTER); + } + } + + public void doRegisterVerify(RegisterBody registerBody) { + if (checkCode(registerBody.getPhonenumber(), registerBody.getCode(), OauthVerificationUse.REGISTER)) { + SysUser sysUser = new SysUser(); + sysUser.setUserName(registerBody.getPhonenumber()); + sysUser.setPassword(SecurityUtils.encryptPassword(registerBody.getPassword())); + sysUser.setPhonenumber(registerBody.getPhonenumber()); + } else { + throw new ServiceException("验证码错误"); + } + } + + public void doReset(String phone) { + SysUser sysUser = userService.selectUserByPhone(phone); + if (sysUser == null) { + throw new ServiceException("该手机号未绑定用户"); + } else { + sendCode(phone, RandomCodeUtil.numberCode(6), OauthVerificationUse.RESET); + } + } + + public int doResetVerify(RegisterBody registerBody) { + if (checkCode(registerBody.getPhonenumber(), registerBody.getCode(), OauthVerificationUse.RESET)) { + SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId()); + sysUser.setPhonenumber(registerBody.getPhonenumber()); + return userService.updateUser(sysUser); + } else { + throw new ServiceException("验证码错误"); + } + } + + public void doBind(LoginBody loginBody) { + SysUser sysUser = userService.selectUserByPhone(loginBody.getPhonenumber()); + if (sysUser != null) { + throw new ServiceException("该手机号已绑定用户"); + } + sysUser = userService.selectUserById(SecurityUtils.getUserId()); + if (!SecurityUtils.matchesPassword(loginBody.getPassword(), sysUser.getPassword())) { + throw new ServiceException("密码错误"); + } + sendCode(loginBody.getPhonenumber(), RandomCodeUtil.numberCode(6), OauthVerificationUse.BIND); + } + + public void doBindVerify(LoginBody loginBody) { + if (checkCode(loginBody.getPhonenumber(), loginBody.getCode(), OauthVerificationUse.BIND)) { + SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId()); + if (!SecurityUtils.matchesPassword(loginBody.getPassword(), sysUser.getPassword())) { + throw new ServiceException("密码错误"); + } + sysUser.setPhonenumber(loginBody.getPhonenumber()); + userService.updateUser(sysUser); + } else { + throw new ServiceException("验证码错误"); + } + } +} diff --git a/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/utils/DySmsUtil.java b/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/utils/DySmsUtil.java similarity index 71% rename from ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/utils/DySmsUtil.java rename to ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/utils/DySmsUtil.java index 79569c2..e4d1520 100644 --- a/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/utils/DySmsUtil.java +++ b/ruoyi-auth/ruoyi-tfa-phone/src/main/java/com/ruoyi/tfa/phone/utils/DySmsUtil.java @@ -1,9 +1,7 @@ -package com.ruoyi.oauth.phone.utils; +package com.ruoyi.tfa.phone.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import com.alibaba.fastjson2.JSONObject; import com.aliyun.dysmsapi20170525.Client; @@ -13,15 +11,12 @@ import com.aliyun.tea.TeaException; import com.aliyun.teaopenapi.models.Config; import com.aliyun.teautil.Common; import com.aliyun.teautil.models.RuntimeOptions; -import com.ruoyi.oauth.phone.constant.DySmsConstant; -import com.ruoyi.oauth.phone.enums.DySmsTemplate; +import com.ruoyi.common.utils.spring.SpringUtils; +import com.ruoyi.tfa.phone.constant.DySmsConfig; +import com.ruoyi.tfa.phone.enums.DySmsTemplate; -@Component public class DySmsUtil { - protected final Logger logger = LoggerFactory.getLogger(DySmsUtil.class); - - @Autowired - private DySmsConstant dySmsConfig; + protected final static Logger logger = LoggerFactory.getLogger(DySmsUtil.class); /** * 使用AK&SK初始化账号Client @@ -31,13 +26,13 @@ public class DySmsUtil { * @return Client * @throws Exception */ - private Client createClient() throws Exception { + private static Client createClient() throws Exception { + DySmsConfig dySmsConfig = SpringUtils.getBean(DySmsConfig.class); Config config = new Config() // 必填,您的 AccessKey ID .setAccessKeyId(dySmsConfig.getAccessKeyId()) // 必填,您的 AccessKey Secret .setAccessKeySecret(dySmsConfig.getAccessKeySecret()); - // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi config.endpoint = "dysmsapi.aliyuncs.com"; return new Client(config); } @@ -49,7 +44,7 @@ public class DySmsUtil { * @param dySmsTemplate * @throws Exception */ - private void validateParam(JSONObject templateParamJson, DySmsTemplate dySmsTemplate) { + private static void validateParam(JSONObject templateParamJson, DySmsTemplate dySmsTemplate) { String keys = dySmsTemplate.getKeys(); String[] keyArr = keys.split(","); for (String item : keyArr) { @@ -59,13 +54,8 @@ public class DySmsUtil { } } - public void sendSms(DySmsTemplate dySmsTemplate, JSONObject templateParamJson, String phone) + public static void sendSms(DySmsTemplate dySmsTemplate, JSONObject templateParamJson, String phone) throws Exception { - // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 - // ALIBABA_CLOUD_ACCESS_KEY_SECRET。 - // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey - // 的方式进行调用,仅供参考,建议使用更安全的 STS - // 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html validateParam(templateParamJson, dySmsTemplate); Client client = createClient(); SendSmsRequest sendSmsRequest = new SendSmsRequest() diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/constant/CacheConstants.java b/ruoyi-common/src/main/java/com/ruoyi/common/constant/CacheConstants.java index 1be9faf..dec0254 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/constant/CacheConstants.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/constant/CacheConstants.java @@ -5,8 +5,7 @@ package com.ruoyi.common.constant; * * @author ruoyi */ -public class CacheConstants -{ +public class CacheConstants { /** * 登录用户 redis key */ @@ -41,4 +40,14 @@ public class CacheConstants * 登录账户密码错误次数 redis key */ public static final String PWD_ERR_CNT_KEY = "pwd_err_cnt"; + + /** + * 手机号验证码 phone codes + */ + public static final String PHONE_CODES = "phone_codes"; + + /** + * 邮箱验证码 + */ + public static final String EMAIL_CODES = "email_codes"; } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginBody.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginBody.java index b7ee184..0f68ff4 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginBody.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginBody.java @@ -2,30 +2,49 @@ package com.ruoyi.common.core.domain.model; import com.ruoyi.common.core.domain.BaseEntity; +import io.swagger.v3.oas.annotations.media.Schema; + /** * 用户登录对象 * * @author ruoyi */ +@Schema(title = "用户登录对象") public class LoginBody extends BaseEntity { /** * 用户名 */ + @Schema(title = "用户名") private String username; /** * 用户密码 */ + @Schema(title = "用户密码") private String password; + /** + * 手机号码 + */ + @Schema(title = "手机号码") + private String phonenumber; + + /** + * 邮箱 + */ + @Schema(title = "邮箱") + private String email; + /** * 验证码 */ + @Schema(title = "验证码") private String code; /** * 唯一标识 */ + @Schema(title = "唯一标识") private String uuid; public String getUsername() { @@ -59,4 +78,20 @@ public class LoginBody extends BaseEntity { public void setUuid(String uuid) { this.uuid = uuid; } + + public String getPhonenumber() { + return phonenumber; + } + + public void setPhonenumber(String phonenumber) { + this.phonenumber = phonenumber; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/RegisterBody.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/RegisterBody.java index 868a1fc..1d32cc4 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/RegisterBody.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/RegisterBody.java @@ -1,10 +1,13 @@ package com.ruoyi.common.core.domain.model; +import io.swagger.v3.oas.annotations.media.Schema; + /** * 用户注册对象 * * @author ruoyi */ +@Schema(title = "用户注册对象") public class RegisterBody extends LoginBody { diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/enums/OauthVerificationUse.java b/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/enums/OauthVerificationUse.java deleted file mode 100644 index 0ac3126..0000000 --- a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/enums/OauthVerificationUse.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.ruoyi.oauth.common.enums; - -public enum OauthVerificationUse { - - LOGIN("登录", "login"), - REGISTER("注册", "register"), - DISABLE("禁用", "disable"), - RESET_PASSWORD("重置密码", "reset_password"), - RESET_PHONE("重置手机号", "reset_phone"), - Other("其他","other"); - - - - private String name; - private String value; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - private OauthVerificationUse(String name, String value) { - this.name = name; - this.value = value; - } -} diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/utils/HttpClientUtil.java b/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/utils/HttpClientUtil.java deleted file mode 100644 index 62b764e..0000000 --- a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/utils/HttpClientUtil.java +++ /dev/null @@ -1,298 +0,0 @@ -package com.ruoyi.oauth.common.utils; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.HttpClient; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.conn.ssl.DefaultHostnameVerifier; -import org.apache.http.conn.util.PublicSuffixMatcher; -import org.apache.http.conn.util.PublicSuffixMatcherLoader; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.util.EntityUtils; -import org.springframework.stereotype.Component; - - -@Component -public class HttpClientUtil { - - /** - * 默认参数设置 - * setConnectTimeout:设置连接超时时间,单位毫秒。 - * setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。 - * setSocketTimeout:请求获取数据的超时时间,单位毫秒。访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 暂时定义15分钟 - */ - private RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(600000).setConnectTimeout(600000).setConnectionRequestTimeout(600000).build(); - - /** - * 静态内部类---作用:单例产生类的实例 - * - * @author Administrator - */ - private static class LazyHolder { - private static final HttpClientUtil INSTANCE = new HttpClientUtil(); - - } - - private HttpClientUtil() { - } - - public static HttpClientUtil getInstance() { - return LazyHolder.INSTANCE; - } - - /** - * 发送 post请求 - * - * @param httpUrl 地址 - */ - public String sendHttpPost(String httpUrl) { - HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost - return sendHttpPost(httpPost); - } - - /** - * 发送 post请求 - * - * @param httpUrl 地址 - * @param params 参数(格式:key1=value1&key2=value2) - */ - public String sendHttpPost(String httpUrl, String params) { - HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost - try { - //设置参数 - StringEntity stringEntity = new StringEntity(params, "UTF-8"); - stringEntity.setContentType("application/x-www-form-urlencoded"); - httpPost.setEntity(stringEntity); - } catch (Exception e) { - e.printStackTrace(); - } - return sendHttpPost(httpPost); - } - - /** - * 发送 post请求 - * - * @param httpUrl 地址 - * @param maps 参数 - */ - public String sendHttpPost(String httpUrl, Map maps) { - HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost - // 创建参数队列 - List nameValuePairs = new ArrayList(); - for (String key : maps.keySet()) { - nameValuePairs.add(new BasicNameValuePair(key, maps.get(key))); - } - try { - httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); - } catch (Exception e) { - e.printStackTrace(); - } - return sendHttpPost(httpPost); - } - - /** - * 发送Post请求 - * - * @param httpPost - * @return - */ - private String sendHttpPost(HttpPost httpPost) { - CloseableHttpClient httpClient = null; - CloseableHttpResponse response = null; - HttpEntity entity = null; - String responseContent = null; - try { - // 创建默认的httpClient实例 - httpClient = HttpClients.createDefault(); - httpPost.setConfig(requestConfig); - // 执行请求 - long execStart = System.currentTimeMillis(); - response = httpClient.execute(httpPost); - long execEnd = System.currentTimeMillis(); - System.out.println("=================执行post请求耗时:" + (execEnd - execStart) + "ms"); - long getStart = System.currentTimeMillis(); - entity = response.getEntity(); - responseContent = EntityUtils.toString(entity, "UTF-8"); - long getEnd = System.currentTimeMillis(); - System.out.println("=================获取响应结果耗时:" + (getEnd - getStart) + "ms"); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - // 关闭连接,释放资源 - if (response != null) { - response.close(); - } - if (httpClient != null) { - httpClient.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - return responseContent; - } - - /** - * 发送 get请求 - * - * @param httpUrl - */ - public String sendHttpGet(String httpUrl) { - HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求 - return sendHttpGet(httpGet); - } - - /** - * 发送 get请求Https - * - * @param httpUrl - */ - public String sendHttpsGet(String httpUrl) { - HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求 - return sendHttpsGet(httpGet); - } - - /** - * 发送Get请求 - * - * @param httpGet - * @return - */ - private String sendHttpGet(HttpGet httpGet) { - CloseableHttpClient httpClient = null; - CloseableHttpResponse response = null; - HttpEntity entity = null; - String responseContent = null; - try { - // 创建默认的httpClient实例. - - - httpClient = HttpClients.createDefault(); - - httpGet.setConfig(requestConfig); - // 执行请求 - response = httpClient.execute(httpGet); - entity = response.getEntity(); - responseContent = EntityUtils.toString(entity, "UTF-8"); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - // 关闭连接,释放资源 - if (response != null) { - response.close(); - } - if (httpClient != null) { - httpClient.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - return responseContent; - } - - /** - * 发送Get请求Https - * - * @param httpGet - * @return - */ - private String sendHttpsGet(HttpGet httpGet) { - CloseableHttpClient httpClient = null; - CloseableHttpResponse response = null; - HttpEntity entity = null; - String responseContent = null; - try { - // 创建默认的httpClient实例. - PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString())); - DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher); - httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build(); - httpGet.setConfig(requestConfig); - // 执行请求 - response = httpClient.execute(httpGet); - entity = response.getEntity(); - responseContent = EntityUtils.toString(entity, "UTF-8"); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - // 关闭连接,释放资源 - if (response != null) { - response.close(); - } - if (httpClient != null) { - httpClient.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - return responseContent; - } - - /** - * 发送xml数据 - * - * @param url - * @param xmlData - * @return - * @throws ClientProtocolException - * @throws IOException - */ - public static HttpResponse sendXMLDataByPost(String url, String xmlData) - throws ClientProtocolException, IOException { - HttpClient httpClient = HttpClients.createDefault(); - HttpPost httppost = new HttpPost(url); - StringEntity entity = new StringEntity(xmlData); - httppost.setEntity(entity); - httppost.setHeader("Content-Type", "text/xml;charset=UTF-8"); - HttpResponse response = httpClient.execute(httppost); - return response; - } - - /** - * 获得响应HTTP实体内容 - * - * @param response - * @return - * @throws IOException - * @throws UnsupportedEncodingException - */ - public static String getHttpEntityContent(HttpResponse response) throws IOException, UnsupportedEncodingException { - HttpEntity entity = response.getEntity(); - if (entity != null) { - InputStream is = entity.getContent(); - BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); - String line = br.readLine(); - StringBuilder sb = new StringBuilder(); - while (line != null) { - sb.append(line + "\n"); - line = br.readLine(); - } - return sb.toString(); - } - return ""; - } - - -} \ No newline at end of file diff --git a/ruoyi-oauth/ruoyi-oauth-email/src/main/java/com/ruoyi/oauth/email/controller/MailController.java b/ruoyi-oauth/ruoyi-oauth-email/src/main/java/com/ruoyi/oauth/email/controller/MailController.java deleted file mode 100644 index cfeacee..0000000 --- a/ruoyi-oauth/ruoyi-oauth-email/src/main/java/com/ruoyi/oauth/email/controller/MailController.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.ruoyi.oauth.email.controller; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import com.ruoyi.common.annotation.Anonymous; -import com.ruoyi.common.core.controller.BaseController; -import com.ruoyi.oauth.email.service.IMailService; - -@RestController -@Anonymous -@RequestMapping("/mail") -public class MailController extends BaseController { - - @Autowired - IMailService serviceImpl; - -} diff --git a/ruoyi-oauth/ruoyi-oauth-email/src/main/java/com/ruoyi/oauth/email/service/IMailService.java b/ruoyi-oauth/ruoyi-oauth-email/src/main/java/com/ruoyi/oauth/email/service/IMailService.java deleted file mode 100644 index 3317426..0000000 --- a/ruoyi-oauth/ruoyi-oauth-email/src/main/java/com/ruoyi/oauth/email/service/IMailService.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.ruoyi.oauth.email.service; - -import com.ruoyi.oauth.common.service.OauthVerificationCodeService; - -public interface IMailService extends OauthVerificationCodeService { - public boolean sendMimeMail(String email, String code); -} diff --git a/ruoyi-oauth/ruoyi-oauth-email/src/main/java/com/ruoyi/oauth/email/service/impl/MailServiceImpl.java b/ruoyi-oauth/ruoyi-oauth-email/src/main/java/com/ruoyi/oauth/email/service/impl/MailServiceImpl.java deleted file mode 100644 index a830133..0000000 --- a/ruoyi-oauth/ruoyi-oauth-email/src/main/java/com/ruoyi/oauth/email/service/impl/MailServiceImpl.java +++ /dev/null @@ -1,153 +0,0 @@ -package com.ruoyi.oauth.email.service.impl; - -import java.util.Random; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.mail.SimpleMailMessage; -import org.springframework.mail.javamail.JavaMailSenderImpl; -import org.springframework.stereotype.Service; - -import com.ruoyi.common.core.domain.entity.SysUser; -import com.ruoyi.common.core.domain.model.LoginUser; -import com.ruoyi.common.exception.ServiceException; -import com.ruoyi.common.utils.CacheUtils; -import com.ruoyi.framework.web.service.TokenService; -import com.ruoyi.framework.web.service.UserDetailsServiceImpl; -import com.ruoyi.oauth.common.enums.OauthVerificationUse; -import com.ruoyi.oauth.email.service.IMailService; -import com.ruoyi.system.service.ISysUserService; - -@Service -public class MailServiceImpl implements IMailService { - - public String CACHE_NAME = "mail_codes"; - - @Autowired - private JavaMailSenderImpl mailSender; - @Autowired - private ISysUserService userService; - @Autowired - private TokenService tokenService; - @Autowired - private UserDetailsServiceImpl userDetailsServiceImpl; - private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class); - // application.properties配置的值 - @Value("${spring.mail.username}") - private String from; - - public boolean beforeSendCode(String email, OauthVerificationUse use) throws Exception {// 1.查验手机号是否存在,分辨登录和删除用户以及注册用户 - boolean haveEmailFlag = userService.selectUserByEmail(email) != null; - if ((use.equals(OauthVerificationUse.LOGIN) || use.equals(OauthVerificationUse.DISABLE) - || use.equals(OauthVerificationUse.RESET_PASSWORD)) && !haveEmailFlag) { - throw new ServiceException("该邮箱未绑定用户"); - } else if ((use.equals(OauthVerificationUse.REGISTER) || use.equals(OauthVerificationUse.RESET_PHONE)) - && haveEmailFlag) { - throw new ServiceException("该邮箱已绑定用户"); - } - return true; - } - - /** - * 给前端输入的邮箱,发送验证码 - * - * @param email - * @param session - * @return - */ - @Override - public boolean sendMimeMail(String email, String code) { - try { - SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); - simpleMailMessage.setSubject("验证码邮件"); // 主题 - simpleMailMessage.setText("您收到的验证码是:" + code); // 内容 - simpleMailMessage.setFrom(from); // 发件人 - simpleMailMessage.setTo(email); // 收件人 - mailSender.send(simpleMailMessage); // 发送 - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * 随机生成6位数的验证码 - * - * @return String code - */ - public static String generateRandomString(int n) { - String characters = "0123456789"; // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz - StringBuilder result = new StringBuilder(); - Random random = new Random(); - - for (int i = 0; i < n; i++) { - int index = random.nextInt(characters.length()); - result.append(characters.charAt(index)); - } - - return result.toString(); - } - - @Override - public boolean sendCode(String email, String code, OauthVerificationUse use) throws Exception { - // 限制短信一分钟只能发送一次短信 - if (CacheUtils.hasKey(CACHE_NAME, email + use.getValue())) { - throw new ServiceException("请在1分钟后再发送短信"); - } - - try { - sendMimeMail(email, code); - CacheUtils.put(CACHE_NAME, email + use.getValue(), code, 1, TimeUnit.MINUTES); - log.info("发送邮箱验证码成功:{ phone: " + email + " code:" + code + "}"); - return true; - } catch (Exception e) { - log.error("发送邮箱验证码异常:" + email); - throw e; - } - } - - @Override - public String checkCode(String email, String code, OauthVerificationUse use) throws Exception { - String cachedCode = CacheUtils.get(use.getValue(), use.getValue() + email, String.class); // 从缓存中获取验证码 - CacheUtils.remove(use.getValue(), use.getValue() + email); - boolean haveEmailFlag = userService.selectUserByEmail(email) != null; - if (use.equals(OauthVerificationUse.LOGIN) && haveEmailFlag) {// 登录校验 - if (code.equals(cachedCode)) { - SysUser sysUser = userService.selectUserByEmail(email); - LoginUser loginUser = (LoginUser) userDetailsServiceImpl.createLoginUser(sysUser); - return tokenService.createToken(loginUser); - } else { - throw new ServiceException("验证码错误"); - } - } else if (use.equals(OauthVerificationUse.REGISTER) && !haveEmailFlag) {// 注册校验 - if (code.equals(cachedCode)) { - return Boolean.toString(true); - } else { - throw new ServiceException("验证码错误"); - } - } else if (use.equals(OauthVerificationUse.DISABLE) && haveEmailFlag) {// 注销校验 - if (code.equals(cachedCode)) { - return Boolean.toString(true); - } else { - throw new ServiceException("验证码错误"); - } - } else if (use.equals(OauthVerificationUse.RESET_PASSWORD) && haveEmailFlag) {// 重置密码校验 - if (code.equals(cachedCode)) { - return Boolean.toString(true); - } else { - throw new ServiceException("验证码错误"); - } - } else if (use.equals(OauthVerificationUse.RESET_PHONE) && !haveEmailFlag) {// 重置账号校验 - if (code.equals(cachedCode)) { - return Boolean.toString(true); - } else { - throw new ServiceException("验证码错误"); - } - } - return Boolean.toString(false); - } -} diff --git a/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/controller/DySmsController.java b/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/controller/DySmsController.java deleted file mode 100644 index 37da61c..0000000 --- a/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/controller/DySmsController.java +++ /dev/null @@ -1,153 +0,0 @@ -package com.ruoyi.oauth.phone.controller; - -import java.util.Random; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import com.ruoyi.common.annotation.Anonymous; -import com.ruoyi.common.core.controller.BaseController; -import com.ruoyi.common.core.domain.AjaxResult; -import com.ruoyi.common.core.domain.entity.SysUser; -import com.ruoyi.common.core.domain.model.LoginBody; -import com.ruoyi.common.utils.SecurityUtils; -import com.ruoyi.oauth.common.enums.OauthVerificationUse; -import com.ruoyi.oauth.phone.service.DySmsService; -import com.ruoyi.system.service.ISysUserService; - -/** - * 手机号认证Controller - * - * @author zlh - * @date 2024-04-16 - */ -@RestController -@RequestMapping("/oauth/phone") -public class DySmsController extends BaseController { - @Autowired - public DySmsService dySmsService; - - @Autowired - private ISysUserService userService; - - @Anonymous - @PostMapping("/sendcode/{mode}") // 发送验证码 - public AjaxResult sendcode(@RequestBody LoginBody loginBody, @PathVariable(value = "mode") String mode) - throws Exception { - try { - OauthVerificationUse oauthVerificationUse = null; - switch (mode) { - case "login": - oauthVerificationUse = OauthVerificationUse.LOGIN; - break; - case "register": - oauthVerificationUse = OauthVerificationUse.REGISTER; - break; - case "disable": - oauthVerificationUse = OauthVerificationUse.DISABLE; - break; - case "resetpassword": - oauthVerificationUse = OauthVerificationUse.RESET_PASSWORD; - break; - case "resetphone": - oauthVerificationUse = OauthVerificationUse.RESET_PHONE; - break; - default: - oauthVerificationUse = OauthVerificationUse.Other; - } - String code = generateRandomString(6); - dySmsService.sendCode(loginBody.getUsername(), code, oauthVerificationUse); - return AjaxResult.success("发送验证码成功"); - } catch (Exception e) { - return AjaxResult.error("发送验证码失败,原因: " + e.getMessage()); - } - } - - public static String generateRandomString(int n) { - String characters = "0123456789"; // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz - StringBuilder result = new StringBuilder(); - Random random = new Random(); - - for (int i = 0; i < n; i++) { - int index = random.nextInt(characters.length()); - result.append(characters.charAt(index)); - } - - return result.toString(); - } - - @Anonymous - @PostMapping("/login") // 登录 - public AjaxResult postMethodName(@RequestBody LoginBody loginBody) throws Exception { - if (dySmsService.checkCode(loginBody.getUsername(), loginBody.getCode(), - OauthVerificationUse.LOGIN) == "false") { - return AjaxResult.error("登录失败"); - } else { - return AjaxResult.success("登录成功"); - } - } - - @Anonymous - @PostMapping("/register") // 注册 - public AjaxResult postenroll(@RequestBody LoginBody loginBody) throws Exception { - try { - dySmsService.checkCode(loginBody.getUsername(), loginBody.getCode(), OauthVerificationUse.REGISTER); - // 验证通过,执行注册逻辑 - SysUser user = new SysUser(); - user.setUserName(loginBody.getUsername()); - user.setNickName(loginBody.getUsername()); - user.setPassword(SecurityUtils.encryptPassword(loginBody.getPassword())); - user.setPhonenumber(loginBody.getUsername()); - userService.insertUser(user); - return AjaxResult.success("注册成功"); - } catch (Exception e) { - // return "注册失败,原因: " + e.getMessage(); - return AjaxResult.error("注册失败"); - } - } - - @PostMapping("/disable") // 注销 - public AjaxResult postlogout(@RequestBody LoginBody loginBody) throws Exception { - try { - dySmsService.checkCode(getUsername(), loginBody.getCode(), OauthVerificationUse.DISABLE); - // 验证通过,执行注销逻辑 - SysUser sysUser = userService.selectUserByPhone(loginBody.getUsername()); - userService.deleteUserById(sysUser.getUserId()); - return AjaxResult.success("注销成功"); - } catch (Exception e) { - return AjaxResult.error("注销失败,原因: " + e.getMessage()); - } - } - @Anonymous - @PostMapping("/resetpassword") // 重置密码 - public AjaxResult postpassword(@RequestBody LoginBody loginBody) throws Exception { - try { - dySmsService.checkCode(loginBody.getUsername(), loginBody.getCode(), OauthVerificationUse.RESET_PASSWORD); - // 验证通过,执行重置密码逻辑 - SysUser sysUser = userService.selectUserByPhone(loginBody.getUsername()); - sysUser.setPassword(SecurityUtils.encryptPassword(loginBody.getPassword())); - userService.updateUser(sysUser); - return AjaxResult.success("重置密码成功"); - } catch (Exception e) { - return AjaxResult.error("重置密码失败,原因: " + e.getMessage()); - } - } - - @PostMapping("/resetphone") // 重置手机号 - public AjaxResult postphone(@RequestBody LoginBody loginBody) throws Exception { - try { - dySmsService.checkCode(loginBody.getUsername(), loginBody.getCode(), OauthVerificationUse.RESET_PHONE); - // 验证通过,执行重置密码逻辑 - SysUser sysUser = userService.selectUserByUserName(getUsername()); - sysUser.setPhonenumber(loginBody.getUsername()); - userService.updateUser(sysUser); - return AjaxResult.success("重置手机号成功"); - } catch (Exception e) { - return AjaxResult.error("重置手机号失败,原因: " + e.getMessage()); - } - } -} diff --git a/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/service/DySmsService.java b/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/service/DySmsService.java deleted file mode 100644 index 065b675..0000000 --- a/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/service/DySmsService.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.ruoyi.oauth.phone.service; - -import com.ruoyi.oauth.common.service.OauthVerificationCodeService; - -/** - * 手机号认证Servcie - * - * @author zlh - * @date 2024-04-16 - */ -public interface DySmsService extends OauthVerificationCodeService { - public String doLogin(String phone); - -} diff --git a/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/service/Impl/DySmsServiceImpl.java b/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/service/Impl/DySmsServiceImpl.java deleted file mode 100644 index ff32b7c..0000000 --- a/ruoyi-oauth/ruoyi-oauth-phone/src/main/java/com/ruoyi/oauth/phone/service/Impl/DySmsServiceImpl.java +++ /dev/null @@ -1,183 +0,0 @@ -package com.ruoyi.oauth.phone.service.Impl; - -import java.util.Random; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.alibaba.fastjson2.JSONObject; -import com.ruoyi.common.core.domain.entity.SysUser; -import com.ruoyi.common.core.domain.model.LoginUser; -import com.ruoyi.common.exception.ServiceException; -import com.ruoyi.common.utils.CacheUtils; -import com.ruoyi.framework.web.service.TokenService; -import com.ruoyi.framework.web.service.UserDetailsServiceImpl; -import com.ruoyi.oauth.common.enums.OauthVerificationUse; -import com.ruoyi.oauth.phone.enums.DySmsTemplate; -import com.ruoyi.oauth.phone.service.DySmsService; -import com.ruoyi.oauth.phone.utils.DySmsUtil; -import com.ruoyi.system.service.ISysUserService; - -/** - * 手机号认证Servcie - * - * @author zlh - * @date 2024-04-16 - */ -@Service -public class DySmsServiceImpl implements DySmsService { - - @Autowired - private DySmsUtil dySmsUtil; - @Autowired - private ISysUserService userService; - @Autowired - private UserDetailsServiceImpl userDetailsServiceImpl; - @Autowired - private TokenService tokenService; - - private static final String CACHE_NAME = "phone_codes"; - - private static final Logger log = LoggerFactory.getLogger(DySmsServiceImpl.class); - - public boolean beforeSendCode(String phone, OauthVerificationUse use) throws Exception {// 1.查验手机号是否存在,分辨登录和删除用户以及注册用户 - boolean havePhoneFlag = userService.selectUserByPhone(phone) != null; - if ((use.equals(OauthVerificationUse.LOGIN) || use.equals(OauthVerificationUse.DISABLE) - || use.equals(OauthVerificationUse.RESET_PASSWORD)) && !havePhoneFlag) { - throw new ServiceException("该手机号未绑定用户"); - } else if ((use.equals(OauthVerificationUse.REGISTER) || use.equals(OauthVerificationUse.RESET_PHONE)) - && havePhoneFlag) { - throw new ServiceException("该手机号已绑定用户"); - } - return true; - } - - @Override - public boolean sendCode(String phone, String code, OauthVerificationUse use) throws Exception {// 1.查验手机号是否存在,分辨登录和删除用户以及注册用户 - // 限制短信一分钟只能发送一次短信 - if (CacheUtils.hasKey(CACHE_NAME, phone + use.getValue())) { - throw new ServiceException("请在1分钟后再发送短信"); - } - - try { - JSONObject templateParams = new JSONObject(); - templateParams.put("code", code); - dySmsUtil.sendSms(DySmsTemplate.Test_TEMPLATE_CODE, templateParams, phone); - CacheUtils.put(CACHE_NAME, phone + use.getValue(), code, 1, TimeUnit.MINUTES); - log.info("发送手机验证码成功:{ phone: " + phone + " code:" + code + "}"); - return true; - } catch (Exception e) { - log.error("发送手机验证码异常:" + phone); - throw e; - } - } - - @Override - public String checkCode(String phone, String code, OauthVerificationUse use) throws Exception { - String cachedCode = CacheUtils.get(use.getValue(), use.getValue() + phone, String.class); // 从缓存中获取验证码 - CacheUtils.remove(use.getValue(), use.getValue() + phone); - boolean havePhoneFlag = userService.selectUserByPhone(phone) != null; - if (use.equals(OauthVerificationUse.LOGIN) && havePhoneFlag) {// 登录校验 - if (code.equals(cachedCode)) { - SysUser sysUser = userService.selectUserByPhone(phone); - LoginUser loginUser = (LoginUser) userDetailsServiceImpl.createLoginUser(sysUser); - return tokenService.createToken(loginUser); - // return true; - } else { - throw new ServiceException("验证码错误"); - } - } else if (use.equals(OauthVerificationUse.REGISTER) && !havePhoneFlag) {// 注册校验 - if (code.equals(cachedCode)) { - return Boolean.toString(true); - } else { - throw new ServiceException("验证码错误"); - } - } else if (use.equals(OauthVerificationUse.DISABLE) && havePhoneFlag) {// 注销校验 - if (code.equals(cachedCode)) { - return Boolean.toString(true); - } else { - throw new ServiceException("验证码错误"); - } - } else if (use.equals(OauthVerificationUse.RESET_PASSWORD) && havePhoneFlag) {// 重置密码校验 - if (code.equals(cachedCode)) { - return Boolean.toString(true); - } else { - throw new ServiceException("验证码错误"); - } - } else if (use.equals(OauthVerificationUse.RESET_PHONE) && !havePhoneFlag) {// 重置账号校验 - if (code.equals(cachedCode)) { - return Boolean.toString(true); - } else { - throw new ServiceException("验证码错误"); - } - } - return Boolean.toString(false); - } - - public static String generateRandomString(int n) { - String characters = "0123456789"; // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz - StringBuilder result = new StringBuilder(); - Random random = new Random(); - - for (int i = 0; i < n; i++) { - int index = random.nextInt(characters.length()); - result.append(characters.charAt(index)); - } - - return result.toString(); - } - - @Override - public String doLogin(String phone) { - String verify = CacheUtils.get(CACHE_NAME, phone + OauthVerificationUse.LOGIN, String.class); - if (verify != null) { - throw new ServiceException("该手机号验证码未过期"); - } - String code = generateRandomString(6); - SysUser sysUser = userService.selectUserByPhone(phone); - if (sysUser == null) { - throw new ServiceException("该手机号未绑定用户"); - } - try { - JSONObject templateParams = new JSONObject(); - templateParams.put("code", code); - dySmsUtil.sendSms(DySmsTemplate.Test_TEMPLATE_CODE, templateParams, phone); - CacheUtils.put(CACHE_NAME, phone + OauthVerificationUse.LOGIN, code, 1, - TimeUnit.MINUTES); - } catch (Exception e) { - e.printStackTrace(); - } - return code; - } - - public String doLoginVerify(String phone, String code) { - String verify = CacheUtils.get(CACHE_NAME, phone + OauthVerificationUse.LOGIN, String.class); - if (code.equals(verify)) { - SysUser sysUser = userService.selectUserByPhone(phone); - LoginUser loginUser = (LoginUser) userDetailsServiceImpl.createLoginUser(sysUser); - return tokenService.createToken(loginUser); - } else { - throw new ServiceException("验证码错误"); - } - } - - public String doRegister(String phone) { - String code = generateRandomString(6); - CacheUtils.put(CACHE_NAME, phone + OauthVerificationUse.REGISTER, code, 1, TimeUnit.MINUTES); - String verify = CacheUtils.get("phone_codes_register", phone, String.class); - if (verify != null) { - throw new ServiceException("该手机号验证码未过期"); - } else { - try { - dySmsUtil.sendSms(null, null, phone); - CacheUtils.put(CACHE_NAME, phone + OauthVerificationUse.REGISTER, code, 1, TimeUnit.MINUTES); - } catch (Exception e) { - e.printStackTrace(); - } - } - return code; - } -}