From 5045483e01fed3abc503892e9ab72b53c4ec74a9 Mon Sep 17 00:00:00 2001 From: D <3066417822@qq.com> Date: Thu, 18 Jan 2024 02:44:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=96=B9=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E8=A7=84=E8=8C=83=EF=BC=88=E5=9F=BA=E7=A1=80=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E6=90=AD=E5=BB=BA=EF=BC=89=E5=AE=8C=E6=88=90=EF=BC=8C=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E5=B0=8F=E7=A8=8B=E5=BA=8F=E7=99=BB=E5=BD=95=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 1 + .../src/main/resources/application-oauth.yml | 6 +- .../src/main/resources/vm/java/domain.java.vm | 2 +- ruoyi-oauth/pom.xml | 14 + ruoyi-oauth/ruoyi-oauth-common/pom.xml | 14 + .../controller/OauthUserController.java | 113 +++++ .../ruoyi/oauth/common/domain/OauthUser.java | 407 ++++++++++++++++++ .../oauth/common/mapper/OauthUserMapper.java | 75 ++++ .../common/service/IOauthUserService.java | 65 +++ .../service/impl/OauthUserServiceImpl.java | 104 +++++ .../mapper/common/OauthUserMapper.xml | 153 +++++++ .../{WxH5Constant.java => WxPubConstant.java} | 10 +- .../wx/controller/WxLoginController.java | 54 ++- .../wx/service/Impl/WxLoginServiceImpl.java | 85 +++- .../oauth/wx/service/WxLoginService.java | 9 +- sql/oauth.sql | 25 +- 16 files changed, 1109 insertions(+), 28 deletions(-) create mode 100644 ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/controller/OauthUserController.java create mode 100644 ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/domain/OauthUser.java create mode 100644 ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/mapper/OauthUserMapper.java create mode 100644 ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/IOauthUserService.java create mode 100644 ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/impl/OauthUserServiceImpl.java create mode 100644 ruoyi-oauth/ruoyi-oauth-common/src/main/resources/mapper/common/OauthUserMapper.xml rename ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/{WxH5Constant.java => WxPubConstant.java} (84%) diff --git a/.vscode/settings.json b/.vscode/settings.json index 11da8a3..5e809b3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,6 @@ "*.yml": "yaml" }, "java.compile.nullAnalysis.mode": "disabled", + "maven.view": "hierarchical", // "java.compile.nullAnalysis.mode": "automatic" } \ No newline at end of file diff --git a/ruoyi-admin/src/main/resources/application-oauth.yml b/ruoyi-admin/src/main/resources/application-oauth.yml index 638e9c0..87fe96d 100644 --- a/ruoyi-admin/src/main/resources/application-oauth.yml +++ b/ruoyi-admin/src/main/resources/application-oauth.yml @@ -2,10 +2,10 @@ wx: miniapp: open: true - appId: miniAppid - appSecret: miniAppSercret + appId: wx2dd450adc727b59e + appSecret: 471e9abe00b93460d6a8654a30b4c85f url: https://api.weixin.qq.com/sns/jscode2session - h5: + pub: open: true appId: wx98501e665b0f0596 appSecret: wx98501e665b0f0596 diff --git a/ruoyi-generator/src/main/resources/vm/java/domain.java.vm b/ruoyi-generator/src/main/resources/vm/java/domain.java.vm index 5000c4b..691d07f 100644 --- a/ruoyi-generator/src/main/resources/vm/java/domain.java.vm +++ b/ruoyi-generator/src/main/resources/vm/java/domain.java.vm @@ -3,7 +3,7 @@ package ${packageName}.domain; #foreach ($import in $importList) import ${import}; #end -import io.swagger.annotations.ApiModelProperty; +import io.swagger.v3.oas.annotations.media.Schema; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import com.ruoyi.common.annotation.Excel; diff --git a/ruoyi-oauth/pom.xml b/ruoyi-oauth/pom.xml index ea72605..a6c852e 100644 --- a/ruoyi-oauth/pom.xml +++ b/ruoyi-oauth/pom.xml @@ -28,6 +28,20 @@ ${ruoyi.version} + + + com.ruoyi + ruoyi-framework + ${ruoyi.version} + + + + + com.ruoyi + ruoyi-system + ${ruoyi.version} + + com.ruoyi diff --git a/ruoyi-oauth/ruoyi-oauth-common/pom.xml b/ruoyi-oauth/ruoyi-oauth-common/pom.xml index 513cd94..65dafa7 100644 --- a/ruoyi-oauth/ruoyi-oauth-common/pom.xml +++ b/ruoyi-oauth/ruoyi-oauth-common/pom.xml @@ -23,6 +23,20 @@ ruoyi-common + + + com.ruoyi + ruoyi-framework + ${ruoyi.version} + + + + + com.ruoyi + ruoyi-system + ${ruoyi.version} + + org.apache.httpcomponents diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/controller/OauthUserController.java b/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/controller/OauthUserController.java new file mode 100644 index 0000000..b20305d --- /dev/null +++ b/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/controller/OauthUserController.java @@ -0,0 +1,113 @@ +package com.ruoyi.oauth.common.controller; + +import java.util.List; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +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.Log; +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.enums.BusinessType; +import com.ruoyi.oauth.common.domain.OauthUser; +import com.ruoyi.oauth.common.service.IOauthUserService; +import com.ruoyi.common.utils.poi.ExcelUtil; +import com.ruoyi.common.core.page.TableDataInfo; +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.Operation; + +/** + * 第三方认证Controller + * + * @author ruoyi + * @date 2024-01-18 + */ +@RestController +@RequestMapping("/system/oauth") +@Tag(name = "【第三方认证】管理") +public class OauthUserController extends BaseController +{ + @Autowired + private IOauthUserService oauthUserService; + + /** + * 查询第三方认证列表 + */ + @Operation(summary = "查询第三方认证列表") + @PreAuthorize("@ss.hasPermi('system:oauth:list')") + @GetMapping("/list") + public TableDataInfo list(OauthUser oauthUser) + { + startPage(); + List list = oauthUserService.selectOauthUserList(oauthUser); + return getDataTable(list); + } + + /** + * 导出第三方认证列表 + */ + @Operation(summary = "导出第三方认证列表") + @PreAuthorize("@ss.hasPermi('system:oauth:export')") + @Log(title = "第三方认证", businessType = BusinessType.EXPORT) + @PostMapping("/export") + public void export(HttpServletResponse response, OauthUser oauthUser) + { + List list = oauthUserService.selectOauthUserList(oauthUser); + ExcelUtil util = new ExcelUtil(OauthUser.class); + util.exportExcel(response, list, "第三方认证数据"); + } + + /** + * 获取第三方认证详细信息 + */ + @Operation(summary = "获取第三方认证详细信息") + @PreAuthorize("@ss.hasPermi('system:oauth:query')") + @GetMapping(value = "/{id}") + public AjaxResult getInfo(@PathVariable("id") Long id) + { + return success(oauthUserService.selectOauthUserById(id)); + } + + /** + * 新增第三方认证 + */ + @Operation(summary = "新增第三方认证") + @PreAuthorize("@ss.hasPermi('system:oauth:add')") + @Log(title = "第三方认证", businessType = BusinessType.INSERT) + @PostMapping + public AjaxResult add(@RequestBody OauthUser oauthUser) + { + return toAjax(oauthUserService.insertOauthUser(oauthUser)); + } + + /** + * 修改第三方认证 + */ + @Operation(summary = "修改第三方认证") + @PreAuthorize("@ss.hasPermi('system:oauth:edit')") + @Log(title = "第三方认证", businessType = BusinessType.UPDATE) + @PutMapping + public AjaxResult edit(@RequestBody OauthUser oauthUser) + { + return toAjax(oauthUserService.updateOauthUser(oauthUser)); + } + + /** + * 删除第三方认证 + */ + @Operation(summary = "删除第三方认证") + @PreAuthorize("@ss.hasPermi('system:oauth:remove')") + @Log(title = "第三方认证", businessType = BusinessType.DELETE) + @DeleteMapping("/{ids}") + public AjaxResult remove(@PathVariable( name = "ids" ) Long[] ids) + { + return toAjax(oauthUserService.deleteOauthUserByIds(ids)); + } +} diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/domain/OauthUser.java b/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/domain/OauthUser.java new file mode 100644 index 0000000..b59d121 --- /dev/null +++ b/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/domain/OauthUser.java @@ -0,0 +1,407 @@ +package com.ruoyi.oauth.common.domain; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +import com.ruoyi.common.annotation.Excel; +import com.ruoyi.common.core.domain.BaseEntity; + +import io.swagger.v3.oas.annotations.media.Schema; + +/** + * 第三方认证对象 oauth_user + * + * @author ruoyi + * @date 2024-01-18 + */ +@Schema(description = "第三方认证对象") +public class OauthUser extends BaseEntity +{ + private static final long serialVersionUID = 1L; + + + /** 主键 */ + @Schema(defaultValue = "主键") + private Long id; + + + + + /** 第三方系统的唯一ID,详细解释请参考:名词解释 */ + @Schema(defaultValue = "第三方系统的唯一ID,详细解释请参考:名词解释") + @Excel(name = "第三方系统的唯一ID,详细解释请参考:名词解释") + private String uuid; + + + + + /** 用户ID */ + @Schema(defaultValue = "用户ID") + @Excel(name = "用户ID") + private Long userId; + + + + + /** 第三方用户来源,可选值:GITHUB、GITEE、QQ,更多请参考:AuthDefaultSource.java(opens new window) */ + @Schema(defaultValue = "第三方用户来源,可选值:GITHUB、GITEE、QQ,更多请参考:AuthDefaultSource.java(opens new window)") + @Excel(name = "第三方用户来源,可选值:GITHUB、GITEE、QQ,更多请参考:AuthDefaultSource.java(opens new window)") + private String source; + + + + + /** 用户的授权令牌 */ + @Schema(defaultValue = "用户的授权令牌") + @Excel(name = "用户的授权令牌") + private String accessToken; + + + + + /** 第三方用户的授权令牌的有效期,部分平台可能没有 */ + @Schema(defaultValue = "第三方用户的授权令牌的有效期,部分平台可能没有") + @Excel(name = "第三方用户的授权令牌的有效期,部分平台可能没有") + private Long expireIn; + + + + + /** 刷新令牌,部分平台可能没有 */ + @Schema(defaultValue = "刷新令牌,部分平台可能没有") + @Excel(name = "刷新令牌,部分平台可能没有") + private String refreshToken; + + + + + /** 第三方用户的 open id,部分平台可能没有 */ + @Schema(defaultValue = "第三方用户的 open id,部分平台可能没有") + @Excel(name = "第三方用户的 open id,部分平台可能没有") + private String openId; + + + + + /** 第三方用户的 ID,部分平台可能没有 */ + @Schema(defaultValue = "第三方用户的 ID,部分平台可能没有") + @Excel(name = "第三方用户的 ID,部分平台可能没有") + private String uid; + + + + + /** 个别平台的授权信息,部分平台可能没有 */ + @Schema(defaultValue = "个别平台的授权信息,部分平台可能没有") + @Excel(name = "个别平台的授权信息,部分平台可能没有") + private String accessCode; + + + + + /** 第三方用户的 union id,部分平台可能没有 */ + @Schema(defaultValue = "第三方用户的 union id,部分平台可能没有") + @Excel(name = "第三方用户的 union id,部分平台可能没有") + private String unionId; + + + + + /** 第三方用户授予的权限,部分平台可能没有 */ + @Schema(defaultValue = "第三方用户授予的权限,部分平台可能没有") + @Excel(name = "第三方用户授予的权限,部分平台可能没有") + private String scope; + + + + + /** 个别平台的授权信息,部分平台可能没有 */ + @Schema(defaultValue = "个别平台的授权信息,部分平台可能没有") + @Excel(name = "个别平台的授权信息,部分平台可能没有") + private String tokenType; + + + + + /** id token,部分平台可能没有 */ + @Schema(defaultValue = "id token,部分平台可能没有") + @Excel(name = "id token,部分平台可能没有") + private String idToken; + + + + + /** 小米平台用户的附带属性,部分平台可能没有 */ + @Schema(defaultValue = "小米平台用户的附带属性,部分平台可能没有") + @Excel(name = "小米平台用户的附带属性,部分平台可能没有") + private String macAlgorithm; + + + + + /** 小米平台用户的附带属性,部分平台可能没有 */ + @Schema(defaultValue = "小米平台用户的附带属性,部分平台可能没有") + @Excel(name = "小米平台用户的附带属性,部分平台可能没有") + private String macKey; + + + + + /** 用户的授权code,部分平台可能没有 */ + @Schema(defaultValue = "用户的授权code,部分平台可能没有") + @Excel(name = "用户的授权code,部分平台可能没有") + private String code; + + + + + /** Twitter平台用户的附带属性,部分平台可能没有 */ + @Schema(defaultValue = "Twitter平台用户的附带属性,部分平台可能没有") + @Excel(name = "Twitter平台用户的附带属性,部分平台可能没有") + private String oauthToken; + + + + + /** Twitter平台用户的附带属性,部分平台可能没有 */ + @Schema(defaultValue = "Twitter平台用户的附带属性,部分平台可能没有") + @Excel(name = "Twitter平台用户的附带属性,部分平台可能没有") + private String oauthTokenSecret; + + + + public void setId(Long id) + { + this.id = id; + } + + public Long getId() + { + return id; + } + + + public void setUuid(String uuid) + { + this.uuid = uuid; + } + + public String getUuid() + { + return uuid; + } + + + public void setUserId(Long userId) + { + this.userId = userId; + } + + public Long getUserId() + { + return userId; + } + + + public void setSource(String source) + { + this.source = source; + } + + public String getSource() + { + return source; + } + + + public void setAccessToken(String accessToken) + { + this.accessToken = accessToken; + } + + public String getAccessToken() + { + return accessToken; + } + + + public void setExpireIn(Long expireIn) + { + this.expireIn = expireIn; + } + + public Long getExpireIn() + { + return expireIn; + } + + + public void setRefreshToken(String refreshToken) + { + this.refreshToken = refreshToken; + } + + public String getRefreshToken() + { + return refreshToken; + } + + + public void setOpenId(String openId) + { + this.openId = openId; + } + + public String getOpenId() + { + return openId; + } + + + public void setUid(String uid) + { + this.uid = uid; + } + + public String getUid() + { + return uid; + } + + + public void setAccessCode(String accessCode) + { + this.accessCode = accessCode; + } + + public String getAccessCode() + { + return accessCode; + } + + + public void setUnionId(String unionId) + { + this.unionId = unionId; + } + + public String getUnionId() + { + return unionId; + } + + + public void setScope(String scope) + { + this.scope = scope; + } + + public String getScope() + { + return scope; + } + + + public void setTokenType(String tokenType) + { + this.tokenType = tokenType; + } + + public String getTokenType() + { + return tokenType; + } + + + public void setIdToken(String idToken) + { + this.idToken = idToken; + } + + public String getIdToken() + { + return idToken; + } + + + public void setMacAlgorithm(String macAlgorithm) + { + this.macAlgorithm = macAlgorithm; + } + + public String getMacAlgorithm() + { + return macAlgorithm; + } + + + public void setMacKey(String macKey) + { + this.macKey = macKey; + } + + public String getMacKey() + { + return macKey; + } + + + public void setCode(String code) + { + this.code = code; + } + + public String getCode() + { + return code; + } + + + public void setOauthToken(String oauthToken) + { + this.oauthToken = oauthToken; + } + + public String getOauthToken() + { + return oauthToken; + } + + + public void setOauthTokenSecret(String oauthTokenSecret) + { + this.oauthTokenSecret = oauthTokenSecret; + } + + public String getOauthTokenSecret() + { + return oauthTokenSecret; + } + + + + @Override + public String toString() { + return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) + .append("id", getId()) + .append("uuid", getUuid()) + .append("userId", getUserId()) + .append("source", getSource()) + .append("accessToken", getAccessToken()) + .append("expireIn", getExpireIn()) + .append("refreshToken", getRefreshToken()) + .append("openId", getOpenId()) + .append("uid", getUid()) + .append("accessCode", getAccessCode()) + .append("unionId", getUnionId()) + .append("scope", getScope()) + .append("tokenType", getTokenType()) + .append("idToken", getIdToken()) + .append("macAlgorithm", getMacAlgorithm()) + .append("macKey", getMacKey()) + .append("code", getCode()) + .append("oauthToken", getOauthToken()) + .append("oauthTokenSecret", getOauthTokenSecret()) + .toString(); + } +} diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/mapper/OauthUserMapper.java b/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/mapper/OauthUserMapper.java new file mode 100644 index 0000000..875c033 --- /dev/null +++ b/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/mapper/OauthUserMapper.java @@ -0,0 +1,75 @@ +package com.ruoyi.oauth.common.mapper; + +import java.util.List; + +import com.ruoyi.oauth.common.domain.OauthUser; + +/** + * 第三方认证Mapper接口 + * + * @author ruoyi + * @date 2024-01-18 + */ +public interface OauthUserMapper { + /** + * 查询第三方认证 + * + * @param id 第三方认证主键 + * @return 第三方认证 + */ + public OauthUser selectOauthUserById(Long id); + public OauthUser selectOauthUserByUserId(Long userId); + + /** + * 查询第三方认证 + * 钉钉、抖音:uuid 为用户的 unionid + * 微信公众平台登录、京东、酷家乐、美团:uuid 为用户的 openId + * 微信开放平台登录、QQ:uuid 为用户的 openId,平台支持获取unionid, unionid 在 AuthToken + * 中(如果支持),在登录完成后,可以通过 response.getData().getToken().getUnionId() 获取 + * Google:uuid 为用户的 sub,sub为Google的所有账户体系中用户唯一的身份标识符,详见:OpenID Connect + * + * @param uuid + * @return + */ + public OauthUser selectOauthUserByUUID(String uuid); + + /** + * 查询第三方认证列表 + * + * @param oauthUser 第三方认证 + * @return 第三方认证集合 + */ + public List selectOauthUserList(OauthUser oauthUser); + + /** + * 新增第三方认证 + * + * @param oauthUser 第三方认证 + * @return 结果 + */ + public int insertOauthUser(OauthUser oauthUser); + + /** + * 修改第三方认证 + * + * @param oauthUser 第三方认证 + * @return 结果 + */ + public int updateOauthUser(OauthUser oauthUser); + + /** + * 删除第三方认证 + * + * @param id 第三方认证主键 + * @return 结果 + */ + public int deleteOauthUserById(Long id); + + /** + * 批量删除第三方认证 + * + * @param ids 需要删除的数据主键集合 + * @return 结果 + */ + public int deleteOauthUserByIds(Long[] ids); +} diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/IOauthUserService.java b/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/IOauthUserService.java new file mode 100644 index 0000000..04a2897 --- /dev/null +++ b/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/IOauthUserService.java @@ -0,0 +1,65 @@ +package com.ruoyi.oauth.common.service; + +import java.util.List; + +import com.ruoyi.oauth.common.domain.OauthUser; + +/** + * 第三方认证Service接口 + * + * @author ruoyi + * @date 2024-01-18 + */ +public interface IOauthUserService +{ + /** + * 查询第三方认证 + * + * @param id 第三方认证主键 + * @return 第三方认证 + */ + public OauthUser selectOauthUserById(Long id); + public OauthUser selectOauthUserByUUID(String uuid); + public OauthUser selectOauthUserByUserId(Long userId); + + + /** + * 查询第三方认证列表 + * + * @param oauthUser 第三方认证 + * @return 第三方认证集合 + */ + public List selectOauthUserList(OauthUser oauthUser); + + /** + * 新增第三方认证 + * + * @param oauthUser 第三方认证 + * @return 结果 + */ + public int insertOauthUser(OauthUser oauthUser); + + /** + * 修改第三方认证 + * + * @param oauthUser 第三方认证 + * @return 结果 + */ + public int updateOauthUser(OauthUser oauthUser); + + /** + * 批量删除第三方认证 + * + * @param ids 需要删除的第三方认证主键集合 + * @return 结果 + */ + public int deleteOauthUserByIds(Long[] ids); + + /** + * 删除第三方认证信息 + * + * @param id 第三方认证主键 + * @return 结果 + */ + public int deleteOauthUserById(Long id); +} diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/impl/OauthUserServiceImpl.java b/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/impl/OauthUserServiceImpl.java new file mode 100644 index 0000000..c555ff1 --- /dev/null +++ b/ruoyi-oauth/ruoyi-oauth-common/src/main/java/com/ruoyi/oauth/common/service/impl/OauthUserServiceImpl.java @@ -0,0 +1,104 @@ +package com.ruoyi.oauth.common.service.impl; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.ruoyi.oauth.common.domain.OauthUser; +import com.ruoyi.oauth.common.mapper.OauthUserMapper; +import com.ruoyi.oauth.common.service.IOauthUserService; + +/** + * 第三方认证Service业务层处理 + * + * @author ruoyi + * @date 2024-01-18 + */ +@Service +public class OauthUserServiceImpl implements IOauthUserService +{ + @Autowired + private OauthUserMapper oauthUserMapper; + + /** + * 查询第三方认证 + * + * @param id 第三方认证主键 + * @return 第三方认证 + */ + @Override + public OauthUser selectOauthUserById(Long id) + { + return oauthUserMapper.selectOauthUserById(id); + } + @Override + public OauthUser selectOauthUserByUUID(String uuid) + { + return oauthUserMapper.selectOauthUserByUUID(uuid); + } + @Override + public OauthUser selectOauthUserByUserId(Long userId) + { + return oauthUserMapper.selectOauthUserByUserId(userId); + } + /** + * 查询第三方认证列表 + * + * @param oauthUser 第三方认证 + * @return 第三方认证 + */ + @Override + public List selectOauthUserList(OauthUser oauthUser) + { + return oauthUserMapper.selectOauthUserList(oauthUser); + } + + /** + * 新增第三方认证 + * + * @param oauthUser 第三方认证 + * @return 结果 + */ + @Override + public int insertOauthUser(OauthUser oauthUser) + { + return oauthUserMapper.insertOauthUser(oauthUser); + } + + /** + * 修改第三方认证 + * + * @param oauthUser 第三方认证 + * @return 结果 + */ + @Override + public int updateOauthUser(OauthUser oauthUser) + { + return oauthUserMapper.updateOauthUser(oauthUser); + } + + /** + * 批量删除第三方认证 + * + * @param ids 需要删除的第三方认证主键 + * @return 结果 + */ + @Override + public int deleteOauthUserByIds(Long[] ids) + { + return oauthUserMapper.deleteOauthUserByIds(ids); + } + + /** + * 删除第三方认证信息 + * + * @param id 第三方认证主键 + * @return 结果 + */ + @Override + public int deleteOauthUserById(Long id) + { + return oauthUserMapper.deleteOauthUserById(id); + } +} diff --git a/ruoyi-oauth/ruoyi-oauth-common/src/main/resources/mapper/common/OauthUserMapper.xml b/ruoyi-oauth/ruoyi-oauth-common/src/main/resources/mapper/common/OauthUserMapper.xml new file mode 100644 index 0000000..26cd747 --- /dev/null +++ b/ruoyi-oauth/ruoyi-oauth-common/src/main/resources/mapper/common/OauthUserMapper.xml @@ -0,0 +1,153 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + select id, uuid, user_id, source, access_token, expire_in, refresh_token, open_id, uid, access_code, union_id, scope, token_type, id_token, mac_algorithm, mac_key, code, oauth_token, oauth_token_secret from oauth_user + + + + + + + + + + + + insert into oauth_user + + id, + uuid, + user_id, + source, + access_token, + expire_in, + refresh_token, + open_id, + uid, + access_code, + union_id, + scope, + token_type, + id_token, + mac_algorithm, + mac_key, + code, + oauth_token, + oauth_token_secret, + + + #{id}, + #{uuid}, + #{userId}, + #{source}, + #{accessToken}, + #{expireIn}, + #{refreshToken}, + #{openId}, + #{uid}, + #{accessCode}, + #{unionId}, + #{scope}, + #{tokenType}, + #{idToken}, + #{macAlgorithm}, + #{macKey}, + #{code}, + #{oauthToken}, + #{oauthTokenSecret}, + + + + + update oauth_user + + uuid = #{uuid}, + user_id = #{userId}, + source = #{source}, + access_token = #{accessToken}, + expire_in = #{expireIn}, + refresh_token = #{refreshToken}, + open_id = #{openId}, + uid = #{uid}, + access_code = #{accessCode}, + union_id = #{unionId}, + scope = #{scope}, + token_type = #{tokenType}, + id_token = #{idToken}, + mac_algorithm = #{macAlgorithm}, + mac_key = #{macKey}, + code = #{code}, + oauth_token = #{oauthToken}, + oauth_token_secret = #{oauthTokenSecret}, + + where oauth_user.id = #{id} + + + + delete from oauth_user where id = #{id} + + + + delete from oauth_user where id in + + #{id} + + + \ No newline at end of file diff --git a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxH5Constant.java b/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxPubConstant.java similarity index 84% rename from ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxH5Constant.java rename to ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxPubConstant.java index 1ba8217..fb850bb 100644 --- a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxH5Constant.java +++ b/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/constant/WxPubConstant.java @@ -4,17 +4,17 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component -public class WxH5Constant { - @Value("${wx.h5.appId}") +public class WxPubConstant { + @Value("${wx.pub.appId}") private String appId; - @Value("${wx.h5.appSecret}") + @Value("${wx.pub.appSecret}") private String appSecret; - @Value("${wx.h5.url}") + @Value("${wx.pub.url}") private String url; - @Value("${wx.h5.open}") + @Value("${wx.pub.open}") private Boolean open; public String getUrl() { diff --git a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/controller/WxLoginController.java b/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/controller/WxLoginController.java index 4bf6562..0da58d3 100644 --- a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/controller/WxLoginController.java +++ b/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/controller/WxLoginController.java @@ -1,40 +1,72 @@ package com.ruoyi.oauth.wx.controller; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.annotation.Anonymous; +import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; -import com.ruoyi.oauth.wx.constant.WxH5Constant; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.oauth.common.domain.OauthUser; +import com.ruoyi.oauth.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; @RestController @RequestMapping("/wx") public class WxLoginController extends BaseController { @Autowired - public WxH5Constant wxH5AppConstant; + public WxPubConstant wxH5AppConstant; @Autowired public WxMiniAppConstant wxMiniAppConstant; @Autowired - WxLoginServiceImpl wxLoginServiceImpl; + private WxLoginServiceImpl wxLoginServiceImpl; + + @Autowired + private IOauthUserService oauthUserService; @Anonymous - @GetMapping("/miniapp/{code}") - public AjaxResult loginMiniApp(@PathVariable("code") String code) { - return success(wxLoginServiceImpl.doLoginMiniApp(code)); + @PostMapping("/login/{source}/{code}") + public AjaxResult loginMiniApp(@PathVariable("source") String source, @PathVariable("code") String code) { + String token = null; + AjaxResult ajax = AjaxResult.success(); + if ("miniapp".equals(source)) + token = wxLoginServiceImpl.doLoginMiniApp(code); + else if ("pub".equals(source)) { + token = wxLoginServiceImpl.doLoginPub(code); + } else { + return error("错误的登录方式"); + } + ajax.put(Constants.TOKEN, token); + return ajax; } - @Anonymous - @GetMapping("/h5/{code}") - public AjaxResult loginH5App(@PathVariable("code") String code) { - return success(wxLoginServiceImpl.doLoginMiniApp(code)); + @PostMapping("/register/{source}/{code}") + public AjaxResult register(@PathVariable("source") String source, @PathVariable("code") String code) { + OauthUser oauthUser = oauthUserService.selectOauthUserByUserId(getUserId()); + if (oauthUser != null) { + return error("不可以重复绑定"); + } else { + String msg = ""; + oauthUser = new OauthUser(); + oauthUser.setUserId(getUserId()); + oauthUser.setCode(code); + if ("miniapp".equals(source)) + msg = wxLoginServiceImpl.doRegisterMiniApp(oauthUser); + else if ("pub".equals(source)) { + msg = wxLoginServiceImpl.doRegisterPub(oauthUser); + } else { + return error("错误的注册方式"); + } + return StringUtils.isEmpty(msg) ? success() : error(msg); + } } } diff --git a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/Impl/WxLoginServiceImpl.java b/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/Impl/WxLoginServiceImpl.java index bab2f50..fb06d9e 100644 --- a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/Impl/WxLoginServiceImpl.java +++ b/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/Impl/WxLoginServiceImpl.java @@ -8,11 +8,19 @@ import org.springframework.stereotype.Service; import com.alibaba.fastjson2.JSON; 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.StringUtils; +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.oauth.wx.constant.WxH5Constant; import com.ruoyi.oauth.wx.constant.WxMiniAppConstant; +import com.ruoyi.oauth.wx.constant.WxPubConstant; import com.ruoyi.oauth.wx.service.WxLoginService; +import com.ruoyi.system.service.ISysUserService; @Service public class WxLoginServiceImpl implements WxLoginService { @@ -20,12 +28,20 @@ public class WxLoginServiceImpl implements WxLoginService { private WxMiniAppConstant wxAppConstant; @Autowired - private WxH5Constant wxH5Constant; + private WxPubConstant wxH5Constant; @Autowired private HttpClientUtil httpClientUtil; + @Autowired + private TokenService tokenService; + @Autowired + private UserDetailsServiceImpl userDetailsServiceImpl; + @Autowired + private ISysUserService userService; + @Autowired + private IOauthUserService oauthUserService; - public Map doLogin(String url, String appid, String secret, String code) { + 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); @@ -44,22 +60,79 @@ public class WxLoginServiceImpl implements WxLoginService { } } + public String doLogin(String openid) { + OauthUser selectOauthUser = oauthUserService.selectOauthUserByUUID(openid); + if (selectOauthUser == null) { + return null; + } + SysUser sysUser = userService.selectUserById(selectOauthUser.getUserId()); + if (sysUser == null) { + throw new ServiceException("该微信未绑定用户"); + } + LoginUser loginUser = (LoginUser) userDetailsServiceImpl.createLoginUser(sysUser); + return tokenService.createToken(loginUser); + } + @Override - public String doLoginH5(String code) { - return doLogin( + public String doLoginPub(String code) { + String openid = doAuth( wxH5Constant.getUrl(), wxH5Constant.getAppId(), wxH5Constant.getAppSecret(), code).get("openid"); + return doLogin(openid); } @Override public String doLoginMiniApp(String code) { - return doLogin( + String openid = doAuth( wxAppConstant.getUrl(), wxAppConstant.getAppId(), wxAppConstant.getAppSecret(), code).get("openid"); + return doLogin(openid); + } + + @Override + public String doRegisterPub(OauthUser oauthUser) { + if (StringUtils.isEmpty(oauthUser.getCode())) { + return "没有凭证"; + } + if (oauthUser.getUserId() == null) { + return "请先注册账号"; + } + Map doAuth = doAuth( + wxH5Constant.getUrl(), + wxH5Constant.getAppId(), + wxH5Constant.getAppSecret(), + oauthUser.getCode()); + oauthUser.setOpenId(doAuth.get("openid")); + oauthUser.setUuid(doAuth.get("openid")); + oauthUser.setSource("WXPub"); + oauthUser.setAccessToken(doAuth.get("sessionKey")); + oauthUserService.insertOauthUser(oauthUser); + return ""; + } + + @Override + public String doRegisterMiniApp(OauthUser oauthUser) { + if (StringUtils.isEmpty(oauthUser.getCode())) { + return "没有凭证"; + } + if (oauthUser.getUserId() == null) { + return "请先注册账号"; + } + Map doAuth = doAuth( + wxAppConstant.getUrl(), + wxAppConstant.getAppId(), + wxAppConstant.getAppSecret(), + oauthUser.getCode()); + oauthUser.setOpenId(doAuth.get("openid")); + oauthUser.setUuid(doAuth.get("openid")); + oauthUser.setSource("WXPub"); + oauthUser.setAccessToken(doAuth.get("sessionKey")); + oauthUserService.insertOauthUser(oauthUser); + return ""; } } diff --git a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/WxLoginService.java b/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/WxLoginService.java index 35333cf..c4f3ccf 100644 --- a/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/WxLoginService.java +++ b/ruoyi-oauth/ruoyi-oauth-wx/src/main/java/com/ruoyi/oauth/wx/service/WxLoginService.java @@ -1,7 +1,14 @@ package com.ruoyi.oauth.wx.service; +import com.ruoyi.oauth.common.domain.OauthUser; + public interface WxLoginService { public String doLoginMiniApp(String code); - public String doLoginH5(String code); + public String doLoginPub(String code); + + + public String doRegisterPub(OauthUser oauthUser); + + public String doRegisterMiniApp(OauthUser oauthUser); } diff --git a/sql/oauth.sql b/sql/oauth.sql index e90e3dd..751ce6a 100644 --- a/sql/oauth.sql +++ b/sql/oauth.sql @@ -19,4 +19,27 @@ CREATE TABLE oauth_user ( code VARCHAR(255) COMMENT '用户的授权code,部分平台可能没有', oauth_token VARCHAR(255) COMMENT 'Twitter平台用户的附带属性,部分平台可能没有', oauth_token_secret VARCHAR(255) COMMENT 'Twitter平台用户的附带属性,部分平台可能没有' -); \ No newline at end of file +); + +-- 菜单 SQL +insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) +values('第三方认证', '1', '1', 'oauth', 'system/oauth/index', 1, 0, 'C', '0', '0', 'system:oauth:list', '#', 'admin', sysdate(), '', null, '第三方认证菜单'); + +-- 按钮父菜单ID +SELECT @parentId := LAST_INSERT_ID(); + +-- 按钮 SQL +insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) +values('第三方认证查询', @parentId, '1', '#', '', 1, 0, 'F', '0', '0', 'system:oauth:query', '#', 'admin', sysdate(), '', null, ''); + +insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) +values('第三方认证新增', @parentId, '2', '#', '', 1, 0, 'F', '0', '0', 'system:oauth:add', '#', 'admin', sysdate(), '', null, ''); + +insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) +values('第三方认证修改', @parentId, '3', '#', '', 1, 0, 'F', '0', '0', 'system:oauth:edit', '#', 'admin', sysdate(), '', null, ''); + +insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) +values('第三方认证删除', @parentId, '4', '#', '', 1, 0, 'F', '0', '0', 'system:oauth:remove', '#', 'admin', sysdate(), '', null, ''); + +insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) +values('第三方认证导出', @parentId, '5', '#', '', 1, 0, 'F', '0', '0', 'system:oauth:export', '#', 'admin', sysdate(), '', null, ''); \ No newline at end of file