Browse Source

去掉无用代码

master
刘忱 2 years ago
parent
commit
5568cf4a72
  1. 2
      win-framework/pom.xml
  2. 56
      win-framework/win-spring-boot-starter-biz-social/pom.xml
  3. 36
      win-framework/win-spring-boot-starter-biz-social/src/main/java/com/win/framework/social/config/WinSocialAutoConfiguration.java
  4. 94
      win-framework/win-spring-boot-starter-biz-social/src/main/java/com/win/framework/social/core/WinAuthRequestFactory.java
  5. 45
      win-framework/win-spring-boot-starter-biz-social/src/main/java/com/win/framework/social/core/enums/AuthExtendSource.java
  6. 97
      win-framework/win-spring-boot-starter-biz-social/src/main/java/com/win/framework/social/core/request/AuthWeChatMiniAppRequest.java
  7. 178
      win-framework/win-spring-boot-starter-biz-social/src/main/java/com/win/framework/social/core/request/AuthWeChatMpRequest.java
  8. 1
      win-framework/win-spring-boot-starter-biz-social/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
  9. 45
      win-framework/win-spring-boot-starter-biz-weixin/pom.xml
  10. 7
      win-framework/win-spring-boot-starter-biz-weixin/src/main/java/com/win/framework/weixin/package-info.java
  11. 1
      win-framework/win-spring-boot-starter-protection/src/main/java/com/win/framework/resilience4j/《芋道 Spring Boot 服务容错 Resilience4j 入门》.md
  12. 1
      win-module-bpm/win-module-bpm-biz/src/main/java/com/win/module/bpm/convert/《芋道 Spring Boot 对象转换 MapStruct 入门》.md
  13. 1
      win-module-infra/win-module-infra-biz/src/main/java/com/win/module/infra/convert/《芋道 Spring Boot 对象转换 MapStruct 入门》.md
  14. 1
      win-module-infra/win-module-infra-biz/src/main/java/com/win/module/infra/framework/monitor/《芋道 Spring Boot 监控工具 Admin 入门》.md
  15. 51
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/admin/auth/AuthController.java
  16. 26
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/admin/auth/vo/AuthLoginReqVO.java
  17. 34
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/admin/auth/vo/AuthSocialLoginReqVO.java
  18. 12
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/convert/auth/AuthConvert.java
  19. 23
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/auth/AdminAuthService.java
  20. 60
      win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/auth/AdminAuthServiceImpl.java
  21. 4
      win-server/src/main/resources/application.yaml

2
win-framework/pom.xml

@ -26,8 +26,6 @@
<module>win-spring-boot-starter-biz-operatelog</module>
<module>win-spring-boot-starter-biz-dict</module>
<module>win-spring-boot-starter-biz-sms</module>
<module>win-spring-boot-starter-biz-weixin</module>
<module>win-spring-boot-starter-biz-social</module>
<module>win-spring-boot-starter-biz-tenant</module>
<module>win-spring-boot-starter-biz-data-permission</module>
<module>win-spring-boot-starter-biz-error-code</module>

56
win-framework/win-spring-boot-starter-biz-social/pom.xml

@ -1,56 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<parent>
<groupId>com.win</groupId>
<artifactId>win-framework</artifactId>
<version>${revision}</version>
</parent>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
<artifactId>win-spring-boot-starter-biz-social</artifactId>
<name>${project.artifactId}</name>
<dependencies>
<dependency>
<groupId>com.win</groupId>
<artifactId>win-common</artifactId>
</dependency>
<!-- Spring 核心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- Web 相关 -->
<dependency>
<groupId>com.win</groupId>
<artifactId>win-spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot 配置所需依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 三方云服务相关 -->
<dependency>
<groupId>com.xingyuv</groupId>
<artifactId>spring-boot-starter-justauth</artifactId>
<exclusions>
<exclusion>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.win</groupId>
<artifactId>win-spring-boot-starter-redis</artifactId>
</dependency>
</dependencies>
</project>

36
win-framework/win-spring-boot-starter-biz-social/src/main/java/com/win/framework/social/config/WinSocialAutoConfiguration.java

