From 93c646890d0687d87a652cb343ace8e68bfcccf3 Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Sat, 4 Jul 2020 23:25:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=20mall-spring-boot-starter-s?= =?UTF-8?q?ecurity-user=20=E6=A8=A1=E5=9D=97=EF=BC=8C=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9A=84=E8=AE=A4=E8=AF=81=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/util/ServiceExceptionUtil.java | 6 +- common/mall-security-annotations/pom.xml | 15 ++++ .../annotations}/RequiresAuthenticate.java | 2 +- .../security/annotations}/RequiresNone.java | 2 +- .../annotations}/RequiresPermissions.java | 2 +- .../pom.xml | 47 ++++++++++++ .../config/UserSecurityAutoConfiguration.java | 35 +++++++++ .../core/context/UserSecurityContext.java | 18 +++++ .../context/UserSecurityContextHolder.java | 35 +++++++++ .../interceptor/UserSecurityInterceptor.java | 72 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 2 + common/pom.xml | 2 + mall-dependencies/pom.xml | 10 +++ .../enums/SystemErrorCodeEnum.java | 5 +- .../mall/userservice/rpc/user/vo/UserVO.java | 4 ++ user-web-app/pom.xml | 5 ++ .../controller/user/UserController.java | 48 +++++++++++++ .../controller/user/vo/UserInfoVO.java | 4 +- .../mall/userweb/convert/package-info.java | 1 - .../userweb/convert/user/UserConvert.java | 15 ++++ .../userweb/manager/user/UserManager.java | 23 ++++++ .../src/main/resources/application.yml | 4 +- .../controller/users/PassportController.java | 56 --------------- .../controller/users/UserController.java | 56 --------------- .../user/application/convert/UserConvert.java | 22 ------ .../vo/users/UsersAccessTokenVO.java | 20 ------ .../vo/users/UsersMobileRegisterVO.java | 20 ------ 27 files changed, 345 insertions(+), 186 deletions(-) create mode 100644 common/mall-security-annotations/pom.xml rename common/{mall-spring-boot-starter-security/src/main/java/cn/iocoder/mall/security/core/annotation => mall-security-annotations/src/main/java/cn/iocoder/security/annotations}/RequiresAuthenticate.java (93%) rename common/{mall-spring-boot-starter-security/src/main/java/cn/iocoder/mall/security/core/annotation => mall-security-annotations/src/main/java/cn/iocoder/security/annotations}/RequiresNone.java (85%) rename common/{mall-spring-boot-starter-security/src/main/java/cn/iocoder/mall/security/core/annotation => mall-security-annotations/src/main/java/cn/iocoder/security/annotations}/RequiresPermissions.java (93%) create mode 100644 common/mall-spring-boot-starter-security-user/pom.xml create mode 100644 common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/config/UserSecurityAutoConfiguration.java create mode 100644 common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/context/UserSecurityContext.java create mode 100644 common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/context/UserSecurityContextHolder.java create mode 100644 common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/interceptor/UserSecurityInterceptor.java create mode 100644 common/mall-spring-boot-starter-security-user/src/main/resources/META-INF/spring.factories rename user/user-start/src/main/java/cn/iocoder/mall/user/application/vo/users/UsersUserVO.java => user-web-app/src/main/java/cn/iocoder/mall/userweb/controller/user/vo/UserInfoVO.java (89%) delete mode 100644 user-web-app/src/main/java/cn/iocoder/mall/userweb/convert/package-info.java create mode 100644 user-web-app/src/main/java/cn/iocoder/mall/userweb/convert/user/UserConvert.java create mode 100644 user-web-app/src/main/java/cn/iocoder/mall/userweb/manager/user/UserManager.java delete mode 100644 user/user-start/src/main/java/cn/iocoder/mall/user/application/controller/users/PassportController.java delete mode 100644 user/user-start/src/main/java/cn/iocoder/mall/user/application/controller/users/UserController.java delete mode 100644 user/user-start/src/main/java/cn/iocoder/mall/user/application/convert/UserConvert.java delete mode 100644 user/user-start/src/main/java/cn/iocoder/mall/user/application/vo/users/UsersAccessTokenVO.java delete mode 100644 user/user-start/src/main/java/cn/iocoder/mall/user/application/vo/users/UsersMobileRegisterVO.java diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/ServiceExceptionUtil.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/ServiceExceptionUtil.java index 7fc1fa1c2..71beedce4 100644 --- a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/ServiceExceptionUtil.java +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/ServiceExceptionUtil.java @@ -71,11 +71,13 @@ public class ServiceExceptionUtil { } public static ServiceException exception(Enumerable enumerable) { - return exception(enumerable.getCode()); + String messagePattern = messages.getOrDefault(enumerable.getCode(), enumerable.getMessage()); + return exception0(enumerable.getCode(), messagePattern); } public static ServiceException exception(Enumerable enumerable, Object... params) { - return exception(enumerable.getCode(), params); + String messagePattern = messages.getOrDefault(enumerable.getCode(), enumerable.getMessage()); + return exception0(enumerable.getCode(), messagePattern, params); } /** diff --git a/common/mall-security-annotations/pom.xml b/common/mall-security-annotations/pom.xml new file mode 100644 index 000000000..9b5840ab5 --- /dev/null +++ b/common/mall-security-annotations/pom.xml @@ -0,0 +1,15 @@ + + + + common + cn.iocoder.mall + 1.0-SNAPSHOT + + 4.0.0 + + mall-security-annotations + + + diff --git a/common/mall-spring-boot-starter-security/src/main/java/cn/iocoder/mall/security/core/annotation/RequiresAuthenticate.java b/common/mall-security-annotations/src/main/java/cn/iocoder/security/annotations/RequiresAuthenticate.java similarity index 93% rename from common/mall-spring-boot-starter-security/src/main/java/cn/iocoder/mall/security/core/annotation/RequiresAuthenticate.java rename to common/mall-security-annotations/src/main/java/cn/iocoder/security/annotations/RequiresAuthenticate.java index bd6b84d2d..98d5ba9f8 100644 --- a/common/mall-spring-boot-starter-security/src/main/java/cn/iocoder/mall/security/core/annotation/RequiresAuthenticate.java +++ b/common/mall-security-annotations/src/main/java/cn/iocoder/security/annotations/RequiresAuthenticate.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.security.core.annotation; +package cn.iocoder.security.annotations; import java.lang.annotation.*; diff --git a/common/mall-spring-boot-starter-security/src/main/java/cn/iocoder/mall/security/core/annotation/RequiresNone.java b/common/mall-security-annotations/src/main/java/cn/iocoder/security/annotations/RequiresNone.java similarity index 85% rename from common/mall-spring-boot-starter-security/src/main/java/cn/iocoder/mall/security/core/annotation/RequiresNone.java rename to common/mall-security-annotations/src/main/java/cn/iocoder/security/annotations/RequiresNone.java index 25119e0c0..efc4801a4 100644 --- a/common/mall-spring-boot-starter-security/src/main/java/cn/iocoder/mall/security/core/annotation/RequiresNone.java +++ b/common/mall-security-annotations/src/main/java/cn/iocoder/security/annotations/RequiresNone.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.security.core.annotation; +package cn.iocoder.security.annotations; import java.lang.annotation.*; diff --git a/common/mall-spring-boot-starter-security/src/main/java/cn/iocoder/mall/security/core/annotation/RequiresPermissions.java b/common/mall-security-annotations/src/main/java/cn/iocoder/security/annotations/RequiresPermissions.java similarity index 93% rename from common/mall-spring-boot-starter-security/src/main/java/cn/iocoder/mall/security/core/annotation/RequiresPermissions.java rename to common/mall-security-annotations/src/main/java/cn/iocoder/security/annotations/RequiresPermissions.java index b40ced91a..efb21c235 100644 --- a/common/mall-spring-boot-starter-security/src/main/java/cn/iocoder/mall/security/core/annotation/RequiresPermissions.java +++ b/common/mall-security-annotations/src/main/java/cn/iocoder/security/annotations/RequiresPermissions.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.security.core.annotation; +package cn.iocoder.security.annotations; import java.lang.annotation.*; diff --git a/common/mall-spring-boot-starter-security-user/pom.xml b/common/mall-spring-boot-starter-security-user/pom.xml new file mode 100644 index 000000000..c998755a4 --- /dev/null +++ b/common/mall-spring-boot-starter-security-user/pom.xml @@ -0,0 +1,47 @@ + + + + common + cn.iocoder.mall + 1.0-SNAPSHOT + + 4.0.0 + + mall-spring-boot-starter-security-user + + + + + cn.iocoder.mall + system-service-api + 1.0-SNAPSHOT + + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + + cn.iocoder.mall + mall-spring-boot-starter-web + + + + cn.iocoder.mall + mall-security-annotations + + + + + org.apache.dubbo + dubbo + + + + diff --git a/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/config/UserSecurityAutoConfiguration.java b/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/config/UserSecurityAutoConfiguration.java new file mode 100644 index 000000000..e6b1577e4 --- /dev/null +++ b/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/config/UserSecurityAutoConfiguration.java @@ -0,0 +1,35 @@ +package cn.iocoder.mall.security.user.config; + +import cn.iocoder.mall.security.user.core.interceptor.UserSecurityInterceptor; +import cn.iocoder.mall.web.config.CommonWebAutoConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@AutoConfigureAfter(CommonWebAutoConfiguration.class) // 在 CommonWebAutoConfiguration 之后自动配置,保证过滤器的顺序 +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) +public class UserSecurityAutoConfiguration implements WebMvcConfigurer { + + private Logger logger = LoggerFactory.getLogger(getClass()); + + // ========== 拦截器相关 ========== + + @Bean + public UserSecurityInterceptor userSecurityInterceptor() { + return new UserSecurityInterceptor(); + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + // UserSecurityInterceptor 拦截器 + registry.addInterceptor(this.userSecurityInterceptor()); + logger.info("[addInterceptors][加载 UserSecurityInterceptor 拦截器完成]"); + } + +} diff --git a/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/context/UserSecurityContext.java b/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/context/UserSecurityContext.java new file mode 100644 index 000000000..12ff0904e --- /dev/null +++ b/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/context/UserSecurityContext.java @@ -0,0 +1,18 @@ +package cn.iocoder.mall.security.user.core.context; + +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * User Security 上下文 + */ +@Data +@Accessors(chain = true) +public class UserSecurityContext { + + /** + * 用户编号 + */ + private Integer userId; + +} diff --git a/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/context/UserSecurityContextHolder.java b/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/context/UserSecurityContextHolder.java new file mode 100644 index 000000000..14702639b --- /dev/null +++ b/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/context/UserSecurityContextHolder.java @@ -0,0 +1,35 @@ +package cn.iocoder.mall.security.user.core.context; + +/** + * {@link UserSecurityContext} Holder + * + * 参考 spring security 的 ThreadLocalSecurityContextHolderStrategy 类,简单实现。 + */ +public class UserSecurityContextHolder { + + private static final ThreadLocal SECURITY_CONTEXT = new ThreadLocal(); + + public static void setContext(UserSecurityContext context) { + SECURITY_CONTEXT.set(context); + } + + public static UserSecurityContext getContext() { + UserSecurityContext ctx = SECURITY_CONTEXT.get(); + // 为空时,设置一个空的进去 + if (ctx == null) { + ctx = new UserSecurityContext(); + SECURITY_CONTEXT.set(ctx); + } + return ctx; + } + + public static Integer getUserId() { + UserSecurityContext ctx = SECURITY_CONTEXT.get(); + return ctx != null ? ctx.getUserId() : null; + } + + public static void clear() { + SECURITY_CONTEXT.remove(); + } + +} diff --git a/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/interceptor/UserSecurityInterceptor.java b/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/interceptor/UserSecurityInterceptor.java new file mode 100644 index 000000000..acfa030fd --- /dev/null +++ b/common/mall-spring-boot-starter-security-user/src/main/java/cn/iocoder/mall/security/user/core/interceptor/UserSecurityInterceptor.java @@ -0,0 +1,72 @@ +package cn.iocoder.mall.security.user.core.interceptor; + +import cn.iocoder.common.framework.enums.UserTypeEnum; +import cn.iocoder.common.framework.util.HttpUtil; +import cn.iocoder.common.framework.util.ServiceExceptionUtil; +import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.security.user.core.context.UserSecurityContext; +import cn.iocoder.mall.security.user.core.context.UserSecurityContextHolder; +import cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum; +import cn.iocoder.mall.systemservice.rpc.oauth.OAuth2Rpc; +import cn.iocoder.mall.systemservice.rpc.oauth.vo.OAuth2AccessTokenVO; +import cn.iocoder.mall.web.core.util.CommonWebUtil; +import cn.iocoder.security.annotations.RequiresAuthenticate; +import cn.iocoder.security.annotations.RequiresPermissions; +import org.apache.dubbo.config.annotation.Reference; +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.OAUTH_USER_TYPE_ERROR; + +public class UserSecurityInterceptor extends HandlerInterceptorAdapter { + + @Reference(validation = "true", version = "${dubbo.consumer.OAuth2Rpc.version}") + private OAuth2Rpc oauth2Rpc; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { + // 获得访问令牌 + String accessToken = HttpUtil.obtainAuthorization(request); + Integer userId = null; + if (accessToken != null) { + CommonResult checkAccessTokenResult = oauth2Rpc.checkAccessToken(accessToken); + checkAccessTokenResult.checkError(); + // 校验用户类型正确 + if (!UserTypeEnum.USER.getValue().equals(checkAccessTokenResult.getData().getUserType())) { + throw ServiceExceptionUtil.exception(OAUTH_USER_TYPE_ERROR); + } + // 获得用户编号 + userId = checkAccessTokenResult.getData().getUserId(); + // 设置到 Request 中 + CommonWebUtil.setUserId(request, userId); + CommonWebUtil.setUserType(request, UserTypeEnum.USER.getValue()); + // 设置到 + UserSecurityContext userSecurityContext = new UserSecurityContext().setUserId(userId); + UserSecurityContextHolder.setContext(userSecurityContext); + } + // 校验认证 + this.checkAuthentication((HandlerMethod) handler, userId); + return true; + } + + private void checkAuthentication(HandlerMethod handlerMethod, Integer userId) { + boolean requiresAuthenticate = false; // 对于 USER 来说,默认无需登录 + if (handlerMethod.hasMethodAnnotation(RequiresAuthenticate.class) + || handlerMethod.hasMethodAnnotation(RequiresPermissions.class)) { // 如果需要权限验证,也认为需要认证 + requiresAuthenticate = true; + } + if (requiresAuthenticate && userId == null) { + throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.OAUTH2_NOT_AUTHENTICATION); + } + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { + // 清空 SecurityContext + UserSecurityContextHolder.clear(); + } + +} diff --git a/common/mall-spring-boot-starter-security-user/src/main/resources/META-INF/spring.factories b/common/mall-spring-boot-starter-security-user/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..716481f0a --- /dev/null +++ b/common/mall-spring-boot-starter-security-user/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + cn.iocoder.mall.security.user.config.UserSecurityAutoConfiguration diff --git a/common/pom.xml b/common/pom.xml index 5bdc0c06f..3929611ec 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -16,7 +16,9 @@ mall-spring-boot mall-spring-boot-starter-swagger mall-spring-boot-starter-web + mall-security-annotations mall-spring-boot-starter-security + mall-spring-boot-starter-security-user mall-spring-boot-starter-mybatis diff --git a/mall-dependencies/pom.xml b/mall-dependencies/pom.xml index c42475a83..5980e2879 100644 --- a/mall-dependencies/pom.xml +++ b/mall-dependencies/pom.xml @@ -140,11 +140,21 @@ 1.0-SNAPSHOT + + cn.iocoder.mall + mall-security-annotations + 1.0-SNAPSHOT + cn.iocoder.mall mall-spring-boot-starter-security 1.0-SNAPSHOT + + cn.iocoder.mall + mall-spring-boot-starter-security-user + 1.0-SNAPSHOT + cn.iocoder.mall diff --git a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/SystemErrorCodeEnum.java b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/SystemErrorCodeEnum.java index 884e6721f..898bdbaf9 100644 --- a/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/SystemErrorCodeEnum.java +++ b/system-service-project/system-service-api/src/main/java/cn/iocoder/mall/systemservice/enums/SystemErrorCodeEnum.java @@ -15,13 +15,12 @@ public enum SystemErrorCodeEnum implements ServiceExceptionUtil.Enumerablespring-boot-starter-web + + cn.iocoder.mall + mall-spring-boot-starter-security-user + + com.alibaba.cloud diff --git a/user-web-app/src/main/java/cn/iocoder/mall/userweb/controller/user/UserController.java b/user-web-app/src/main/java/cn/iocoder/mall/userweb/controller/user/UserController.java index f3714e2c6..619c86551 100644 --- a/user-web-app/src/main/java/cn/iocoder/mall/userweb/controller/user/UserController.java +++ b/user-web-app/src/main/java/cn/iocoder/mall/userweb/controller/user/UserController.java @@ -1,7 +1,55 @@ package cn.iocoder.mall.userweb.controller.user; +import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.security.user.core.context.UserSecurityContextHolder; +import cn.iocoder.mall.userweb.controller.user.vo.UserInfoVO; +import cn.iocoder.mall.userweb.manager.user.UserManager; +import cn.iocoder.security.annotations.RequiresAuthenticate; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import static cn.iocoder.common.framework.vo.CommonResult.success; @Api(tags = "用户信息 API") +@RestController +@RequestMapping("/user") public class UserController { + + @Autowired + private UserManager userManager; + + @ApiOperation(value = "用户信息") + @GetMapping("/info") + @RequiresAuthenticate + public CommonResult info() { + UserInfoVO user = userManager.getUser(UserSecurityContextHolder.getUserId()); + return success(user); + } + +// @PostMapping("/update_avatar") +// @RequiresLogin +// @ApiOperation(value = "更新头像") +// public CommonResult updateAvatar(@RequestParam("avatar") String avatar) { +// // 创建 +// UserUpdateDTO userUpdateDTO = new UserUpdateDTO().setId(UserSecurityContextHolder.getContext().getUserId()) +// .setAvatar(avatar); +// // 更新头像 +// return success(userService.updateUser(userUpdateDTO)); +// } +// +// @PostMapping("/update_nickname") +// @RequiresLogin +// @ApiOperation(value = "更新昵称") +// public CommonResult updateNickname(@RequestParam("nickname") String nickname) { +// // 创建 +// UserUpdateDTO userUpdateDTO = new UserUpdateDTO().setId(UserSecurityContextHolder.getContext().getUserId()) +// .setNickname(nickname); +// // 更新头像 +// return success(userService.updateUser(userUpdateDTO)); +// } + } diff --git a/user/user-start/src/main/java/cn/iocoder/mall/user/application/vo/users/UsersUserVO.java b/user-web-app/src/main/java/cn/iocoder/mall/userweb/controller/user/vo/UserInfoVO.java similarity index 89% rename from user/user-start/src/main/java/cn/iocoder/mall/user/application/vo/users/UsersUserVO.java rename to user-web-app/src/main/java/cn/iocoder/mall/userweb/controller/user/vo/UserInfoVO.java index ca03b2d48..ac373bacb 100644 --- a/user/user-start/src/main/java/cn/iocoder/mall/user/application/vo/users/UsersUserVO.java +++ b/user-web-app/src/main/java/cn/iocoder/mall/userweb/controller/user/vo/UserInfoVO.java @@ -1,4 +1,4 @@ -package cn.iocoder.mall.user.application.vo.users; +package cn.iocoder.mall.userweb.controller.user.vo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -8,7 +8,7 @@ import lombok.experimental.Accessors; @ApiModel("用户信息 VO") @Data @Accessors(chain = true) -public class UsersUserVO { +public class UserInfoVO { @ApiModelProperty(value = "用户编号", required = true, example = "123") private Integer id; diff --git a/user-web-app/src/main/java/cn/iocoder/mall/userweb/convert/package-info.java b/user-web-app/src/main/java/cn/iocoder/mall/userweb/convert/package-info.java deleted file mode 100644 index 10838ccc1..000000000 --- a/user-web-app/src/main/java/cn/iocoder/mall/userweb/convert/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package cn.iocoder.mall.userweb.convert; diff --git a/user-web-app/src/main/java/cn/iocoder/mall/userweb/convert/user/UserConvert.java b/user-web-app/src/main/java/cn/iocoder/mall/userweb/convert/user/UserConvert.java new file mode 100644 index 000000000..4036396fc --- /dev/null +++ b/user-web-app/src/main/java/cn/iocoder/mall/userweb/convert/user/UserConvert.java @@ -0,0 +1,15 @@ +package cn.iocoder.mall.userweb.convert.user; + +import cn.iocoder.mall.userservice.rpc.user.vo.UserVO; +import cn.iocoder.mall.userweb.controller.user.vo.UserInfoVO; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface UserConvert { + + UserConvert INSTANCE = Mappers.getMapper(UserConvert.class); + + UserInfoVO convert(UserVO bean); + +} diff --git a/user-web-app/src/main/java/cn/iocoder/mall/userweb/manager/user/UserManager.java b/user-web-app/src/main/java/cn/iocoder/mall/userweb/manager/user/UserManager.java new file mode 100644 index 000000000..939b16f56 --- /dev/null +++ b/user-web-app/src/main/java/cn/iocoder/mall/userweb/manager/user/UserManager.java @@ -0,0 +1,23 @@ +package cn.iocoder.mall.userweb.manager.user; + +import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.userservice.rpc.user.UserRpc; +import cn.iocoder.mall.userservice.rpc.user.vo.UserVO; +import cn.iocoder.mall.userweb.controller.user.vo.UserInfoVO; +import cn.iocoder.mall.userweb.convert.user.UserConvert; +import org.apache.dubbo.config.annotation.Reference; +import org.springframework.stereotype.Service; + +@Service +public class UserManager { + + @Reference(version = "${dubbo.consumer.UserRpc.version}", validation = "false") + private UserRpc userRpc; + + public UserInfoVO getUser(Integer id) { + CommonResult userResult = userRpc.getUser(id); + userResult.checkError(); + return UserConvert.INSTANCE.convert(userResult.getData()); + } + +} diff --git a/user-web-app/src/main/resources/application.yml b/user-web-app/src/main/resources/application.yml index 6e32b063a..4dca395be 100644 --- a/user-web-app/src/main/resources/application.yml +++ b/user-web-app/src/main/resources/application.yml @@ -16,7 +16,7 @@ spring: dubbo: # Spring Cloud Alibaba Dubbo 专属配置 cloud: - subscribed-services: 'user-service, system-service' # 设置订阅的应用列表,默认为 * 订阅所有应用 + subscribed-services: 'user-service,system-service' # 设置订阅的应用列表,默认为 * 订阅所有应用 # Dubbo 服务消费者的配置 consumer: timeout: 10000 @@ -26,3 +26,5 @@ dubbo: version: 1.0.0 OAuth2Rpc: version: 1.0.0 + SystemLogRPC: + version: 1.0.0 diff --git a/user/user-start/src/main/java/cn/iocoder/mall/user/application/controller/users/PassportController.java b/user/user-start/src/main/java/cn/iocoder/mall/user/application/controller/users/PassportController.java deleted file mode 100644 index 5afea5194..000000000 --- a/user/user-start/src/main/java/cn/iocoder/mall/user/application/controller/users/PassportController.java +++ /dev/null @@ -1,56 +0,0 @@ -package cn.iocoder.mall.user.application.controller.users; - -import cn.iocoder.common.framework.constant.UserTypeEnum; -import cn.iocoder.common.framework.vo.CommonResult; -import cn.iocoder.mall.system.api.OAuth2Service; -import cn.iocoder.mall.system.api.bo.oauth2.OAuth2AccessTokenBO; -import cn.iocoder.mall.system.api.dto.oauth2.OAuth2RefreshTokenDTO; -import cn.iocoder.mall.user.api.UserService; -import io.swagger.annotations.Api; -import org.apache.dubbo.config.annotation.Reference; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -import static cn.iocoder.common.framework.vo.CommonResult.success; - -@RestController -@RequestMapping("users/passport") -@Api("Passport 模块") -public class PassportController { - - @Reference(validation = "true", version = "${dubbo.consumer.OAuth2Service.version}") - private OAuth2Service oauth2Service; - @Reference(validation = "true", version = "${dubbo.provider.UserService.version}") - private UserService userService; - - // TODO 功能:手机密码登陆 -// @PostMapping("/mobile/pwd/login") -// public OAuth2AccessToken mobileLogin(@RequestParam("mobile") String mobile, -// @RequestParam("password") String password) { -// return oauth2Service.getAccessToken(clientId, clientSecret, mobile, password); -// } - - // TODO 芋艿,改绑手机号 - - // TODO 功能:qq 登陆 - @PostMapping("/qq/login") - public String qqLogin() { - return null; - } - - // TODO 功能:qq 绑定 - @PostMapping("/qq/bind") - public String qqBind() { - return null; - } - - @PostMapping("/refresh_token") // TODO 功能:刷新 token - public CommonResult refreshToken(@RequestParam("refreshToken") String refreshToken) { - return success(oauth2Service.refreshToken(new OAuth2RefreshTokenDTO().setRefreshToken(refreshToken) - .setUserType(UserTypeEnum.USER.getValue()))); - } - - // TODO 功能:退出,销毁 token -} diff --git a/user/user-start/src/main/java/cn/iocoder/mall/user/application/controller/users/UserController.java b/user/user-start/src/main/java/cn/iocoder/mall/user/application/controller/users/UserController.java deleted file mode 100644 index fdd232e1b..000000000 --- a/user/user-start/src/main/java/cn/iocoder/mall/user/application/controller/users/UserController.java +++ /dev/null @@ -1,56 +0,0 @@ -package cn.iocoder.mall.user.application.controller.users; - -import cn.iocoder.common.framework.vo.CommonResult; -import cn.iocoder.mall.user.api.UserService; -import cn.iocoder.mall.user.api.bo.UserBO; -import cn.iocoder.mall.user.api.dto.UserUpdateDTO; -import cn.iocoder.mall.user.application.convert.UserConvert; -import cn.iocoder.mall.user.application.vo.users.UsersUserVO; -import cn.iocoder.mall.user.sdk.annotation.RequiresLogin; -import cn.iocoder.mall.user.sdk.context.UserSecurityContextHolder; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import org.apache.dubbo.config.annotation.Reference; -import org.springframework.web.bind.annotation.*; - -import static cn.iocoder.common.framework.vo.CommonResult.success; - -@RestController -@RequestMapping("/users/user") -@Api("用户模块") -public class UserController { - - @Reference(validation = "true", version = "${dubbo.provider.UserService.version}") - private UserService userService; - - @GetMapping("/info") - @RequiresLogin - @ApiOperation(value = "用户信息") - public CommonResult info() { - UserBO userResult = userService.getUser(UserSecurityContextHolder.getContext().getUserId()); - return success(UserConvert.INSTANCE.convert2(userResult)); - } - - @PostMapping("/update_avatar") - @RequiresLogin - @ApiOperation(value = "更新头像") - public CommonResult updateAvatar(@RequestParam("avatar") String avatar) { - // 创建 - UserUpdateDTO userUpdateDTO = new UserUpdateDTO().setId(UserSecurityContextHolder.getContext().getUserId()) - .setAvatar(avatar); - // 更新头像 - return success(userService.updateUser(userUpdateDTO)); - } - - @PostMapping("/update_nickname") - @RequiresLogin - @ApiOperation(value = "更新昵称") - public CommonResult updateNickname(@RequestParam("nickname") String nickname) { - // 创建 - UserUpdateDTO userUpdateDTO = new UserUpdateDTO().setId(UserSecurityContextHolder.getContext().getUserId()) - .setNickname(nickname); - // 更新头像 - return success(userService.updateUser(userUpdateDTO)); - } - -} diff --git a/user/user-start/src/main/java/cn/iocoder/mall/user/application/convert/UserConvert.java b/user/user-start/src/main/java/cn/iocoder/mall/user/application/convert/UserConvert.java deleted file mode 100644 index 89327c2ad..000000000 --- a/user/user-start/src/main/java/cn/iocoder/mall/user/application/convert/UserConvert.java +++ /dev/null @@ -1,22 +0,0 @@ -package cn.iocoder.mall.user.application.convert; - -import cn.iocoder.mall.user.api.bo.UserBO; -import cn.iocoder.mall.user.api.bo.UserPageBO; -import cn.iocoder.mall.user.application.vo.admins.AdminsUserPageVO; -import cn.iocoder.mall.user.application.vo.users.UsersUserVO; -import org.mapstruct.Mapper; -import org.mapstruct.Mappings; -import org.mapstruct.factory.Mappers; - -@Mapper -public interface UserConvert { - - UserConvert INSTANCE = Mappers.getMapper(UserConvert.class); - - @Mappings({}) - AdminsUserPageVO convert(UserPageBO result); - - @Mappings({}) - UsersUserVO convert2(UserBO result); - -} diff --git a/user/user-start/src/main/java/cn/iocoder/mall/user/application/vo/users/UsersAccessTokenVO.java b/user/user-start/src/main/java/cn/iocoder/mall/user/application/vo/users/UsersAccessTokenVO.java deleted file mode 100644 index 54d2b5531..000000000 --- a/user/user-start/src/main/java/cn/iocoder/mall/user/application/vo/users/UsersAccessTokenVO.java +++ /dev/null @@ -1,20 +0,0 @@ -package cn.iocoder.mall.user.application.vo.users; - -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; -import lombok.Data; -import lombok.experimental.Accessors; - -@ApiModel("认证令牌 VO") -@Data -@Accessors(chain = true) -public class UsersAccessTokenVO { - - @ApiModelProperty(value = "访问令牌", required = true, example = "2e3d7635c15e47e997611707a237859f") - private String accessToken; - @ApiModelProperty(value = "刷新令牌", required = true, example = "d091e7c35bbb4313b0f557a6ef23d033") - private String refreshToken; - @ApiModelProperty(value = "过期时间,单位:秒", required = true, example = "2879") - private Integer expiresIn; - -} diff --git a/user/user-start/src/main/java/cn/iocoder/mall/user/application/vo/users/UsersMobileRegisterVO.java b/user/user-start/src/main/java/cn/iocoder/mall/user/application/vo/users/UsersMobileRegisterVO.java deleted file mode 100644 index 380dd7262..000000000 --- a/user/user-start/src/main/java/cn/iocoder/mall/user/application/vo/users/UsersMobileRegisterVO.java +++ /dev/null @@ -1,20 +0,0 @@ -package cn.iocoder.mall.user.application.vo.users; - -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; -import lombok.Data; -import lombok.experimental.Accessors; - -@ApiModel("手机注册结果 VO") -@Data -@Accessors(chain = true) -public class UsersMobileRegisterVO { - - @ApiModelProperty(value = "访问令牌", required = true, example = "2e3d7635c15e47e997611707a237859f") - private String accessToken; - @ApiModelProperty(value = "刷新令牌", required = true, example = "d091e7c35bbb4313b0f557a6ef23d033") - private String refreshToken; - @ApiModelProperty(value = "过期时间,单位:秒", required = true, example = "2879") - private Integer expiresIn; - -}