From 10c196326d632bab9e2e6596d8a777d2df4bed3a Mon Sep 17 00:00:00 2001 From: Gary Date: Thu, 11 Jan 2024 19:51:28 +0800 Subject: [PATCH 1/2] 1.add swagger for user --- .../src/main/java/entity/ResultVO.java | 4 +- luoo_user/pom.xml | 21 +++++++ .../java/com/luoo/user/UserApplication.java | 28 +++++++++- .../luoo/user/config/InterceptorConfig.java | 14 ++++- .../luoo/user/swagger/BaseSwaggerConfig.java | 55 +++++++++++++++++++ .../com/luoo/user/swagger/SwaggerConfig.java | 25 +++++++++ .../luoo/user/swagger/SwaggerProperties.java | 47 ++++++++++++++++ .../main/java/com/luoo/user/vo/UserVO.java | 17 ++++++ 8 files changed, 205 insertions(+), 6 deletions(-) create mode 100644 luoo_user/src/main/java/com/luoo/user/swagger/BaseSwaggerConfig.java create mode 100644 luoo_user/src/main/java/com/luoo/user/swagger/SwaggerConfig.java create mode 100644 luoo_user/src/main/java/com/luoo/user/swagger/SwaggerProperties.java create mode 100644 luoo_user/src/main/java/com/luoo/user/vo/UserVO.java diff --git a/luoo_common/src/main/java/entity/ResultVO.java b/luoo_common/src/main/java/entity/ResultVO.java index 0a3c56d..0bbb950 100644 --- a/luoo_common/src/main/java/entity/ResultVO.java +++ b/luoo_common/src/main/java/entity/ResultVO.java @@ -44,14 +44,14 @@ public class ResultVO{ public void setMessage(String message) { this.message = message; } - public Object getData() { + public T getData() { return data; } public void setData(T data) { this.data = data; } - public static ResultVO success(E data){ + public static ResultVO success(T data){ return new ResultVO<>(true,200,"操作成功", data); } diff --git a/luoo_user/pom.xml b/luoo_user/pom.xml index b45760d..11d1653 100644 --- a/luoo_user/pom.xml +++ b/luoo_user/pom.xml @@ -56,6 +56,27 @@ org.springframework.boot spring-boot-starter-actuator + + org.projectlombok + lombok + true + + + + io.springfox + springfox-swagger2 + 2.9.2 + + + com.github.xiaoymin + swagger-bootstrap-ui + 1.9.3 + + + com.apifan.common + common-random + 1.0.21 + app diff --git a/luoo_user/src/main/java/com/luoo/user/UserApplication.java b/luoo_user/src/main/java/com/luoo/user/UserApplication.java index e08d94b..65c07ee 100644 --- a/luoo_user/src/main/java/com/luoo/user/UserApplication.java +++ b/luoo_user/src/main/java/com/luoo/user/UserApplication.java @@ -1,18 +1,40 @@ package com.luoo.user; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.Environment; import org.springframework.context.annotation.Bean; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import util.IdWorker; import util.JwtUtil; @SpringBootApplication @EnableEurekaClient +@EnableJpaAuditing public class UserApplication { - - public static void main(String[] args) { - SpringApplication.run(UserApplication.class, args); + static Logger logger= LoggerFactory.getLogger(UserApplication.class); + public static void main(String[] args) throws UnknownHostException { + ConfigurableApplicationContext application=SpringApplication.run(UserApplication.class, args); + Environment env = application.getEnvironment(); + logger.info("\n----------------------------------------------------------\n\t" + + "Application '{}' is running! Access URLs:\n\t" + + "Local: \t\thttp://localhost:{}\n\t" + + "External: \thttp://{}:{}\n\t"+ + "Doc: \thttp://{}:{}/doc.html\n"+ + "----------------------------------------------------------", + env.getProperty("spring.application.name"), + env.getProperty("server.port"), + InetAddress.getLocalHost().getHostAddress(), + env.getProperty("server.port"), + InetAddress.getLocalHost().getHostAddress(), + env.getProperty("server.port")); } @Bean diff --git a/luoo_user/src/main/java/com/luoo/user/config/InterceptorConfig.java b/luoo_user/src/main/java/com/luoo/user/config/InterceptorConfig.java index ae086f8..f95541b 100644 --- a/luoo_user/src/main/java/com/luoo/user/config/InterceptorConfig.java +++ b/luoo_user/src/main/java/com/luoo/user/config/InterceptorConfig.java @@ -4,6 +4,7 @@ import com.luoo.user.interceptor.JwtInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration @@ -12,8 +13,19 @@ public class InterceptorConfig extends WebMvcConfigurationSupport { @Autowired private JwtInterceptor jwtInterceptor; protected void addInterceptors(InterceptorRegistry registry) { + String[] excludePathPatterns = { "/user/login/**","/user/appLogin/**","/user/sendsms/**","/doc.html/**","/swagger-resources/**","/webjars/**","/v2/**"}; + registry.addInterceptor(jwtInterceptor). addPathPatterns("/**"). - excludePathPatterns("/**/login/**"); + excludePathPatterns(excludePathPatterns); + } + + @Override + protected void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("doc.html") + .addResourceLocations("classpath:/META-INF/resources/"); + registry.addResourceHandler("/webjars/**") + .addResourceLocations("classpath:/META-INF/resources/webjars/"); + super.addResourceHandlers(registry); } } diff --git a/luoo_user/src/main/java/com/luoo/user/swagger/BaseSwaggerConfig.java b/luoo_user/src/main/java/com/luoo/user/swagger/BaseSwaggerConfig.java new file mode 100644 index 0000000..f9f6bd7 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/swagger/BaseSwaggerConfig.java @@ -0,0 +1,55 @@ +package com.luoo.user.swagger; + +import org.springframework.context.annotation.Bean; +import org.springframework.core.annotation.Order; +import org.springframework.web.bind.annotation.RequestMethod; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.builders.ResponseMessageBuilder; +import springfox.documentation.schema.ModelRef; +import springfox.documentation.service.*; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import java.util.ArrayList; +import java.util.List; + +public abstract class BaseSwaggerConfig { + + @Bean + @Order(value = 1) + public Docket createRestApi() { + List responseMessageList =new ArrayList<>(); + responseMessageList.add(new ResponseMessageBuilder().code(200).message("成功").responseModel(new ModelRef("成功")).build()); + responseMessageList.add(new ResponseMessageBuilder().code(20001).message("失败").responseModel(new ModelRef("失败")).build()); + responseMessageList.add(new ResponseMessageBuilder().code(20002).message("用户名或密码错误").responseModel(new ModelRef("用户名或密码错误")).build()); + responseMessageList.add(new ResponseMessageBuilder().code(20003).message("无权访问").responseModel(new ModelRef("无权访问")).build()); + responseMessageList.add(new ResponseMessageBuilder().code(20009).message("重复提交").responseModel(new ModelRef("重复提交")).build()); + SwaggerProperties swaggerProperties = swaggerProperties(); + return new Docket(DocumentationType.SWAGGER_2) + .globalResponseMessage(RequestMethod.GET, responseMessageList) + .globalResponseMessage(RequestMethod.POST, responseMessageList) + .globalResponseMessage(RequestMethod.PUT, responseMessageList) + .globalResponseMessage(RequestMethod.DELETE, responseMessageList) + .apiInfo(apiInfo(swaggerProperties)) + .select() + .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getApiBasePackage())) + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo(SwaggerProperties swaggerProperties) { + return new ApiInfoBuilder() + .title(swaggerProperties.getTitle()) + .description(swaggerProperties.getDescription()) + .contact(new Contact(swaggerProperties.getContactName(), swaggerProperties.getContactUrl(), swaggerProperties.getContactEmail())) + .version(swaggerProperties.getVersion()) + .build(); + } + + /** + * 自定义Swagger配置 + */ + public abstract SwaggerProperties swaggerProperties(); + +} diff --git a/luoo_user/src/main/java/com/luoo/user/swagger/SwaggerConfig.java b/luoo_user/src/main/java/com/luoo/user/swagger/SwaggerConfig.java new file mode 100644 index 0000000..7023726 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/swagger/SwaggerConfig.java @@ -0,0 +1,25 @@ +package com.luoo.user.swagger; + +import org.springframework.context.annotation.Configuration; + +import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; + +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@Configuration +@EnableSwagger2 +@EnableSwaggerBootstrapUI +public class SwaggerConfig extends BaseSwaggerConfig { + + @Override + public SwaggerProperties swaggerProperties() { + return SwaggerProperties.builder() + .apiBasePackage("com.luoo.user.controller") + .title("luoo-user API") + .description("luoo-user 后端接口文档") + .contactName("何长庚") + .version("1.0") + .enableSecurity(false) + .build(); + } +} diff --git a/luoo_user/src/main/java/com/luoo/user/swagger/SwaggerProperties.java b/luoo_user/src/main/java/com/luoo/user/swagger/SwaggerProperties.java new file mode 100644 index 0000000..3535e0b --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/swagger/SwaggerProperties.java @@ -0,0 +1,47 @@ +package com.luoo.user.swagger; + +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * Swagger自定义配置 + * Created by macro on 2020/7/16. + */ +@Data +@EqualsAndHashCode(callSuper = false) +@Builder +public class SwaggerProperties { + /** + * API文档生成基础路径 + */ + private String apiBasePackage; + /** + * 是否要启用登录认证 + */ + private boolean enableSecurity; + /** + * 文档标题 + */ + private String title; + /** + * 文档描述 + */ + private String description; + /** + * 文档版本 + */ + private String version; + /** + * 文档联系人姓名 + */ + private String contactName; + /** + * 文档联系人网址 + */ + private String contactUrl; + /** + * 文档联系人邮箱 + */ + private String contactEmail; +} diff --git a/luoo_user/src/main/java/com/luoo/user/vo/UserVO.java b/luoo_user/src/main/java/com/luoo/user/vo/UserVO.java new file mode 100644 index 0000000..849d7fb --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/vo/UserVO.java @@ -0,0 +1,17 @@ +package com.luoo.user.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +@Data +@ApiModel(value = "用户") +public class UserVO { + @ApiModelProperty(value = "ID") + private String id;//ID + @ApiModelProperty(value = "昵称",notes="初次登录,随机生成") + private String nickname;//昵称 + @ApiModelProperty(value = "头像",notes="初次登录,默认头像") + private String avatar;//头像 + @ApiModelProperty(value = "TOKEN") + private String token; +} From 878b6f1dc5cec8f08d48c5fb860adf9895aa98a6 Mon Sep 17 00:00:00 2001 From: hechanggeng Date: Thu, 11 Jan 2024 20:26:58 +0800 Subject: [PATCH 2/2] 1.fix swagger version issue --- luoo_user/pom.xml | 2 +- .../luoo/user/controller/UserController.java | 32 +++++++++---------- .../com/luoo/user/service/UserService.java | 22 ++++++++++++- .../luoo/user/swagger/BaseSwaggerConfig.java | 3 ++ 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/luoo_user/pom.xml b/luoo_user/pom.xml index 11d1653..144d112 100644 --- a/luoo_user/pom.xml +++ b/luoo_user/pom.xml @@ -65,7 +65,7 @@ io.springfox springfox-swagger2 - 2.9.2 + 2.8.0 com.github.xiaoymin diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java index fb11141..0c2de6e 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java @@ -16,10 +16,14 @@ import org.springframework.web.bind.annotation.RestController; import com.luoo.user.pojo.User; import com.luoo.user.service.UserService; +import com.luoo.user.vo.UserVO; import entity.PageResult; import entity.Result; +import entity.ResultVO; import entity.StatusCode; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import util.IdWorker; import util.JwtUtil; @@ -31,6 +35,7 @@ import util.JwtUtil; @RestController @CrossOrigin @RequestMapping("/user") +@Api(tags="UserController") public class UserController { @Autowired @@ -73,37 +78,30 @@ public class UserController { return new Result(true,StatusCode.OK,"登录成功",map); } - + @ApiOperation(value="2.登录/注册后返回token") @RequestMapping(value = "/appLogin/{mobile}/{checkcode}",method = RequestMethod.POST) - public Result appLogin(@PathVariable String mobile,@PathVariable String checkcode){ - + public ResultVO appLogin(@PathVariable String mobile,@PathVariable String checkcode){ // 得到缓存中的验证码 String checkcodeRedis = (String)redisTemplate.opsForValue().get("checkcode_"+mobile); if (checkcodeRedis.isEmpty()){ - return new Result(false,StatusCode.ERROR,"请先获取手机验证码"); + return new ResultVO(false,StatusCode.ERROR,"请先获取手机验证码"); } if (!checkcodeRedis.equals(checkcode)) { - return new Result(false,StatusCode.ERROR,"请输入正确的验证码"); + return new ResultVO(false,StatusCode.ERROR,"请输入正确的验证码"); } - - User user = userService.findByMobile(mobile); - // 如果数据库中有这个手机号,登录通道, - if (user != null){ - String token = jwtUtil.createJWT(user.getId(),user.getMobile(),"user"); - } - // 如果数据库中没有这个手机号,为新用户,注册添加用户 - - return new Result(true,StatusCode.OK,"登录成功");} + UserVO userVO=userService.loginOrRegister(mobile); + return ResultVO.success(userVO); + } /** * 发送短信验证码 */ - + @ApiOperation(value="1.发送短信验证码") @RequestMapping(value = "/sendsms/{mobile}",method = RequestMethod.POST) - public Result sendSms(@PathVariable String mobile) { + public ResultVO sendSms(@PathVariable String mobile) { userService.sendSms(mobile); - return new Result(true,StatusCode.OK,"发送成功"); + return ResultVO.success("发送成功"); } @RequestMapping(value = "/register/{code}",method = RequestMethod.POST) diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserService.java b/luoo_user/src/main/java/com/luoo/user/service/UserService.java index 8526912..12d2fa7 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserService.java @@ -17,6 +17,7 @@ import io.jsonwebtoken.Claims; import org.apache.commons.lang3.RandomStringUtils; import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -29,8 +30,11 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import util.IdWorker; +import com.apifan.common.random.RandomSource; import com.luoo.user.dao.UserDao; import com.luoo.user.pojo.User; +import com.luoo.user.vo.UserVO; + import util.JwtUtil; /** @@ -249,7 +253,23 @@ public class UserService { userDao.updatefanscount(x,friendid); userDao.updatefollowcount(x,userid); } - + public UserVO loginOrRegister(String mobile) { + User user = userDao.findByMobile(mobile); + if(null==user) { + user=new User(); + user.setId(String.valueOf(idWorker.nextId())); + user.setMobile(mobile); + user.setNickname(RandomSource.personInfoSource().randomChineseNickName(8)); + user.setAvatar("default"); + userDao.save(user); + } + + UserVO userVO=new UserVO(); + BeanUtils.copyProperties(user, userVO); + String token = jwtUtil.createJWT(user.getId(),user.getMobile(),"user"); + userVO.setToken(token); + return userVO; + } public User findByMobile(String mobile) { User user = userDao.findByMobile(mobile); return user; diff --git a/luoo_user/src/main/java/com/luoo/user/swagger/BaseSwaggerConfig.java b/luoo_user/src/main/java/com/luoo/user/swagger/BaseSwaggerConfig.java index f9f6bd7..2ed11b5 100644 --- a/luoo_user/src/main/java/com/luoo/user/swagger/BaseSwaggerConfig.java +++ b/luoo_user/src/main/java/com/luoo/user/swagger/BaseSwaggerConfig.java @@ -3,6 +3,8 @@ package com.luoo.user.swagger; import org.springframework.context.annotation.Bean; import org.springframework.core.annotation.Order; import org.springframework.web.bind.annotation.RequestMethod; + +import io.swagger.annotations.ApiOperation; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; @@ -34,6 +36,7 @@ public abstract class BaseSwaggerConfig { .apiInfo(apiInfo(swaggerProperties)) .select() .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getApiBasePackage())) + //.apis(RequestHandlerSelectors.withClassAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); }