@ -1,36 +0,0 @@
package com.win.framework.social.config;
import com.win.framework.social.core.WinAuthRequestFactory;
import com.xingyuv.http.HttpUtil;
import com.xingyuv.http.support.hutool.HutoolImpl;
import com.xingyuv.jushauth.cache.AuthStateCache;
import com.xingyuv.justauth.autoconfigure.JustAuthProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
/**
* 社交自动装配类
*
* @author timfruit
* @date 2021-10-30
*/
@Slf4j
@AutoConfiguration
@EnableConfigurationProperties(JustAuthProperties.class)
public class WinSocialAutoConfiguration {
@Bean
@Primary
@ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true)
public WinAuthRequestFactory winAuthRequestFactory(JustAuthProperties properties, AuthStateCache authStateCache) {
// 需要修改 HttpUtil 使用的实现,避免类报错
HttpUtil.setHttp(new HutoolImpl());
// 创建 WinAuthRequestFactory
return new WinAuthRequestFactory(properties, authStateCache);
}
}

94
win-framework/win-spring-boot-starter-biz-social/src/main/java/com/win/framework/social/core/WinAuthRequestFactory.java

@ -1,94 +0,0 @@
package com.win.framework.social.core;
import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ReflectUtil;
import com.win.framework.social.core.enums.AuthExtendSource;
import com.win.framework.social.core.request.AuthWeChatMiniAppRequest;
import com.win.framework.social.core.request.AuthWeChatMpRequest;
import com.xingyuv.jushauth.cache.AuthStateCache;
import com.xingyuv.jushauth.config.AuthConfig;
import com.xingyuv.jushauth.config.AuthSource;
import com.xingyuv.jushauth.request.AuthRequest;
import com.xingyuv.justauth.AuthRequestFactory;
import com.xingyuv.justauth.autoconfigure.JustAuthProperties;
import java.lang.reflect.Method;
import static com.xingyuv.jushauth.config.AuthDefaultSource.WECHAT_MP;
/**
* 第三方授权拓展 request 工厂类
* 为使得拓展配置 {@link AuthConfig} 和默认配置齐平所以自定义本工厂类
*
* @author timfruit
* @date 2021-10-31
*/
public class WinAuthRequestFactory extends AuthRequestFactory {
protected JustAuthProperties properties;
protected AuthStateCache authStateCache;
/**
* 由于父类 configureHttpConfig 方法是 private 修饰所以获取后进行反射调用
*/
private final Method configureHttpConfigMethod = ReflectUtil.getMethod(AuthRequestFactory.class,
"configureHttpConfig", String.class, AuthConfig.class, JustAuthProperties.JustAuthHttpConfig.class);
public WinAuthRequestFactory(JustAuthProperties properties, AuthStateCache authStateCache) {
super(properties, authStateCache);
this.properties = properties;
this.authStateCache = authStateCache;
}
/**
* 返回 AuthRequest 对象
*
* @param source {@link AuthSource}
* @return {@link AuthRequest}
*/
@Override
public AuthRequest get(String source) {
// 先尝试获取自定义扩展的
AuthRequest authRequest = getExtendRequest(source);
// 找不到,使用默认拓展
if (authRequest == null) {
authRequest = super.get(source);
}
return authRequest;
}
protected AuthRequest getExtendRequest(String source) {
// TODO 芋艿:临时兼容 justauth 迁移的类型不对问题;
if (WECHAT_MP.name().equalsIgnoreCase(source)) {
AuthConfig config = properties.getType().get(WECHAT_MP.name());
return new AuthWeChatMpRequest(config, authStateCache);
}
AuthExtendSource authExtendSource;
try {
authExtendSource = EnumUtil.fromString(AuthExtendSource.class, source.toUpperCase());
} catch (IllegalArgumentException e) {
// 无自定义匹配
return null;
}
// 拓展配置和默认配置齐平,properties 放在一起
AuthConfig config = properties.getType().get(authExtendSource.name());
// 找不到对应关系,直接返回空
if (config == null) {
return null;
}
// 反射调用,配置 http config
ReflectUtil.invoke(this, configureHttpConfigMethod, authExtendSource.name(), config, properties.getHttpConfig());
// 获得拓展的 Request
// noinspection SwitchStatementWithTooFewBranches
switch (authExtendSource) {
case WECHAT_MINI_APP:
return new AuthWeChatMiniAppRequest(config, authStateCache);
default:
return null;
}
}
}

