diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/config/GlobalResponseBodyAdvice.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/config/GlobalResponseBodyAdvice.java deleted file mode 100644 index 7c55e1bb..00000000 --- a/common/common-framework/src/main/java/cn/iocoder/common/framework/config/GlobalResponseBodyAdvice.java +++ /dev/null @@ -1,31 +0,0 @@ -package cn.iocoder.common.framework.config; - -import cn.iocoder.common.framework.vo.CommonResult; -import cn.iocoder.common.framework.vo.RestResult; -import org.springframework.core.MethodParameter; -import org.springframework.http.MediaType; -import org.springframework.http.server.ServerHttpRequest; -import org.springframework.http.server.ServerHttpResponse; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; - -@ControllerAdvice -public class GlobalResponseBodyAdvice implements ResponseBodyAdvice { - - @Override - public boolean supports(MethodParameter returnType, Class converterType) { - return true; // TODO 芋艿,未来,这里可以剔除掉一些,需要特殊返回的接口 - } - - @Override - public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { - if (body instanceof RestResult) { - return body; - } - if (body instanceof CommonResult) { // TODO 芋艿,后续要改下 - return body; - } - return RestResult.ok(body); - } - -} \ No newline at end of file diff --git a/order/order-application/src/main/java/cn/iocoder/mall/order/config/MVCConfiguration.java b/order/order-application/src/main/java/cn/iocoder/mall/order/config/MVCConfiguration.java new file mode 100644 index 00000000..343dcc45 --- /dev/null +++ b/order/order-application/src/main/java/cn/iocoder/mall/order/config/MVCConfiguration.java @@ -0,0 +1,36 @@ +package cn.iocoder.mall.order.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.*; + +@EnableWebMvc +@Configuration +public class MVCConfiguration implements WebMvcConfigurer { + +// @Autowired +// private SecurityInterceptor securityInterceptor; + +// @Reference +// private OAuth2Service oauth2Service; + + @Override + public void addInterceptors(InterceptorRegistry registry) { +// registry.addInterceptor(securityInterceptor); + } + +// @Override +// public void addViewControllers(ViewControllerRegistry registry) { +// registry.addRedirectViewController("/api/v2/api-docs", "/v2/api-docs"); +// registry.addRedirectViewController("/api/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui"); +// registry.addRedirectViewController("/api/swagger-resources/configuration/security", "/swagger-resources/configuration/security"); +// registry.addRedirectViewController("/api/swagger-resources", "/swagger-resources"); +// } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + // 解决 swagger-ui.html 的访问,参考自 https://stackoverflow.com/questions/43545540/swagger-ui-no-mapping-found-for-http-request 解决 + registry.addResourceHandler("swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html"); + registry.addResourceHandler("webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); + } + +} \ No newline at end of file diff --git a/order/order-application/src/main/java/cn/iocoder/mall/order/config/SwaggerConfiguration.java b/order/order-application/src/main/java/cn/iocoder/mall/order/config/SwaggerConfiguration.java new file mode 100644 index 00000000..08f28bd7 --- /dev/null +++ b/order/order-application/src/main/java/cn/iocoder/mall/order/config/SwaggerConfiguration.java @@ -0,0 +1,36 @@ +package cn.iocoder.mall.order.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@Configuration +@EnableSwagger2 +public class SwaggerConfiguration { + + @Bean + public Docket createRestApi() { + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("cn.iocoder.mall.order.controller")) + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title("订单子系统") + .description("订单子系统") + .termsOfServiceUrl("http://www.iocoder.cn") + .version("1.0.0") + .build(); + } + +} \ No newline at end of file diff --git a/product/product-application/src/main/java/cn/iocoder/mall/product/config/MVCConfiguration.java b/product/product-application/src/main/java/cn/iocoder/mall/product/config/MVCConfiguration.java index c1b865c8..b27d40f4 100644 --- a/product/product-application/src/main/java/cn/iocoder/mall/product/config/MVCConfiguration.java +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/config/MVCConfiguration.java @@ -1,16 +1,14 @@ package cn.iocoder.mall.product.config; -import cn.iocoder.common.framework.config.GlobalExceptionHandler; -import cn.iocoder.common.framework.config.GlobalResponseBodyAdvice; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @EnableWebMvc @Configuration -@Import(value = {GlobalResponseBodyAdvice.class, GlobalExceptionHandler.class}) // 统一全局返回 +//@Import(value = {GlobalExceptionHandler.class}) // 统一全局返回 public class MVCConfiguration implements WebMvcConfigurer { // @Autowired @@ -24,4 +22,11 @@ public class MVCConfiguration implements WebMvcConfigurer { // registry.addInterceptor(securityInterceptor); } + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + // 解决 swagger-ui.html 的访问,参考自 https://stackoverflow.com/questions/43545540/swagger-ui-no-mapping-found-for-http-request 解决 + registry.addResourceHandler("swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html"); + registry.addResourceHandler("webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); + } + } \ No newline at end of file diff --git a/product/product-application/src/main/java/cn/iocoder/mall/product/controller/user/ProductCategoryController.java b/product/product-application/src/main/java/cn/iocoder/mall/product/controller/user/ProductCategoryController.java index 1067df36..d35ac88d 100644 --- a/product/product-application/src/main/java/cn/iocoder/mall/product/controller/user/ProductCategoryController.java +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/controller/user/ProductCategoryController.java @@ -24,7 +24,7 @@ public class ProductCategoryController { @GetMapping @ApiOperation("获得指定编号下的子分类的数组") - @ApiImplicitParam(name = "pid", value = "指定分类编号", required = true) + @ApiImplicitParam(name = "pid", value = "指定分类编号", required = true, example = "0") public List list(@RequestParam("pid") Integer pid) { return ProductCategoryConvert.INSTANCE.convertToVO( productCategoryService.getListByPid(pid) diff --git a/product/product-application/src/main/resources/application.yaml b/product/product-application/src/main/resources/application.yaml index 8c775d3f..a200bc9f 100644 --- a/product/product-application/src/main/resources/application.yaml +++ b/product/product-application/src/main/resources/application.yaml @@ -10,7 +10,7 @@ spring: # server server: - port: 8080 + port: 8081 # mybatis mybatis: diff --git a/user/user-application/pom.xml b/user/user-application/pom.xml index 8e0070b1..847b2ba3 100644 --- a/user/user-application/pom.xml +++ b/user/user-application/pom.xml @@ -68,7 +68,6 @@ ${org.mapstruct.version} - io.springfox springfox-swagger2 @@ -79,6 +78,7 @@ springfox-swagger-ui 2.9.2 + cn.iocoder.mall user-service-api diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/UserApplication.java b/user/user-application/src/main/java/cn/iocoder/mall/user/UserApplication.java index 894d4475..c915336d 100644 --- a/user/user-application/src/main/java/cn/iocoder/mall/user/UserApplication.java +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/UserApplication.java @@ -3,7 +3,7 @@ package cn.iocoder.mall.user; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -@SpringBootApplication(scanBasePackages = "cn.iocoder.mall.user") +@SpringBootApplication(scanBasePackages = {"cn.iocoder.mall.user"}) public class UserApplication { public static void main(String[] args) { diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/config/MVCConfiguration.java b/user/user-application/src/main/java/cn/iocoder/mall/user/config/MVCConfiguration.java index 4527c4f9..653579f7 100644 --- a/user/user-application/src/main/java/cn/iocoder/mall/user/config/MVCConfiguration.java +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/config/MVCConfiguration.java @@ -1,18 +1,18 @@ package cn.iocoder.mall.user.config; import cn.iocoder.common.framework.config.GlobalExceptionHandler; -import cn.iocoder.common.framework.config.GlobalResponseBodyAdvice; import cn.iocoder.mall.user.sdk.interceptor.SecurityInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @EnableWebMvc @Configuration -@Import(value = {GlobalResponseBodyAdvice.class, GlobalExceptionHandler.class, // 统一全局返回 +@Import(value = {GlobalExceptionHandler.class, // 统一全局返回 SecurityInterceptor.class}) // 安全拦截器,实现认证和授权功能。 public class MVCConfiguration implements WebMvcConfigurer { @@ -21,7 +21,14 @@ public class MVCConfiguration implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(securityInterceptor); +// registry.addInterceptor(securityInterceptor).addPathPatterns("/user/**", "/admin/**"); // 只拦截我们定义的接口 + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + // 解决 swagger-ui.html 的访问,参考自 https://stackoverflow.com/questions/43545540/swagger-ui-no-mapping-found-for-http-request 解决 + registry.addResourceHandler("swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html"); + registry.addResourceHandler("webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } } \ No newline at end of file diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/config/SwaggerConfiguration.java b/user/user-application/src/main/java/cn/iocoder/mall/user/config/SwaggerConfiguration.java new file mode 100644 index 00000000..967f24e4 --- /dev/null +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/config/SwaggerConfiguration.java @@ -0,0 +1,36 @@ +package cn.iocoder.mall.user.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@Configuration +@EnableSwagger2 +public class SwaggerConfiguration { + + @Bean + public Docket createRestApi() { + return new Docket(DocumentationType.SWAGGER_2) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("cn.iocoder.mall.user.controller")) + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title("用户子系统") + .description("用户子系统") + .termsOfServiceUrl("http://www.iocoder.cn") + .version("1.0.0") + .build(); + } + +} \ No newline at end of file diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/controller/PassportController.java b/user/user-application/src/main/java/cn/iocoder/mall/user/controller/PassportController.java index d8406f93..b97ed44d 100644 --- a/user/user-application/src/main/java/cn/iocoder/mall/user/controller/PassportController.java +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/controller/PassportController.java @@ -1,22 +1,24 @@ package cn.iocoder.mall.user.controller; -import cn.iocoder.common.framework.exception.ServiceException; -import cn.iocoder.common.framework.util.ExceptionUtil; import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.user.convert.PassportConvert; import cn.iocoder.mall.user.sdk.annotation.PermitAll; import cn.iocoder.mall.user.service.api.MobileCodeService; import cn.iocoder.mall.user.service.api.OAuth2Service; import cn.iocoder.mall.user.service.api.UserService; import cn.iocoder.mall.user.service.api.bo.OAuth2AccessTokenBO; -import cn.iocoder.mall.user.service.api.constant.UserErrorCodeEnum; +import cn.iocoder.mall.user.vo.MobileRegisterVO; import com.alibaba.dubbo.config.annotation.Reference; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; 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; @RestController -@RequestMapping("/passport") +@RequestMapping("user/passport") public class PassportController { @Reference @@ -33,63 +35,17 @@ public class PassportController { // return oauth2Service.getAccessToken(clientId, clientSecret, mobile, password); // } - /** - * 手机号 + 验证码登陆 - * - * @see #mobileRegister2(String, String) 使用替代 - * - * @param mobile 手机号 - * @param code 验证码 - * @return 授权信息 - */ - @Deprecated @PermitAll - @PostMapping("/mobile/login") - public OAuth2AccessTokenBO mobileRegister(@RequestParam("mobile") String mobile, - @RequestParam("code") String code) { - // 尝试直接授权 - OAuth2AccessTokenBO accessTokenDTO; - try { - accessTokenDTO = oauth2Service.getAccessToken(mobile, code); - return accessTokenDTO; - } catch (Exception ex) { - ServiceException serviceException = ExceptionUtil.getServiceException(ex); - if (serviceException == null) { - throw ex; - } - if (!serviceException.getCode().equals(UserErrorCodeEnum.USER_MOBILE_NOT_REGISTERED.getCode())) { // 如果是未注册异常,忽略。下面发起自动注册逻辑。 - throw serviceException; - } - } - // 上面尝试授权失败,说明用户未注册,发起自动注册。 - try { - userService.createUser(mobile, code); - } catch (Exception ex) { - ServiceException serviceException = ExceptionUtil.getServiceException(ex); - if (serviceException == null) { - throw ex; - } - if (!serviceException.getCode().equals(UserErrorCodeEnum.USER_MOBILE_ALREADY_REGISTERED.getCode())) { // 如果是已注册异常,忽略。下面再次发起授权 - throw serviceException; - } - } - // 再次发起授权 - accessTokenDTO = oauth2Service.getAccessToken(mobile, code); - return accessTokenDTO; - } - - /** - * 手机号 + 验证码登陆 - * - * @param mobile 手机号 - * @param code 验证码 - * @return 授权信息 - */ - @PermitAll - @PostMapping("/mobile/login2") - public CommonResult mobileRegister2(@RequestParam("mobile") String mobile, - @RequestParam("code") String code) { - return oauth2Service.getAccessToken2(mobile, code); + @PostMapping("/mobile/register") + @ApiOperation(value = "手机号 + 验证码登陆(注册)", notes = "如果手机对应的账号不存在,则会自动创建") + @ApiImplicitParams({ + @ApiImplicitParam(name = "mobile", value = "手机号", required = true, example = "15601691300"), + @ApiImplicitParam(name = "code", value = "验证码", required = true, example = "9999") + }) + public CommonResult mobileRegister(@RequestParam("mobile") String mobile, + @RequestParam("code") String code) { + CommonResult result = oauth2Service.getAccessToken2(mobile, code); + return PassportConvert.INSTANCE.convert(result); } /** diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/convert/PassportConvert.java b/user/user-application/src/main/java/cn/iocoder/mall/user/convert/PassportConvert.java new file mode 100644 index 00000000..9ece6786 --- /dev/null +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/convert/PassportConvert.java @@ -0,0 +1,21 @@ +package cn.iocoder.mall.user.convert; + +import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.user.service.api.bo.OAuth2AccessTokenBO; +import cn.iocoder.mall.user.vo.MobileRegisterVO; +import org.mapstruct.Mapper; +import org.mapstruct.Mappings; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface PassportConvert { + + PassportConvert INSTANCE = Mappers.getMapper(PassportConvert.class); + + @Mappings({}) + MobileRegisterVO convert(OAuth2AccessTokenBO oauth2AccessTokenBO); + + @Mappings({}) + CommonResult convert(CommonResult oauth2AccessTokenBO); + +} \ No newline at end of file diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/vo/MobileRegisterVO.java b/user/user-application/src/main/java/cn/iocoder/mall/user/vo/MobileRegisterVO.java new file mode 100644 index 00000000..f0f960f9 --- /dev/null +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/vo/MobileRegisterVO.java @@ -0,0 +1,43 @@ +package cn.iocoder.mall.user.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel("手机注册结果VO") +public class MobileRegisterVO { + + @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; + + public String getAccessToken() { + return accessToken; + } + + public MobileRegisterVO setAccessToken(String accessToken) { + this.accessToken = accessToken; + return this; + } + + public String getRefreshToken() { + return refreshToken; + } + + public MobileRegisterVO setRefreshToken(String refreshToken) { + this.refreshToken = refreshToken; + return this; + } + + public Integer getExpiresIn() { + return expiresIn; + } + + public MobileRegisterVO setExpiresIn(Integer expiresIn) { + this.expiresIn = expiresIn; + return this; + } + +} \ No newline at end of file