From 1168b46560e7b6420b53859596e3c4b89d98e539 Mon Sep 17 00:00:00 2001 From: zhouyixun <291028775@qq.com> Date: Wed, 13 Oct 2021 23:47:56 +0800 Subject: [PATCH 1/2] save_project_api_token_user --- config/sonic-server-gateway-dev.yml | 3 +- .../common/config/APIDocumentConfig.java | 10 +-- .../common/config/GlobalWebException.java | 6 +- .../sonic/common/config/WebAspectConfig.java | 2 +- ...CronException.java => SonicException.java} | 4 +- .../com/sonic/common/tools/JWTTokenTool.java | 15 ---- .../controller/ProjectsController.java | 10 +++ .../controller/TestCasesController.java | 10 ++- .../controller/TestSuitesController.java | 10 ++- .../controller/UsersController.java | 76 ++++++++++++++++ .../sonic/controller/dao/UsersRepository.java | 8 ++ .../com/sonic/controller/models/Users.java | 21 +++-- .../controller/models/http/ChangePwd.java | 36 ++++++++ .../controller/models/http/UserInfo.java | 10 +-- .../controller/services/ProjectsService.java | 3 + .../controller/services/UsersService.java | 17 ++++ .../services/impl/ProjectsServiceImpl.java | 48 +++++++++- .../services/impl/TestSuitesServiceImpl.java | 6 +- .../services/impl/UsersServiceImpl.java | 90 +++++++++++++++++++ .../com/sonic/gateway/config/AuthFilter.java | 34 ++----- .../sonic/task/controller/JobsController.java | 6 +- .../java/com/sonic/task/quartz/QuartzJob.java | 7 +- .../com/sonic/task/service/JobsService.java | 4 +- .../task/service/impl/JobsServiceImpl.java | 8 +- 24 files changed, 353 insertions(+), 91 deletions(-) rename sonic-server-common/src/main/java/com/sonic/common/exception/{SonicCronException.java => SonicException.java} (54%) create mode 100644 sonic-server-controller/src/main/java/com/sonic/controller/controller/UsersController.java create mode 100644 sonic-server-controller/src/main/java/com/sonic/controller/dao/UsersRepository.java create mode 100644 sonic-server-controller/src/main/java/com/sonic/controller/models/http/ChangePwd.java create mode 100644 sonic-server-controller/src/main/java/com/sonic/controller/services/UsersService.java create mode 100644 sonic-server-controller/src/main/java/com/sonic/controller/services/impl/UsersServiceImpl.java diff --git a/config/sonic-server-gateway-dev.yml b/config/sonic-server-gateway-dev.yml index 7392d0ab..f06df4ae 100644 --- a/config/sonic-server-gateway-dev.yml +++ b/config/sonic-server-gateway-dev.yml @@ -2,7 +2,8 @@ server: port: 8094 filter: - white-list: /swagger-resources,/v2/api-docs,/folder/upload,/folder/recordFiles,/keepFiles/,/imageFiles/,/recordFiles/,/logFiles/ + white-list: /users/login,/users/register,/swagger-resources,/v2/api-docs,/folder/upload,/folder/recordFiles,/keepFiles/,/imageFiles/,/recordFiles/,/logFiles/ + resetToken: true spring: cloud: diff --git a/sonic-server-common/src/main/java/com/sonic/common/config/APIDocumentConfig.java b/sonic-server-common/src/main/java/com/sonic/common/config/APIDocumentConfig.java index 7fe73a71..549fc5f9 100644 --- a/sonic-server-common/src/main/java/com/sonic/common/config/APIDocumentConfig.java +++ b/sonic-server-common/src/main/java/com/sonic/common/config/APIDocumentConfig.java @@ -69,7 +69,7 @@ public Docket createRestAPIDocket() { .extensions(openApiExtensionResolver.buildSettingExtensions()) .useDefaultResponseMessages(false) .securitySchemes(Arrays.asList( - new ApiKey("sonicToken", "sonicToken", "header"))) + new ApiKey("SonicToken", "SonicToken", "header"))) .securityContexts(securityContexts()); } @@ -83,7 +83,7 @@ private List securityContexts() { return Arrays.asList( SecurityContext.builder() .securityReferences(auth()) - .forPaths(PathSelectors.regex("^((?!(login)).)*$")) + .forPaths(PathSelectors.regex("^((?!(register|login)).)*$")) .build() ); } @@ -91,7 +91,7 @@ private List securityContexts() { /** * @return java.util.List * @author ZhouYiXun - * @des 设置sonicToken在接口文档页面 + * @des 设置SonicToken在接口文档页面 * @date 2021/8/15 22:54 */ private List auth() { @@ -99,7 +99,7 @@ private List auth() { AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; return Arrays.asList( - new SecurityReference("sonicToken", authorizationScopes)); + new SecurityReference("SonicToken", authorizationScopes)); } /** @@ -112,7 +112,7 @@ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(title + " Open API") .termsOfServiceUrl("Please visit: https://github.com/ZhouYixun/sonic-server") - .contact(new Contact("ZhouYiXun email: zyx291028775@qq.com","", "")) + .contact(new Contact("ZhouYiXun email: zyx291028775@qq.com", "", "")) .version(version) .description(name + " 服务开放API,注意不要频繁请求。") .build(); diff --git a/sonic-server-common/src/main/java/com/sonic/common/config/GlobalWebException.java b/sonic-server-common/src/main/java/com/sonic/common/config/GlobalWebException.java index ca6f1362..4e68bf5a 100644 --- a/sonic-server-common/src/main/java/com/sonic/common/config/GlobalWebException.java +++ b/sonic-server-common/src/main/java/com/sonic/common/config/GlobalWebException.java @@ -1,6 +1,6 @@ package com.sonic.common.config; -import com.sonic.common.exception.SonicCronException; +import com.sonic.common.exception.SonicException; import com.sonic.common.http.RespEnum; import com.sonic.common.http.RespModel; import org.slf4j.Logger; @@ -33,8 +33,8 @@ public RespModel ErrHandler(Exception exception) { return new RespModel(RespEnum.PARAMS_NOT_VALID); } else if (exception instanceof HttpMessageNotReadableException) { return new RespModel(RespEnum.PARAMS_NOT_READABLE); - } else if (exception instanceof SonicCronException) { - return new RespModel(4006,exception.getMessage()); + } else if (exception instanceof SonicException) { + return new RespModel(4006, exception.getMessage()); } else { exception.printStackTrace(); return new RespModel(RespEnum.UNKNOWN_ERROR); diff --git a/sonic-server-common/src/main/java/com/sonic/common/config/WebAspectConfig.java b/sonic-server-common/src/main/java/com/sonic/common/config/WebAspectConfig.java index abd8681d..6abce897 100644 --- a/sonic-server-common/src/main/java/com/sonic/common/config/WebAspectConfig.java +++ b/sonic-server-common/src/main/java/com/sonic/common/config/WebAspectConfig.java @@ -47,7 +47,7 @@ public void deBefore(JoinPoint joinPoint) throws Throwable { JSONObject jsonObject = new JSONObject(); jsonObject.put("url", request.getRequestURL().toString()); jsonObject.put("method", request.getMethod()); - jsonObject.put("auth", request.getHeader("sonicToken")); + jsonObject.put("auth", request.getHeader("SonicToken")); jsonObject.put("class", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); jsonObject.put("request", Arrays.toString(joinPoint.getArgs())); logger.info(jsonObject.toJSONString()); diff --git a/sonic-server-common/src/main/java/com/sonic/common/exception/SonicCronException.java b/sonic-server-common/src/main/java/com/sonic/common/exception/SonicException.java similarity index 54% rename from sonic-server-common/src/main/java/com/sonic/common/exception/SonicCronException.java rename to sonic-server-common/src/main/java/com/sonic/common/exception/SonicException.java index ca1c1355..e180c700 100644 --- a/sonic-server-common/src/main/java/com/sonic/common/exception/SonicCronException.java +++ b/sonic-server-common/src/main/java/com/sonic/common/exception/SonicException.java @@ -5,8 +5,8 @@ * @des * @date 2021/10/10 11:57 */ -public class SonicCronException extends Exception { - public SonicCronException(String message) { +public class SonicException extends Exception { + public SonicException(String message) { super(message); } } diff --git a/sonic-server-common/src/main/java/com/sonic/common/tools/JWTTokenTool.java b/sonic-server-common/src/main/java/com/sonic/common/tools/JWTTokenTool.java index e5acaa15..f14e8da1 100644 --- a/sonic-server-common/src/main/java/com/sonic/common/tools/JWTTokenTool.java +++ b/sonic-server-common/src/main/java/com/sonic/common/tools/JWTTokenTool.java @@ -28,21 +28,6 @@ public static String getToken(String username) { .sign(Algorithm.HMAC256(TOKEN_SECRET)); } - /** - * @param token - * @return java.lang.String - * @author ZhouYiXun - * @des 由token获取生成时的用户信息 - * @date 2021/8/15 23:05 - */ - public static String getUserName(String token) { - try { - return JWT.decode(token).getAudience().get(0); - } catch (JWTDecodeException e) { - return null; - } - } - /** * @param token * @return boolean diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/controller/ProjectsController.java b/sonic-server-controller/src/main/java/com/sonic/controller/controller/ProjectsController.java index a9ef9731..91a812bb 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/controller/ProjectsController.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/controller/ProjectsController.java @@ -1,6 +1,7 @@ package com.sonic.controller.controller; import com.sonic.common.config.WebAspect; +import com.sonic.common.exception.SonicException; import com.sonic.common.http.RespEnum; import com.sonic.common.http.RespModel; import com.sonic.controller.models.Projects; @@ -53,4 +54,13 @@ public RespModel findById(@RequestParam(name = "id") int id) { return new RespModel(RespEnum.ID_NOT_FOUND); } } + + @WebAspect + @ApiOperation(value = "删除", notes = "删除对应id下的详细信息") + @ApiImplicitParam(name = "id", value = "项目id", dataTypeClass = Integer.class) + @DeleteMapping + public RespModel delete(@RequestParam(name = "id") int id) throws SonicException { + projectsService.delete(id); + return new RespModel(RespEnum.DELETE_OK); + } } diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/controller/TestCasesController.java b/sonic-server-controller/src/main/java/com/sonic/controller/controller/TestCasesController.java index a817f619..4207d8bc 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/controller/TestCasesController.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/controller/TestCasesController.java @@ -5,6 +5,7 @@ import com.sonic.common.http.RespEnum; import com.sonic.common.http.RespModel; import com.sonic.controller.models.TestCases; +import com.sonic.controller.models.Users; import com.sonic.controller.services.TestCasesService; import com.sonic.controller.tools.RedisTool; import io.swagger.annotations.Api; @@ -78,10 +79,11 @@ public RespModel delete(@RequestParam(name = "id") int id) { @ApiOperation(value = "更新测试用例信息", notes = "新增或更改测试用例信息") @PutMapping public RespModel save(@Validated @RequestBody TestCases testCases, HttpServletRequest request) { - if (request.getHeader("sonicToken") != null) { - String token = request.getHeader("sonicToken"); - if (RedisTool.get("sonic:user:" + token) != null) { - String userName = RedisTool.get("sonic:user:" + token).toString(); + if (request.getHeader("SonicToken") != null) { + String token = request.getHeader("SonicToken"); + Object t = RedisTool.get("sonic:user:" + token); + if (t != null) { + String userName = ((Users) t).getUserName(); testCases.setDesigner(userName); } } diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/controller/TestSuitesController.java b/sonic-server-controller/src/main/java/com/sonic/controller/controller/TestSuitesController.java index cde55103..6f0e7dda 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/controller/TestSuitesController.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/controller/TestSuitesController.java @@ -4,6 +4,7 @@ import com.sonic.common.http.RespEnum; import com.sonic.common.http.RespModel; import com.sonic.controller.models.TestSuites; +import com.sonic.controller.models.Users; import com.sonic.controller.services.TestSuitesService; import com.sonic.controller.tools.RedisTool; import io.swagger.annotations.Api; @@ -35,10 +36,11 @@ public class TestSuitesController { public RespModel runSuite(@RequestParam(name = "id") int id , HttpServletRequest request) { String strike = "SYSTEM"; - if (request.getHeader("sonicToken") != null) { - String token = request.getHeader("sonicToken"); - if (RedisTool.get("sonic:user:" + token) != null) { - strike = RedisTool.get("sonic:user:" + token).toString(); + if (request.getHeader("SonicToken") != null) { + String token = request.getHeader("SonicToken"); + Object t = RedisTool.get("sonic:user:" + token); + if (t != null) { + strike = ((Users) t).getUserName(); } } return testSuitesService.runSuite(id, strike); diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/controller/UsersController.java b/sonic-server-controller/src/main/java/com/sonic/controller/controller/UsersController.java new file mode 100644 index 00000000..b809be7e --- /dev/null +++ b/sonic-server-controller/src/main/java/com/sonic/controller/controller/UsersController.java @@ -0,0 +1,76 @@ +package com.sonic.controller.controller; + +import com.sonic.common.config.WebAspect; +import com.sonic.common.exception.SonicException; +import com.sonic.common.http.RespEnum; +import com.sonic.common.http.RespModel; +import com.sonic.controller.models.Users; +import com.sonic.controller.models.Versions; +import com.sonic.controller.models.http.ChangePwd; +import com.sonic.controller.models.http.UserInfo; +import com.sonic.controller.services.UsersService; +import com.sonic.controller.tools.RedisTool; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; + +/** + * @author ZhouYiXun + * @des + * @date 2021/10/13 19:05 + */ +@Api(tags = "用户体系相关") +@RestController +@RequestMapping("/users") +public class UsersController { + @Autowired + private UsersService usersService; + + @WebAspect + @ApiOperation(value = "登录", notes = "用户登录") + @PostMapping("/login") + public RespModel login(@Validated @RequestBody UserInfo userInfo) { + String token = usersService.login(userInfo); + if (token != null) { + return new RespModel(2000, "登录成功!", token); + } else { + return new RespModel(2001, "登录失败!"); + } + } + + @WebAspect + @ApiOperation(value = "注册", notes = "注册用户") + @PostMapping("/register") + public RespModel register(@Validated @RequestBody Users users) throws SonicException { + usersService.register(users); + return new RespModel(2000, "注册成功!"); + } + + @WebAspect + @ApiOperation(value = "获取用户信息", notes = "获取token的用户信息") + @GetMapping + public RespModel getUserInfo(HttpServletRequest request) { + if (request.getHeader("SonicToken") != null) { + String token = request.getHeader("SonicToken"); + return new RespModel(RespEnum.SEARCH_OK, usersService.getUserInfo(token)); + } else { + return new RespModel(RespEnum.UNAUTHORIZED); + } + } + + @WebAspect + @ApiOperation(value = "修改密码", notes = "修改token的用户密码") + @PutMapping + public RespModel changePwd(HttpServletRequest request, @Validated @RequestBody ChangePwd changePwd) { + if (request.getHeader("SonicToken") != null) { + String token = request.getHeader("SonicToken"); + return usersService.resetPwd(token, changePwd); + } else { + return new RespModel(RespEnum.UNAUTHORIZED); + } + } +} diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/dao/UsersRepository.java b/sonic-server-controller/src/main/java/com/sonic/controller/dao/UsersRepository.java new file mode 100644 index 00000000..9a0ecdec --- /dev/null +++ b/sonic-server-controller/src/main/java/com/sonic/controller/dao/UsersRepository.java @@ -0,0 +1,8 @@ +package com.sonic.controller.dao; + +import com.sonic.controller.models.Users; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UsersRepository extends JpaRepository { + Users findByUserName(String username); +} diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/models/Users.java b/sonic-server-controller/src/main/java/com/sonic/controller/models/Users.java index 64416b9a..d19d27e8 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/models/Users.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/models/Users.java @@ -3,15 +3,13 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.persistence.*; import javax.validation.constraints.NotNull; import javax.validation.constraints.Positive; @Entity @ApiModel("用户模型") +@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"userName"})) public class Users { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -19,7 +17,7 @@ public class Users { int id; @NotNull @ApiModelProperty(value = "用户名称", required = true, example = "ZhouYiXun") - String username; + String userName; @NotNull @ApiModelProperty(value = "用户密码", required = true, example = "123456") String password; @@ -27,7 +25,8 @@ public class Users { @ApiModelProperty(value = "角色", required = true, example = "1") int role; - public Users(){} + public Users() { + } public int getId() { return id; @@ -37,12 +36,12 @@ public void setId(int id) { this.id = id; } - public String getUsername() { - return username; + public String getUserName() { + return userName; } - public void setUsername(String username) { - this.username = username; + public void setUserName(String userName) { + this.userName = userName; } public String getPassword() { @@ -65,7 +64,7 @@ public void setRole(int role) { public String toString() { return "Users{" + "id=" + id + - ", username='" + username + '\'' + + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", role=" + role + '}'; diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/models/http/ChangePwd.java b/sonic-server-controller/src/main/java/com/sonic/controller/models/http/ChangePwd.java new file mode 100644 index 00000000..bd7b66ed --- /dev/null +++ b/sonic-server-controller/src/main/java/com/sonic/controller/models/http/ChangePwd.java @@ -0,0 +1,36 @@ +package com.sonic.controller.models.http; + +import io.swagger.annotations.ApiModelProperty; + +import javax.validation.constraints.NotNull; + +/** + * @author ZhouYiXun + * @des + * @date 2021/10/13 18:45 + */ +public class ChangePwd { + @NotNull + @ApiModelProperty(value = "旧密码", required = true, example = "123456") + private String oldPwd; + + @NotNull + @ApiModelProperty(value = "新密码", required = true, example = "123456") + private String newPwd; + + public String getOldPwd() { + return oldPwd; + } + + public void setOldPwd(String oldPwd) { + this.oldPwd = oldPwd; + } + + public String getNewPwd() { + return newPwd; + } + + public void setNewPwd(String newPwd) { + this.newPwd = newPwd; + } +} diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/models/http/UserInfo.java b/sonic-server-controller/src/main/java/com/sonic/controller/models/http/UserInfo.java index c3351317..7c146cec 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/models/http/UserInfo.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/models/http/UserInfo.java @@ -9,18 +9,18 @@ public class UserInfo { @NotNull @ApiModelProperty(value = "账户名", required = true, example = "ZhouYiXun") - private String username; + private String userName; @NotNull @ApiModelProperty(value = "密码", required = true, example = "123456") private String password; - public String getUsername() { - return username; + public String getUserName() { + return userName; } - public void setUsername(String username) { - this.username = username; + public void setUserName(String userName) { + this.userName = userName; } public String getPassword() { diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/services/ProjectsService.java b/sonic-server-controller/src/main/java/com/sonic/controller/services/ProjectsService.java index 74465f44..86a95e24 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/services/ProjectsService.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/services/ProjectsService.java @@ -1,5 +1,6 @@ package com.sonic.controller.services; +import com.sonic.common.exception.SonicException; import com.sonic.controller.models.Projects; import java.util.List; @@ -15,4 +16,6 @@ public interface ProjectsService { Projects findById(int id); List findAll(); + + void delete(int id) throws SonicException; } diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/services/UsersService.java b/sonic-server-controller/src/main/java/com/sonic/controller/services/UsersService.java new file mode 100644 index 00000000..f4e41692 --- /dev/null +++ b/sonic-server-controller/src/main/java/com/sonic/controller/services/UsersService.java @@ -0,0 +1,17 @@ +package com.sonic.controller.services; + +import com.sonic.common.exception.SonicException; +import com.sonic.common.http.RespModel; +import com.sonic.controller.models.Users; +import com.sonic.controller.models.http.ChangePwd; +import com.sonic.controller.models.http.UserInfo; + +public interface UsersService { + void register(Users users) throws SonicException; + + String login(UserInfo userInfo); + + Users getUserInfo(String token); + + RespModel resetPwd(String token, ChangePwd changePwd); +} diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/ProjectsServiceImpl.java b/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/ProjectsServiceImpl.java index f6173dd0..8a03859c 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/ProjectsServiceImpl.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/ProjectsServiceImpl.java @@ -1,10 +1,13 @@ package com.sonic.controller.services.impl; -import com.sonic.controller.dao.ProjectsRepository; +import com.sonic.common.exception.SonicException; +import com.sonic.controller.dao.*; import com.sonic.controller.models.Projects; +import com.sonic.controller.models.Results; import com.sonic.controller.services.ProjectsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -17,6 +20,26 @@ public class ProjectsServiceImpl implements ProjectsService { @Autowired private ProjectsRepository projectsRepository; + @Autowired + private ElementsRepository elementsRepository; + @Autowired + private GlobalParamsRepository globalParamsRepository; + @Autowired + private ModulesRepository modulesRepository; + @Autowired + private VersionsRepository versionsRepository; + @Autowired + private PublicStepsRepository publicStepsRepository; + @Autowired + private ResultsRepository resultsRepository; + @Autowired + private ResultDetailRepository resultDetailRepository; + @Autowired + private StepsRepository stepsRepository; + @Autowired + private TestSuitesRepository testSuitesRepository; + @Autowired + private TestCasesRepository testCasesRepository; @Override public void save(Projects projects) { @@ -36,4 +59,27 @@ public Projects findById(int id) { public List findAll() { return projectsRepository.findAll(); } + + @Override + @Transactional(rollbackFor = SonicException.class) + public void delete(int id) throws SonicException { + try { + testSuitesRepository.deleteByProjectId(id); + publicStepsRepository.deleteByProjectId(id); + testCasesRepository.deleteByProjectId(id); + stepsRepository.deleteByProjectId(id); + elementsRepository.deleteByProjectId(id); + modulesRepository.deleteByProjectId(id); + globalParamsRepository.deleteByProjectId(id); + List resultsList = resultsRepository.findByProjectId(id); + for (Results results : resultsList) { + resultDetailRepository.deleteByResultId(results.getId()); + } + resultsRepository.deleteByProjectId(id); + versionsRepository.deleteByProjectId(id); + projectsRepository.deleteById(id); + } catch (Exception e) { + throw new SonicException("删除出错!请联系管理员!"); + } + } } diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/TestSuitesServiceImpl.java b/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/TestSuitesServiceImpl.java index aabb8910..e4dd4b79 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/TestSuitesServiceImpl.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/TestSuitesServiceImpl.java @@ -52,10 +52,10 @@ public RespModel runSuite(int suiteId, String strike) { if (testSuitesRepository.existsById(suiteId)) { testSuites = testSuitesRepository.findById(suiteId).get(); } else { - return new RespModel(3000, "测试套件已删除!"); + return new RespModel(3001, "测试套件已删除!"); } if (testSuites.getTestCases().size() == 0) { - return new RespModel(3000, "该测试套件内无测试用例!"); + return new RespModel(3002, "该测试套件内无测试用例!"); } List devicesList = new ArrayList<>(testSuites.getDevices()); for (int i = devicesList.size() - 1; i >= 0; i--) { @@ -64,7 +64,7 @@ public RespModel runSuite(int suiteId, String strike) { } } if (devicesList.size() == 0) { - return new RespModel(3000, "所选设备暂无可用!"); + return new RespModel(3003, "所选设备暂无可用!"); } Results results = new Results(); results.setStatus(ResultStatus.RUNNING); diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/UsersServiceImpl.java b/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/UsersServiceImpl.java new file mode 100644 index 00000000..d41d9e46 --- /dev/null +++ b/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/UsersServiceImpl.java @@ -0,0 +1,90 @@ +package com.sonic.controller.services.impl; + +import com.alibaba.fastjson.JSONObject; +import com.sonic.common.exception.SonicException; +import com.sonic.common.http.RespEnum; +import com.sonic.common.http.RespModel; +import com.sonic.common.tools.JWTTokenTool; +import com.sonic.controller.dao.UsersRepository; +import com.sonic.controller.models.Users; +import com.sonic.controller.models.http.ChangePwd; +import com.sonic.controller.models.http.UserInfo; +import com.sonic.controller.services.UsersService; +import com.sonic.controller.tools.RedisTool; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.DigestUtils; + +/** + * @author ZhouYiXun + * @des + * @date 2021/10/13 11:26 + */ +@Service +public class UsersServiceImpl implements UsersService { + private final Logger logger = LoggerFactory.getLogger(UsersServiceImpl.class); + @Autowired + private UsersRepository usersRepository; + @Autowired + private RedisTool redisTool; + + @Override + @Transactional(rollbackFor = SonicException.class) + public void register(Users users) throws SonicException { + try { + users.setPassword(DigestUtils.md5DigestAsHex(users.getPassword().getBytes())); + usersRepository.save(users); + } catch (Exception e) { + e.printStackTrace(); + throw new SonicException("注册失败!用户名已存在!"); + } + } + + @Override + public String login(UserInfo userInfo) { + Users users = usersRepository.findByUserName(userInfo.getUserName()); + if (DigestUtils.md5DigestAsHex(userInfo.getPassword().getBytes()).equals(users.getPassword())) { + String token = JWTTokenTool.getToken(userInfo.getUserName()); + users.setPassword(""); + redisTool.set("sonic:user:" + token, users, 7); + logger.info("用户:" + userInfo.getUserName() + "登入! token:" + token); + return token; + } else { + return null; + } + } + + @Override + public Users getUserInfo(String token) { + Object redisTokenObject = RedisTool.get("sonic:user:" + token); + if (redisTokenObject != null) { + return (Users) redisTokenObject; + } else { + return null; + } + } + + @Override + public RespModel resetPwd(String token, ChangePwd changePwd) { + Object redisTokenObject = RedisTool.get("sonic:user:" + token); + if (redisTokenObject != null) { + Users users = ((Users) redisTokenObject); + if (users != null) { + if (DigestUtils.md5DigestAsHex(changePwd.getOldPwd().getBytes()).equals(users.getPassword())) { + users.setPassword(DigestUtils.md5DigestAsHex(changePwd.getNewPwd().getBytes())); + usersRepository.save(users); + return new RespModel(2000, "修改密码成功!"); + } else { + return new RespModel(4001, "旧密码错误!"); + } + } else { + return new RespModel(RespEnum.UNAUTHORIZED); + } + } else { + return new RespModel(RespEnum.UNAUTHORIZED); + } + } +} diff --git a/sonic-server-gateway/src/main/java/com/sonic/gateway/config/AuthFilter.java b/sonic-server-gateway/src/main/java/com/sonic/gateway/config/AuthFilter.java index 229a970b..70357f16 100644 --- a/sonic-server-gateway/src/main/java/com/sonic/gateway/config/AuthFilter.java +++ b/sonic-server-gateway/src/main/java/com/sonic/gateway/config/AuthFilter.java @@ -25,6 +25,8 @@ public class AuthFilter implements GlobalFilter, Ordered { @Value("${filter.white-list}") private List whiteList; + @Value("${filter.resetToken}") + private Boolean resetToken; @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { @@ -33,41 +35,23 @@ public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { return chain.filter(exchange); } } - String token = exchange.getRequest().getHeaders().getFirst("sonicToken"); - if (token.equals("test")) { - return chain.filter(exchange); - } + String token = exchange.getRequest().getHeaders().getFirst("SonicToken"); ServerHttpResponse response = exchange.getResponse(); response.getHeaders().add("Content-Type", "text/plain;charset=UTF-8"); DataBuffer buffer = sendResp(response); if (token == null) { - response.setStatusCode(HttpStatus.UNAUTHORIZED); - return response.writeWith(Mono.just(buffer)); - } - String username = JWTTokenTool.getUserName(token); - if (username != null && username.length() > 0) { - String redisToken; - Object redisTokenObject = RedisTool.get("sonic:user:" + username); - if (redisTokenObject == null) { - response.setStatusCode(HttpStatus.UNAUTHORIZED); - return response.writeWith(Mono.just(buffer)); - } - redisToken = redisTokenObject.toString(); - if (!redisToken.equals(token)) { - response.setStatusCode(HttpStatus.UNAUTHORIZED); - return response.writeWith(Mono.just(buffer)); - } else { - RedisTool.expire("sonic:user:" + username, 7); - } - } else { - response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.writeWith(Mono.just(buffer)); } // 验证 token if (!JWTTokenTool.verify(token)) { - response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.writeWith(Mono.just(buffer)); } + Object redisTokenObject = RedisTool.get("sonic:user:" + token); + if (redisTokenObject == null) { + return response.writeWith(Mono.just(buffer)); + } else if (resetToken) { + RedisTool.expire("sonic:user:" + token, 7); + } return chain.filter(exchange); } diff --git a/sonic-server-task/src/main/java/com/sonic/task/controller/JobsController.java b/sonic-server-task/src/main/java/com/sonic/task/controller/JobsController.java index 5e98adaf..be108971 100644 --- a/sonic-server-task/src/main/java/com/sonic/task/controller/JobsController.java +++ b/sonic-server-task/src/main/java/com/sonic/task/controller/JobsController.java @@ -1,7 +1,7 @@ package com.sonic.task.controller; import com.sonic.common.config.WebAspect; -import com.sonic.common.exception.SonicCronException; +import com.sonic.common.exception.SonicException; import com.sonic.common.http.RespEnum; import com.sonic.common.http.RespModel; import com.sonic.task.models.Jobs; @@ -18,8 +18,6 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import java.util.List; - /** * @author ZhouYiXun * @des 定时任务控制器 @@ -35,7 +33,7 @@ public class JobsController { @WebAspect @ApiOperation(value = "更新定时任务信息", notes = "新增或更新定时任务的信息") @PutMapping - public RespModel save(@Validated @RequestBody Jobs jobs) throws SonicCronException { + public RespModel save(@Validated @RequestBody Jobs jobs) throws SonicException { return jobsService.save(jobs); } diff --git a/sonic-server-task/src/main/java/com/sonic/task/quartz/QuartzJob.java b/sonic-server-task/src/main/java/com/sonic/task/quartz/QuartzJob.java index b03e5bde..2a3a2630 100644 --- a/sonic-server-task/src/main/java/com/sonic/task/quartz/QuartzJob.java +++ b/sonic-server-task/src/main/java/com/sonic/task/quartz/QuartzJob.java @@ -47,7 +47,12 @@ protected void executeInternal(JobExecutionContext jobExecutionContext) { Jobs jobs = jobsService.findById(dataMap.getInt("id")); if (jobs != null) { RespModel r = controllerFeignClient.runSuite(jobs.getSuiteId()); - logger.info("定时任务开始执行:测试套件" + jobs.getSuiteId() + " " + r); + if (r.getCode() == 3001) { + logger.info("测试套件" + jobs.getSuiteId() + " 已删除" + r); + jobsService.delete(dataMap.getInt("id")); + } else { + logger.info("定时任务开始执行:测试套件" + jobs.getSuiteId() + " " + r); + } } else { logger.info("定时任务id:" + dataMap.getInt("id") + "不存在!"); } diff --git a/sonic-server-task/src/main/java/com/sonic/task/service/JobsService.java b/sonic-server-task/src/main/java/com/sonic/task/service/JobsService.java index e5996ce2..7fe1e6b7 100644 --- a/sonic-server-task/src/main/java/com/sonic/task/service/JobsService.java +++ b/sonic-server-task/src/main/java/com/sonic/task/service/JobsService.java @@ -1,6 +1,6 @@ package com.sonic.task.service; -import com.sonic.common.exception.SonicCronException; +import com.sonic.common.exception.SonicException; import com.sonic.common.http.RespModel; import com.sonic.task.models.Jobs; import org.springframework.data.domain.Page; @@ -12,7 +12,7 @@ * @date 2021/8/22 11:20 */ public interface JobsService { - RespModel save(Jobs jobs) throws SonicCronException; + RespModel save(Jobs jobs) throws SonicException; RespModel updateStatus(int id, int type); diff --git a/sonic-server-task/src/main/java/com/sonic/task/service/impl/JobsServiceImpl.java b/sonic-server-task/src/main/java/com/sonic/task/service/impl/JobsServiceImpl.java index cc968009..f078ebe0 100644 --- a/sonic-server-task/src/main/java/com/sonic/task/service/impl/JobsServiceImpl.java +++ b/sonic-server-task/src/main/java/com/sonic/task/service/impl/JobsServiceImpl.java @@ -3,7 +3,7 @@ import com.sonic.common.http.RespEnum; import com.sonic.common.http.RespModel; import com.sonic.task.dao.JobsRepository; -import com.sonic.common.exception.SonicCronException; +import com.sonic.common.exception.SonicException; import com.sonic.task.models.Jobs; import com.sonic.task.models.interfaces.JobStatus; import com.sonic.task.quartz.QuartzHandler; @@ -29,8 +29,8 @@ public class JobsServiceImpl implements JobsService { private JobsRepository jobsRepository; @Override - @Transactional(rollbackFor = SonicCronException.class) - public RespModel save(Jobs jobs) throws SonicCronException { + @Transactional(rollbackFor = SonicException.class) + public RespModel save(Jobs jobs) throws SonicException { jobs.setStatus(JobStatus.ENABLE); jobsRepository.save(jobs); CronTrigger trigger = quartzHandler.getTrigger(jobs); @@ -43,7 +43,7 @@ public RespModel save(Jobs jobs) throws SonicCronException { return new RespModel(RespEnum.HANDLE_OK); } catch (RuntimeException | SchedulerException e) { e.printStackTrace(); - throw new SonicCronException("操作失败!请检查cron表达式是否无误!"); + throw new SonicException("操作失败!请检查cron表达式是否无误!"); } } From 1dc9863bee26ff8797ec5dcd59203b3c058e8998 Mon Sep 17 00:00:00 2001 From: zhouyixun <291028775@qq.com> Date: Thu, 14 Oct 2021 21:48:03 +0800 Subject: [PATCH 2/2] save_last --- config/actuator-dev.yml | 8 - config/feign-dev.yml | 18 -- config/jdbc-dev.yml | 11 -- config/knife4j-dev.yml | 8 - config/logging-dev.yml | 7 - config/rabbitmq-dev.yml | 7 - config/redis-dev.yml | 5 - config/sonic-server-bus-dev.yml | 11 -- config/sonic-server-controller-dev.yml | 16 -- config/sonic-server-folder-dev.yml | 11 -- config/sonic-server-gateway-dev.yml | 31 ---- config/sonic-server-task-dev.yml | 27 --- config/sonic-server-transport-dev.yml | 16 -- docker-compose.yml | 172 ++++++++++++++++++ pom.xml | 31 ++++ .../src/main/resources/bootstrap.yml | 22 +-- sonic-server-common/pom.xml | 7 + .../services/impl/UsersServiceImpl.java | 4 +- .../src/main/resources/bootstrap.yml | 2 +- .../src/main/resources/application.yml | 12 +- .../src/main/resources/bootstrap.yml | 2 +- .../src/main/resources/bootstrap.yml | 2 +- .../src/main/resources/bootstrap.yml | 2 +- .../src/main/resources/bootstrap.yml | 2 +- 24 files changed, 232 insertions(+), 202 deletions(-) delete mode 100644 config/actuator-dev.yml delete mode 100644 config/feign-dev.yml delete mode 100644 config/jdbc-dev.yml delete mode 100644 config/knife4j-dev.yml delete mode 100644 config/logging-dev.yml delete mode 100644 config/rabbitmq-dev.yml delete mode 100644 config/redis-dev.yml delete mode 100644 config/sonic-server-bus-dev.yml delete mode 100644 config/sonic-server-controller-dev.yml delete mode 100644 config/sonic-server-folder-dev.yml delete mode 100644 config/sonic-server-gateway-dev.yml delete mode 100644 config/sonic-server-task-dev.yml delete mode 100644 config/sonic-server-transport-dev.yml create mode 100644 docker-compose.yml diff --git a/config/actuator-dev.yml b/config/actuator-dev.yml deleted file mode 100644 index 23c24f8a..00000000 --- a/config/actuator-dev.yml +++ /dev/null @@ -1,8 +0,0 @@ -management: - endpoint: - health: - show-details: always - endpoints: - web: - exposure: - include: health,info,refresh \ No newline at end of file diff --git a/config/feign-dev.yml b/config/feign-dev.yml deleted file mode 100644 index 030c57a8..00000000 --- a/config/feign-dev.yml +++ /dev/null @@ -1,18 +0,0 @@ -feign: - hystrix: - enabled: true - client: - config: - default: - connectTimeout: 10000 - readTimeout: 10000 - -hystrix: - command: - default: - execution: - timeout: - enabled: true - isolation: - thread: - timeoutInMilliseconds: 12000 \ No newline at end of file diff --git a/config/jdbc-dev.yml b/config/jdbc-dev.yml deleted file mode 100644 index d26d748c..00000000 --- a/config/jdbc-dev.yml +++ /dev/null @@ -1,11 +0,0 @@ -spring: - datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/sonic?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&autoReconnect=true&serverTimezone=GMT%2b8 - username: root - password: Sonic!@#123 - jpa: - open-in-view: true - hibernate: - ddl-auto: update - show-sql: true \ No newline at end of file diff --git a/config/knife4j-dev.yml b/config/knife4j-dev.yml deleted file mode 100644 index 75998326..00000000 --- a/config/knife4j-dev.yml +++ /dev/null @@ -1,8 +0,0 @@ -knife4j: - enable: true - setting: - enableHost: true - enableHostText: "http://localhost:8094" - enableFooter: false - enableFooterCustom: true - footerCustomContent: MIT License | Copyright 2021 - [ZhouYiXun/sonic-server](https://github.com/ZhouYixun/sonic-server/blob/main/LICENSE) \ No newline at end of file diff --git a/config/logging-dev.yml b/config/logging-dev.yml deleted file mode 100644 index 14818251..00000000 --- a/config/logging-dev.yml +++ /dev/null @@ -1,7 +0,0 @@ -logging: - file: - name: logs/${spring.application.name}.log - logback: - rollingpolicy: - clean-history-on-start: true - max-history: 3 \ No newline at end of file diff --git a/config/rabbitmq-dev.yml b/config/rabbitmq-dev.yml deleted file mode 100644 index 02f2a3b9..00000000 --- a/config/rabbitmq-dev.yml +++ /dev/null @@ -1,7 +0,0 @@ -spring: - rabbitmq: - host: localhost - port: 5672 - username: guest - password: guest - virtual-host: sonic \ No newline at end of file diff --git a/config/redis-dev.yml b/config/redis-dev.yml deleted file mode 100644 index 77af3c0e..00000000 --- a/config/redis-dev.yml +++ /dev/null @@ -1,5 +0,0 @@ -spring: - redis: - database: 0 - host: 127.0.0.1 - port: 6379 \ No newline at end of file diff --git a/config/sonic-server-bus-dev.yml b/config/sonic-server-bus-dev.yml deleted file mode 100644 index dec293d6..00000000 --- a/config/sonic-server-bus-dev.yml +++ /dev/null @@ -1,11 +0,0 @@ -server: - port: 0 - -management: - endpoint: - health: - show-details: always - endpoints: - web: - exposure: - include: health,info,bus-refresh \ No newline at end of file diff --git a/config/sonic-server-controller-dev.yml b/config/sonic-server-controller-dev.yml deleted file mode 100644 index ca201930..00000000 --- a/config/sonic-server-controller-dev.yml +++ /dev/null @@ -1,16 +0,0 @@ -server: - port: 8092 - -spring: - data: - redis: - repositories: - enabled: false - -robot: - client: - host: "http://localhost:3000" - img: - success: "" - warning: "" - error: "" \ No newline at end of file diff --git a/config/sonic-server-folder-dev.yml b/config/sonic-server-folder-dev.yml deleted file mode 100644 index cf8634bf..00000000 --- a/config/sonic-server-folder-dev.yml +++ /dev/null @@ -1,11 +0,0 @@ -server: - port: 8093 - -spring: - servlet: - multipart: - maxFileSize: -1 - maxRequestSize: -1 - -gateway: - host: "http://localhost:8094" \ No newline at end of file diff --git a/config/sonic-server-gateway-dev.yml b/config/sonic-server-gateway-dev.yml deleted file mode 100644 index f06df4ae..00000000 --- a/config/sonic-server-gateway-dev.yml +++ /dev/null @@ -1,31 +0,0 @@ -server: - port: 8094 - -filter: - white-list: /users/login,/users/register,/swagger-resources,/v2/api-docs,/folder/upload,/folder/recordFiles,/keepFiles/,/imageFiles/,/recordFiles/,/logFiles/ - resetToken: true - -spring: - cloud: - gateway: - routes: - - id: controller - uri: lb://sonic-server-controller - predicates: Path=/api/controller/** - filters: - - StripPrefix=2 - - id: folder - uri: lb://sonic-server-folder - predicates: Path=/api/folder/** - filters: - - StripPrefix=2 - - id: task - uri: lb://sonic-server-task - predicates: Path=/api/task/** - filters: - - StripPrefix=2 - - id: transport - uri: lb://sonic-server-transport - predicates: Path=/api/transport/** - filters: - - StripPrefix=2 \ No newline at end of file diff --git a/config/sonic-server-task-dev.yml b/config/sonic-server-task-dev.yml deleted file mode 100644 index 7ba84b06..00000000 --- a/config/sonic-server-task-dev.yml +++ /dev/null @@ -1,27 +0,0 @@ -server: - port: 8096 - -spring: - datasource: - initialization-mode: always - quartz: - properties: - org: - quartz: - scheduler: - instanceName: SonicQuartz - instanceId: AUTO - jobStore: - isClustered: true - job-store-type: jdbc - jdbc: - initialize-schema: never - -sonic: - jobs: - fileCron: 0 0 12 * * ? - resultCron: 0 0 12 * * ? - dayReportCron: 0 0 10 * * ? - weekReportCron: 0 0 10 ? * Mon - filesKeepDay: 60 - resultsKeepDay: 60 diff --git a/config/sonic-server-transport-dev.yml b/config/sonic-server-transport-dev.yml deleted file mode 100644 index 4bc6e17f..00000000 --- a/config/sonic-server-transport-dev.yml +++ /dev/null @@ -1,16 +0,0 @@ -server: - port: 0 - -spring: - rabbitmq: - listener: - simple: - acknowledge-mode: manual - retry: - enabled: true - max-attempts: 5 - max-interval: 10000 - initial-interval: 10000 - default-requeue-rejected: true - publisher-confirm-type: correlated - publisher-returns: true \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..68e9d620 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,172 @@ +version: '3' +#you can add this for log debugging for every container. +#volumes: +# - /home/sonic/logs/:/logs/ +services: + sonic-server-eureka: + image: "zhouyixun/sonic-server-eureka:v1.0.0" + hostname: sonic-server-eureka + environment: + - EUREKA_HOST=sonic-server-eureka + networks: + - sonic-network + ports: + - "9090:9090" + deploy: + replicas: 1 + placement: + constraints: + - node.role == manager + update_config: + parallelism: 1 + delay: 2s + restart_policy: + condition: on-failure + delay: 20s + max_attempts: 6 + sonic-server-bus: + image: "zhouyixun/sonic-server-bus:v1.0.0" + environment: + - PROFILE=prod + - CONFIG_TYPE=git + - EUREKA_URL=http://sonic:sonic@sonic-server-eureka:9090/eureka/ + networks: + - sonic-network + depends_on: + - sonic-server-eureka + deploy: + replicas: 1 + placement: + constraints: + - node.role == manager + update_config: + parallelism: 1 + delay: 2s + restart_policy: + condition: on-failure + delay: 20s + max_attempts: 6 + sonic-server-gateway: + image: "zhouyixun/sonic-server-gateway:v1.0.0" + environment: + - PROFILE=prod + - EUREKA_URL=http://sonic:sonic@sonic-server-eureka:9090/eureka/ + networks: + - sonic-network + depends_on: + - sonic-server-bus + - sonic-server-eureka + ports: + - "8094:8094" + deploy: + replicas: 1 + placement: + constraints: + - node.role == manager + update_config: + parallelism: 1 + delay: 2s + restart_policy: + condition: on-failure + delay: 20s + max_attempts: 6 + sonic-server-controller: + image: "zhouyixun/sonic-server-controller:v1.0.0" + environment: + - PROFILE=prod + - EUREKA_URL=http://sonic:sonic@sonic-server-eureka:9090/eureka/ + networks: + - sonic-network + depends_on: + - sonic-server-bus + - sonic-server-eureka + - sonic-server-gateway + deploy: + replicas: 2 + placement: + constraints: + - node.role == worker + update_config: + parallelism: 1 + delay: 2s + restart_policy: + condition: on-failure + delay: 20s + max_attempts: 6 + sonic-server-folder: + image: "zhouyixun/sonic-server-folder:v1.0.0" + environment: + - PROFILE=prod + - EUREKA_URL=http://sonic:sonic@sonic-server-eureka:9090/eureka/ + networks: + - sonic-network + # volumes: + # - /home/sonic/keepFiles/:/keepFiles/ + # - /home/sonic/imageFiles/:/imageFiles/ + # - /home/sonic/recordFiles/:/recordFiles/ + depends_on: + - sonic-server-bus + - sonic-server-eureka + - sonic-server-gateway + deploy: + replicas: 1 + placement: + constraints: + - node.role == manager + update_config: + parallelism: 1 + delay: 2s + restart_policy: + condition: on-failure + delay: 20s + max_attempts: 6 + sonic-server-task: + image: "zhouyixun/sonic-server-task:v1.0.0" + environment: + - PROFILE=prod + - EUREKA_URL=http://sonic:sonic@sonic-server-eureka:9090/eureka/ + networks: + - sonic-network + depends_on: + - sonic-server-bus + - sonic-server-eureka + - sonic-server-gateway + deploy: + replicas: 1 + placement: + constraints: + - node.role == worker + update_config: + parallelism: 1 + delay: 2s + restart_policy: + condition: on-failure + delay: 20s + max_attempts: 6 + sonic-server-transport: + image: "zhouyixun/sonic-server-transport:v1.0.0" + environment: + - PROFILE=prod + - EUREKA_URL=http://sonic:sonic@sonic-server-eureka:9090/eureka/ + networks: + - sonic-network + depends_on: + - sonic-server-bus + - sonic-server-eureka + - sonic-server-gateway + deploy: + replicas: 1 + placement: + constraints: + - node.role == worker + update_config: + parallelism: 1 + delay: 2s + restart_policy: + condition: on-failure + delay: 20s + max_attempts: 6 + +networks: + sonic-network: + driver: overlay \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0e17e66d..ba1ae2bf 100644 --- a/pom.xml +++ b/pom.xml @@ -77,6 +77,37 @@ org.springframework.boot spring-boot-maven-plugin + + com.spotify + docker-maven-plugin + 1.2.2 + + + imageBuild + package + + build + + + zhouyixun/${project.artifactId}:v${project.version} + adoptopenjdk/openjdk15:alpine AS builder + + ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime + + + ["java","-server","-Xmx800M","-Xms800M","-Xmn300M","-Xss256K","-XX:-UseGCOverheadLimit","-XX:+DisableExplicitGC","-XX:SurvivorRatio=1","-XX:LargePageSizeInBytes=128M","-XX:SoftRefLRUPolicyMSPerMB=0","-Djava.security.egd=file:/dev/./urandom","-jar","/${project.build.finalName}.jar"] + + + + / + ${project.build.directory} + ${project.build.finalName}.jar + + + + + + diff --git a/sonic-server-bus/src/main/resources/bootstrap.yml b/sonic-server-bus/src/main/resources/bootstrap.yml index 6beb6d0b..660b48aa 100644 --- a/sonic-server-bus/src/main/resources/bootstrap.yml +++ b/sonic-server-bus/src/main/resources/bootstrap.yml @@ -10,18 +10,14 @@ spring: name: @project.artifactId@,logging,rabbitmq server: composite: -# 默认取本地的/config目录 - - type: native -# 如果需要取git地址,可以开启下面的方式 -# - type: git -# uri: git@xxxxxxxxxxx.git -# ignore-local-ssh-settings: true -# search-paths: /config -# private-key: | -# -----BEGIN RSA PRIVATE KEY----- -# MIIJKAIBAAKCxxxxxxxxxxxxxYap344ebwVeyRP1FWqTB6XJREZJENEfMPH6TATp -# oESNkPqZKn+1xxxxxxxxxxxxxFEwGGBfGhGE5Tb8S+AWRFwQ26SXnw930HfoPFcP -# -----END RSA PRIVATE KEY----- + - type: ${CONFIG_TYPE:native} + uri: ${GIT_URL:git@github.com:ZhouYixun/sonic-server-config-yml.git} + ignore-local-ssh-settings: true + search-paths: /config + private-key: | + -----BEGIN RSA PRIVATE KEY----- + ${GIT_KEY:xxx} + -----END RSA PRIVATE KEY----- bootstrap: true eureka: @@ -30,5 +26,5 @@ eureka: prefer-ip-address: true client: service-url: - defaultZone: ${EUREKA_URL:http://sonic:tester@127.0.0.1:9090/eureka/} + defaultZone: ${EUREKA_URL:http://sonic:sonic@127.0.0.1:9090/eureka/} diff --git a/sonic-server-common/pom.xml b/sonic-server-common/pom.xml index aa975eda..c69633ad 100644 --- a/sonic-server-common/pom.xml +++ b/sonic-server-common/pom.xml @@ -60,6 +60,13 @@ true + + com.spotify + docker-maven-plugin + + true + + diff --git a/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/UsersServiceImpl.java b/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/UsersServiceImpl.java index d41d9e46..19269371 100644 --- a/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/UsersServiceImpl.java +++ b/sonic-server-controller/src/main/java/com/sonic/controller/services/impl/UsersServiceImpl.java @@ -46,7 +46,7 @@ public void register(Users users) throws SonicException { @Override public String login(UserInfo userInfo) { Users users = usersRepository.findByUserName(userInfo.getUserName()); - if (DigestUtils.md5DigestAsHex(userInfo.getPassword().getBytes()).equals(users.getPassword())) { + if (users != null && DigestUtils.md5DigestAsHex(userInfo.getPassword().getBytes()).equals(users.getPassword())) { String token = JWTTokenTool.getToken(userInfo.getUserName()); users.setPassword(""); redisTool.set("sonic:user:" + token, users, 7); @@ -71,7 +71,7 @@ public Users getUserInfo(String token) { public RespModel resetPwd(String token, ChangePwd changePwd) { Object redisTokenObject = RedisTool.get("sonic:user:" + token); if (redisTokenObject != null) { - Users users = ((Users) redisTokenObject); + Users users = usersRepository.findByUserName(((Users) redisTokenObject).getUserName()); if (users != null) { if (DigestUtils.md5DigestAsHex(changePwd.getOldPwd().getBytes()).equals(users.getPassword())) { users.setPassword(DigestUtils.md5DigestAsHex(changePwd.getNewPwd().getBytes())); diff --git a/sonic-server-controller/src/main/resources/bootstrap.yml b/sonic-server-controller/src/main/resources/bootstrap.yml index a017231c..7dca814b 100644 --- a/sonic-server-controller/src/main/resources/bootstrap.yml +++ b/sonic-server-controller/src/main/resources/bootstrap.yml @@ -18,4 +18,4 @@ eureka: prefer-ip-address: true client: service-url: - defaultZone: ${EUREKA_URL:http://sonic:tester@127.0.0.1:9090/eureka/} + defaultZone: ${EUREKA_URL:http://sonic:sonic@127.0.0.1:9090/eureka/} diff --git a/sonic-server-eureka/src/main/resources/application.yml b/sonic-server-eureka/src/main/resources/application.yml index 0d762853..5f624867 100644 --- a/sonic-server-eureka/src/main/resources/application.yml +++ b/sonic-server-eureka/src/main/resources/application.yml @@ -10,8 +10,8 @@ spring: basic: enabled: true user: - name: sonic - password: tester + name: ${SEC_NAME:sonic} + password: ${SEC_PASSWORD:sonic} eureka: datacenter: sonic-server @@ -20,11 +20,11 @@ eureka: hostname: ${EUREKA_HOST:127.0.0.1} instance-id: ${spring.application.name}(v${spring.version})-${spring.cloud.client.ip-address} client: - # 单机部署的话放开以下注释 - register-with-eureka: false - fetch-registry: false + # 单机部署的话以下为false + register-with-eureka: ${IS_CLUSTERED:false} + fetch-registry: ${IS_CLUSTERED:false} service-url: - defaultZone: ${EUREKA_URL:http://sonic:tester@127.0.0.1:9090/eureka/} + defaultZone: ${EUREKA_URL:http://sonic:sonic@127.0.0.1:9090/eureka/} logging: file: diff --git a/sonic-server-folder/src/main/resources/bootstrap.yml b/sonic-server-folder/src/main/resources/bootstrap.yml index 0df2717b..5c5c5f7c 100644 --- a/sonic-server-folder/src/main/resources/bootstrap.yml +++ b/sonic-server-folder/src/main/resources/bootstrap.yml @@ -18,4 +18,4 @@ eureka: prefer-ip-address: true client: service-url: - defaultZone: ${EUREKA_URL:http://sonic:tester@127.0.0.1:9090/eureka/} + defaultZone: ${EUREKA_URL:http://sonic:sonic@127.0.0.1:9090/eureka/} diff --git a/sonic-server-gateway/src/main/resources/bootstrap.yml b/sonic-server-gateway/src/main/resources/bootstrap.yml index 81bde020..1ef195f9 100644 --- a/sonic-server-gateway/src/main/resources/bootstrap.yml +++ b/sonic-server-gateway/src/main/resources/bootstrap.yml @@ -18,4 +18,4 @@ eureka: prefer-ip-address: true client: service-url: - defaultZone: ${EUREKA_URL:http://sonic:tester@127.0.0.1:9090/eureka/} + defaultZone: ${EUREKA_URL:http://sonic:sonic@127.0.0.1:9090/eureka/} diff --git a/sonic-server-task/src/main/resources/bootstrap.yml b/sonic-server-task/src/main/resources/bootstrap.yml index 20121c92..f44496cb 100644 --- a/sonic-server-task/src/main/resources/bootstrap.yml +++ b/sonic-server-task/src/main/resources/bootstrap.yml @@ -18,4 +18,4 @@ eureka: prefer-ip-address: true client: service-url: - defaultZone: ${EUREKA_URL:http://sonic:tester@127.0.0.1:9090/eureka/} + defaultZone: ${EUREKA_URL:http://sonic:sonic@127.0.0.1:9090/eureka/} diff --git a/sonic-server-transport/src/main/resources/bootstrap.yml b/sonic-server-transport/src/main/resources/bootstrap.yml index 0b9689f5..5efca0b8 100644 --- a/sonic-server-transport/src/main/resources/bootstrap.yml +++ b/sonic-server-transport/src/main/resources/bootstrap.yml @@ -18,4 +18,4 @@ eureka: prefer-ip-address: true client: service-url: - defaultZone: ${EUREKA_URL:http://sonic:tester@127.0.0.1:9090/eureka/} + defaultZone: ${EUREKA_URL:http://sonic:sonic@127.0.0.1:9090/eureka/}