45
win-framework/win-spring-boot-starter-biz-social/src/main/java/com/win/framework/social/core/enums/AuthExtendSource.java

@ -1,45 +0,0 @@
package com.win.framework.social.core.enums;
import com.xingyuv.jushauth.config.AuthSource;
import com.xingyuv.jushauth.request.AuthDefaultRequest;
/**
* 拓展 JustAuth api 需要的 url 用枚举类分平台类型管理
*
* 默认配置 {@link com.xingyuv.jushauth.config.AuthDefaultSource}
*
* @author timfruit
*/
public enum AuthExtendSource implements AuthSource {
/**
* 微信小程序授权登录
*/
WECHAT_MINI_APP {
@Override
public String authorize() {
// 参见 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html 文档
throw new UnsupportedOperationException("不支持获取授权 url,请使用小程序内置函数 wx.login() 登录获取 code");
}
@Override
public String accessToken() {
// 参见 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html 文档
// 获取 openid, unionId , session_key 等字段
return "https://api.weixin.qq.com/sns/jscode2session";
}
@Override
public String userInfo() {
// 参见 https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html 文档
throw new UnsupportedOperationException("不支持获取用户信息 url,请使用小程序内置函数 wx.getUserProfile() 获取用户信息");
}
@Override
public Class<? extends AuthDefaultRequest> getTargetClass() {
return null;
}
}
}

97
win-framework/win-spring-boot-starter-biz-social/src/main/java/com/win/framework/social/core/request/AuthWeChatMiniAppRequest.java

@ -1,97 +0,0 @@
package com.win.framework.social.core.request;
import com.win.framework.common.util.json.JsonUtils;
import com.win.framework.social.core.enums.AuthExtendSource;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.xingyuv.jushauth.cache.AuthStateCache;
import com.xingyuv.jushauth.config.AuthConfig;
import com.xingyuv.jushauth.exception.AuthException;
import com.xingyuv.jushauth.model.AuthCallback;
import com.xingyuv.jushauth.model.AuthToken;
import com.xingyuv.jushauth.model.AuthUser;
import com.xingyuv.jushauth.request.AuthDefaultRequest;
import com.xingyuv.jushauth.utils.HttpUtils;
import com.xingyuv.jushauth.utils.UrlBuilder;
import lombok.Data;
/**
* 微信小程序登陆 Request 请求
*
* 由于 JustAuth 定位是面向 Web 为主的三方登录所以微信小程序只能自己封装
*
* @author timfruit
* @date 2021-10-29
*/
public class AuthWeChatMiniAppRequest extends AuthDefaultRequest {
public AuthWeChatMiniAppRequest(AuthConfig config, AuthStateCache authStateCache) {
super(config, AuthExtendSource.WECHAT_MINI_APP, authStateCache);
}
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
// 参见 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html 文档
// 使用 code 获取对应的 openId、unionId 等字段
String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(authCallback.getCode())).getBody();
JSCode2SessionResponse accessTokenObject = JsonUtils.parseObject(response, JSCode2SessionResponse.class);
assert accessTokenObject != null;
checkResponse(accessTokenObject);
// 拼装结果
return AuthToken.builder()
.openId(accessTokenObject.getOpenid())
.unionId(accessTokenObject.getUnionId())
.build();
}
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
// 参见 https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html 文档
// 如果需要用户信息,需要在小程序调用函数后传给后端
return AuthUser.builder()
.username("")
.nickname("")
.avatar("")
.uuid(authToken.getOpenId())
.token(authToken)
.source(source.toString())
.build();
}
/**
* 检查响应内容是否正确
*
* @param response 请求响应内容
*/
private void checkResponse(JSCode2SessionResponse response) {
if (response.getErrorCode() != 0) {
throw new AuthException(response.getErrorCode(), response.getErrorMsg());
}
}
@Override
protected String accessTokenUrl(String code) {
return UrlBuilder.fromBaseUrl(source.accessToken())
.queryParam("appid", config.getClientId())
.queryParam("secret", config.getClientSecret())
.queryParam("js_code", code)
.queryParam("grant_type", "authorization_code")
.build();
}
@Data
@SuppressWarnings("SpellCheckingInspection")
private static class JSCode2SessionResponse {
@JsonProperty("errcode")
private int errorCode;
@JsonProperty("errmsg")
private String errorMsg;
@JsonProperty("session_key")
private String sessionKey;
private String openid;
@JsonProperty("unionid")
private String unionId;
}
}

178
win-framework/win-spring-boot-starter-biz-social/src/main/java/com/win/framework/social/core/request/AuthWeChatMpRequest.java

@ -1,178 +0,0 @@
package com.win.framework.social.core.request;
import com.alibaba.fastjson.JSONObject;
import com.xingyuv.jushauth.cache.AuthStateCache;
import com.xingyuv.jushauth.config.AuthConfig;
import com.xingyuv.jushauth.config.AuthDefaultSource;
import com.xingyuv.jushauth.enums.AuthResponseStatus;
import com.xingyuv.jushauth.enums.AuthUserGender;
import com.xingyuv.jushauth.enums.scope.AuthWechatMpScope;
import com.xingyuv.jushauth.exception.AuthException;
import com.xingyuv.jushauth.model.AuthCallback;
import com.xingyuv.jushauth.model.AuthResponse;
import com.xingyuv.jushauth.model.AuthToken;
import com.xingyuv.jushauth.model.AuthUser;
import com.xingyuv.jushauth.request.AuthDefaultRequest;
import com.xingyuv.jushauth.utils.AuthScopeUtils;
import com.xingyuv.jushauth.utils.GlobalAuthUtils;
import com.xingyuv.jushauth.utils.HttpUtils;
import com.xingyuv.jushauth.utils.UrlBuilder;
/**
* 微信公众平台登录
*
* @author yangkai.shen (https://xkcoding.com)
* @since 1.1.0
*/
public class AuthWeChatMpRequest extends AuthDefaultRequest {
public AuthWeChatMpRequest(AuthConfig config) {
super(config, AuthDefaultSource.WECHAT_MP);
}
public AuthWeChatMpRequest(AuthConfig config, AuthStateCache authStateCache) {
super(config, AuthDefaultSource.WECHAT_MP, authStateCache);
}
/**
* 微信的特殊性此时返回的信息同时包含 openid access_token
*
* @param authCallback 回调返回的参数
* @return 所有信息
*/
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
return this.getToken(accessTokenUrl(authCallback.getCode()));
}
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
String openId = authToken.getOpenId();
String response = doGetUserInfo(authToken);
JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object);
String location = String.format("%s-%s-%s", object.getString("country"), object.getString("province"), object.getString("city"));
if (object.containsKey("unionid")) {
authToken.setUnionId(object.getString("unionid"));
}
return AuthUser.builder()
.rawUserInfo(object)
.username(object.getString("nickname"))
.nickname(object.getString("nickname"))
.avatar(object.getString("headimgurl"))
.location(location)
.uuid(openId)
.gender(AuthUserGender.getWechatRealGender(object.getString("sex")))
.token(authToken)
.source(source.toString())
.build();
}
@Override
public AuthResponse refresh(AuthToken oldToken) {
return AuthResponse.builder()
.code(AuthResponseStatus.SUCCESS.getCode())
.data(this.getToken(refreshTokenUrl(oldToken.getRefreshToken())))
.build();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private void checkResponse(JSONObject object) {
if (object.containsKey("errcode")) {
throw new AuthException(object.getIntValue("errcode"), object.getString("errmsg"));
}
}
/**
* 获取token适用于获取access_token和刷新token
*
* @param accessTokenUrl 实际请求token的地址
* @return token对象
*/
private AuthToken getToken(String accessTokenUrl) {
String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl).getBody();
JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject);
return AuthToken.builder()
.accessToken(accessTokenObject.getString("access_token"))
.refreshToken(accessTokenObject.getString("refresh_token"))
.expireIn(accessTokenObject.getIntValue("expires_in"))
.openId(accessTokenObject.getString("openid"))
.scope(accessTokenObject.getString("scope"))
.build();
}
/**
* 返回带{@code state}参数的授权url授权回调时会带上这个{@code state}
*
* @param state state 验证授权流程的参数可以防止csrf
* @return 返回授权地址
* @since 1.9.3
*/
@Override
public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("appid", config.getClientId())
.queryParam("redirect_uri", GlobalAuthUtils.urlEncode(config.getRedirectUri()))
.queryParam("response_type", "code")
.queryParam("scope", this.getScopes(",", false, AuthScopeUtils.getDefaultScopes(AuthWechatMpScope.values())))
.queryParam("state", getRealState(state).concat("#wechat_redirect"))
.build();
}
/**
* 返回获取accessToken的url
*
* @param code 授权码
* @return 返回获取accessToken的url
*/
@Override
protected String accessTokenUrl(String code) {
return UrlBuilder.fromBaseUrl(source.accessToken())
.queryParam("appid", config.getClientId())
.queryParam("secret", config.getClientSecret())
.queryParam("code", code)
.queryParam("grant_type", "authorization_code")
.build();
}
/**
* 返回获取userInfo的url
*
* @param authToken 用户授权后的token
* @return 返回获取userInfo的url
*/
@Override
protected String userInfoUrl(AuthToken authToken) {
return UrlBuilder.fromBaseUrl(source.userInfo())
.queryParam("access_token", authToken.getAccessToken())
.queryParam("openid", authToken.getOpenId())
.queryParam("lang", "zh_CN")
.build();
}
/**
* 返回获取userInfo的url
*
* @param refreshToken getAccessToken方法返回的refreshToken
* @return 返回获取userInfo的url
*/
@Override
protected String refreshTokenUrl(String refreshToken) {
return UrlBuilder.fromBaseUrl(source.refresh())
.queryParam("appid", config.getClientId())
.queryParam("grant_type", "refresh_token")
.queryParam("refresh_token", refreshToken)
.build();
}
}

1
win-framework/win-spring-boot-starter-biz-social/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

@ -1 +0,0 @@
com.win.framework.social.config.WinSocialAutoConfiguration

45
win-framework/win-spring-boot-starter-biz-weixin/pom.xml

@ -1,45 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<parent>
<groupId>com.win</groupId>
<artifactId>win-framework</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>win-spring-boot-starter-biz-weixin</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>微信拓展
1. 基于 weixin-java-mp 库,对接微信公众号平台。目前主要解决微信公众号的支付场景。
2. 基于 weixin-java-miniapp 库,对接微信小程序。目前主要解决微信小程序的一键登录场景。
</description>
<url>https://github.com/YunaiV/ruoyi-vue-pro</url>
<dependencies>
<dependency>
<groupId>com.win</groupId>
<artifactId>win-common</artifactId>
</dependency>
<!-- Test 测试相关 -->
<dependency>
<groupId>com.win</groupId>
<artifactId>win-spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 三方云服务相关 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-mp-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-miniapp-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

7
win-framework/win-spring-boot-starter-biz-weixin/src/main/java/com/win/framework/weixin/package-info.java

@ -1,7 +0,0 @@
/**
* 微信拓展
* 1. 基于 weixin-java-mp 对接微信公众号平台目前主要解决微信公众号的支付场景
* 2. 基于 weixin-java-miniapp 对接微信小程序目前主要解决微信小程序的一键登录场景
*/
package com.win.framework.weixin;

1
win-framework/win-spring-boot-starter-protection/src/main/java/com/win/framework/resilience4j/《芋道 Spring Boot 服务容错 Resilience4j 入门》.md

@ -1 +0,0 @@
<https://www.iocoder.cn/Spring-Boot/Resilience4j/?win>

1
win-module-bpm/win-module-bpm-biz/src/main/java/com/win/module/bpm/convert/《芋道 Spring Boot 对象转换 MapStruct 入门》.md

@ -1 +0,0 @@
<http://www.iocoder.cn/Spring-Boot/MapStruct/?win>

1
win-module-infra/win-module-infra-biz/src/main/java/com/win/module/infra/convert/《芋道 Spring Boot 对象转换 MapStruct 入门》.md

@ -1 +0,0 @@
<http://www.iocoder.cn/Spring-Boot/MapStruct/?win>

1
win-module-infra/win-module-infra-biz/src/main/java/com/win/module/infra/framework/monitor/《芋道 Spring Boot 监控工具 Admin 入门》.md

@ -1 +0,0 @@
<http://www.iocoder.cn/Spring-Boot/Admin/?win>

51
win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/admin/auth/AuthController.java

@ -1,12 +1,13 @@
package com.win.module.system.controller.admin.auth;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.win.framework.common.enums.CommonStatusEnum;
import com.win.framework.common.pojo.CommonResult;
import com.win.framework.operatelog.core.annotations.OperateLog;
import com.win.framework.security.config.SecurityProperties;
import com.win.module.system.controller.admin.auth.vo.*;
import com.win.module.system.controller.admin.auth.vo.AuthLoginReqVO;
import com.win.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import com.win.module.system.controller.admin.auth.vo.AuthPermissionInfoRespVO;
import com.win.module.system.convert.auth.AuthConvert;
import com.win.module.system.dal.dataobject.permission.MenuDO;
import com.win.module.system.dal.dataobject.permission.RoleDO;
@ -18,10 +19,9 @@ import com.win.module.system.service.permission.PermissionService;
import com.win.module.system.service.permission.RoleService;
import com.win.module.system.service.social.SocialUserService;
import com.win.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -113,45 +113,4 @@ public class AuthController {
return success(AuthConvert.INSTANCE.convert(user, roles, menuList));
}
// ========== 短信登录相关 ==========
@PostMapping("/sms-login")
@PermitAll
@Operation(summary = "使用短信验证码登录")
@OperateLog(enable = false) // 避免 Post 请求被记录操作日志
public CommonResult<AuthLoginRespVO> smsLogin(@RequestBody @Valid AuthSmsLoginReqVO reqVO) {
return success(authService.smsLogin(reqVO));
}
@PostMapping("/send-sms-code")
@PermitAll
@Operation(summary = "发送手机验证码")
@OperateLog(enable = false) // 避免 Post 请求被记录操作日志
public CommonResult<Boolean> sendLoginSmsCode(@RequestBody @Valid AuthSmsSendReqVO reqVO) {
authService.sendSmsCode(reqVO);
return success(true);
}
// ========== 社交登录相关 ==========
@GetMapping("/social-auth-redirect")
@PermitAll
@Operation(summary = "社交授权的跳转")
@Parameters({
@Parameter(name = "type", description = "社交类型", required = true),
@Parameter(name = "redirectUri", description = "回调路径")
})
public CommonResult<String> socialLogin(@RequestParam("type") Integer type,
@RequestParam("redirectUri") String redirectUri) {
return CommonResult.success(socialUserService.getAuthorizeUrl(type, redirectUri));
}
@PostMapping("/social-login")
@PermitAll
@Operation(summary = "社交快捷登录,使用 code 授权码", description = "适合未登录的用户,但是社交账号已绑定用户")
@OperateLog(enable = false) // 避免 Post 请求被记录操作日志
public CommonResult<AuthLoginRespVO> socialQuickLogin(@RequestBody @Valid AuthSocialLoginReqVO reqVO) {
return success(authService.socialLogin(reqVO));
}
}

26
win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/admin/auth/vo/AuthLoginReqVO.java

@ -1,8 +1,5 @@
package com.win.module.system.controller.admin.auth.vo;
import cn.hutool.core.util.StrUtil;
import com.win.framework.common.validation.InEnum;
import com.win.module.system.enums.social.SocialTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
@ -10,7 +7,6 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
@ -39,31 +35,9 @@ public class AuthLoginReqVO {
@NotEmpty(message = "验证码不能为空", groups = CodeEnableGroup.class)
private String captchaVerification;
// ========== 绑定社交登录时,需要传递如下参数 ==========
@Schema(description = "社交平台的类型,参见 SocialTypeEnum 枚举值", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
@InEnum(SocialTypeEnum.class)
private Integer socialType;
@Schema(description = "授权码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
private String socialCode;
@Schema(description = "state", requiredMode = Schema.RequiredMode.REQUIRED, example = "9b2ffbc1-7425-4155-9894-9d5c08541d62")
private String socialState;
/**
* 开启验证码的 Group
*/
public interface CodeEnableGroup {}
@AssertTrue(message = "授权码不能为空")
public boolean isSocialCodeValid() {
return socialType == null || StrUtil.isNotEmpty(socialCode);
}
@AssertTrue(message = "授权 state 不能为空")
public boolean isSocialState() {
return socialType == null || StrUtil.isNotEmpty(socialState);
}
}

34
win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/admin/auth/vo/AuthSocialLoginReqVO.java

@ -1,34 +0,0 @@
package com.win.module.system.controller.admin.auth.vo;
import com.win.framework.common.validation.InEnum;
import com.win.module.system.enums.social.SocialTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Schema(description = "管理后台 - 社交绑定登录 Request VO,使用 code 授权码 + 账号密码")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AuthSocialLoginReqVO {
@Schema(description = "社交平台的类型,参见 UserSocialTypeEnum 枚举值", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
@InEnum(SocialTypeEnum.class)
@NotNull(message = "社交平台的类型不能为空")
private Integer type;
@Schema(description = "授权码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
@NotEmpty(message = "授权码不能为空")
private String code;
@Schema(description = "state", requiredMode = Schema.RequiredMode.REQUIRED, example = "9b2ffbc1-7425-4155-9894-9d5c08541d62")
@NotEmpty(message = "state 不能为空")
private String state;
}

12
win-module-system/win-module-system-biz/src/main/java/com/win/module/system/convert/auth/AuthConvert.java

@ -1,9 +1,7 @@
package com.win.module.system.convert.auth;
import com.win.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
import com.win.module.system.api.sms.dto.code.SmsCodeUseReqDTO;
import com.win.module.system.api.social.dto.SocialUserBindReqDTO;
import com.win.module.system.controller.admin.auth.vo.*;
import com.win.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import com.win.module.system.controller.admin.auth.vo.AuthPermissionInfoRespVO;
import com.win.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
import com.win.module.system.dal.dataobject.permission.MenuDO;
import com.win.module.system.dal.dataobject.permission.RoleDO;
@ -74,10 +72,4 @@ public interface AuthConvert {
return filterList(treeNodeMap.values(), node -> ID_ROOT.equals(node.getParentId()));
}
SocialUserBindReqDTO convert(Long userId, Integer userType, AuthSocialLoginReqVO reqVO);
SmsCodeSendReqDTO convert(AuthSmsSendReqVO reqVO);
SmsCodeUseReqDTO convert(AuthSmsLoginReqVO reqVO, Integer scene, String usedIp);
}

23
win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/auth/AdminAuthService.java

@ -39,29 +39,6 @@ public interface AdminAuthService {
*/
void logout(String token, Integer logType);
/**
* 短信验证码发送
*
* @param reqVO 发送请求
*/
void sendSmsCode(AuthSmsSendReqVO reqVO);
/**
* 短信登录
*
* @param reqVO 登录信息
* @return 登录结果
*/
AuthLoginRespVO smsLogin(AuthSmsLoginReqVO reqVO) ;
/**
* 社交快捷登录使用 code 授权码
*
* @param reqVO 登录信息
* @return 登录结果
*/
AuthLoginRespVO socialLogin(@Valid AuthSocialLoginReqVO reqVO);
/**
* 刷新访问令牌
*

60
win-module-system/win-module-system-biz/src/main/java/com/win/module/system/service/auth/AdminAuthServiceImpl.java

@ -1,6 +1,7 @@
package com.win.module.system.service.auth;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.annotations.VisibleForTesting;
import com.win.framework.common.enums.CommonStatusEnum;
import com.win.framework.common.enums.UserTypeEnum;
import com.win.framework.common.util.monitor.TracerUtils;
@ -8,16 +9,14 @@ import com.win.framework.common.util.servlet.ServletUtils;
import com.win.framework.common.util.validation.ValidationUtils;
import com.win.module.system.api.logger.dto.LoginLogCreateReqDTO;
import com.win.module.system.api.sms.SmsCodeApi;
import com.win.module.system.api.social.dto.SocialUserBindReqDTO;
import com.win.module.system.api.social.dto.SocialUserRespDTO;
import com.win.module.system.controller.admin.auth.vo.*;
import com.win.module.system.controller.admin.auth.vo.AuthLoginReqVO;
import com.win.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import com.win.module.system.convert.auth.AuthConvert;
import com.win.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
import com.win.module.system.dal.dataobject.user.AdminUserDO;
import com.win.module.system.enums.logger.LoginLogTypeEnum;
import com.win.module.system.enums.logger.LoginResultEnum;
import com.win.module.system.enums.oauth2.OAuth2ClientConstants;
import com.win.module.system.enums.sms.SmsSceneEnum;
import com.win.module.system.service.logger.LoginLogService;
import com.win.module.system.service.member.MemberService;
import com.win.module.system.service.oauth2.OAuth2TokenService;
@ -26,7 +25,6 @@ import com.win.module.system.service.user.AdminUserService;
import com.xingyuv.captcha.model.common.ResponseModel;
import com.xingyuv.captcha.model.vo.CaptchaVO;
import com.xingyuv.captcha.service.CaptchaService;
import com.google.common.annotations.VisibleForTesting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@ -36,7 +34,6 @@ import javax.validation.Validator;
import java.util.Objects;
import static com.win.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.win.framework.common.util.servlet.ServletUtils.getClientIP;
import static com.win.module.system.enums.ErrorCodeConstants.*;
/**
@ -96,44 +93,12 @@ public class AdminAuthServiceImpl implements AdminAuthService {
public AuthLoginRespVO login(AuthLoginReqVO reqVO) {
// 校验验证码
validateCaptcha(reqVO);
// 使用账号密码,进行登录
AdminUserDO user = authenticate(reqVO.getUsername(), reqVO.getPassword());
// 如果 socialType 非空,说明需要绑定社交用户
if (reqVO.getSocialType() != null) {
socialUserService.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState()));
}
// 创建 Token 令牌,记录登录日志
return createTokenAfterLoginSuccess(user.getId(), reqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME);
}
@Override
public void sendSmsCode(AuthSmsSendReqVO reqVO) {
// 登录场景,验证是否存在
if (userService.getUserByMobile(reqVO.getMobile()) == null) {
throw exception(AUTH_MOBILE_NOT_EXISTS);
}
// 发送验证码
smsCodeApi.sendSmsCode(AuthConvert.INSTANCE.convert(reqVO).setCreateIp(getClientIP()));
}
@Override
public AuthLoginRespVO smsLogin(AuthSmsLoginReqVO reqVO) {
// 校验验证码
smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.ADMIN_MEMBER_LOGIN.getScene(), getClientIP()));
// 获得用户信息
AdminUserDO user = userService.getUserByMobile(reqVO.getMobile());
if (user == null) {
throw exception(USER_NOT_EXISTS);
}
// 创建 Token 令牌,记录登录日志
return createTokenAfterLoginSuccess(user.getId(), reqVO.getMobile(), LoginLogTypeEnum.LOGIN_MOBILE);
}
private void createLoginLog(Long userId, String username,
LoginLogTypeEnum logTypeEnum, LoginResultEnum loginResult) {
// 插入登录日志
@ -153,25 +118,6 @@ public class AdminAuthServiceImpl implements AdminAuthService {
}
}
@Override
public AuthLoginRespVO socialLogin(AuthSocialLoginReqVO reqVO) {
// 使用 code 授权码,进行登录。然后,获得到绑定的用户编号
SocialUserRespDTO socialUser = socialUserService.getSocialUser(UserTypeEnum.ADMIN.getValue(), reqVO.getType(),
reqVO.getCode(), reqVO.getState());
if (socialUser == null) {
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
}
// 获得用户
AdminUserDO user = userService.getUser(socialUser.getUserId());
if (user == null) {
throw exception(USER_NOT_EXISTS);
}
// 创建 Token 令牌,记录登录日志
return createTokenAfterLoginSuccess(user.getId(), user.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL);
}
@VisibleForTesting
void validateCaptcha(AuthLoginReqVO reqVO) {
// 如果验证码关闭,则不进行校验

4
win-server/src/main/resources/application.yaml

@ -136,7 +136,7 @@ win:
license: MIT
license-url: https://gitee.com/zhijiantianya/ruoyi-vue-pro/blob/master/LICENSE
captcha:
enable: false # 验证码的开关,默认为 true
enable: true # 验证码的开关,默认为 true
codegen:
base-package: ${win.info.base-package}
db-schemas: ${spring.datasource.dynamic.datasource.master.name}
@ -206,6 +206,8 @@ win:
- rep_demo_jianpiao
- tmp_report_data_1
- tmp_report_data_income
- tmp_report_data_income
- system_serial_number
sms-code: # 短信验证码相关的配置项
expire-times: 10m
send-frequency: 1m

Loading…
Cancel
Save