From a7b9c261915a8b843768f40518649d6b506e3bb8 Mon Sep 17 00:00:00 2001 From: mewhz Date: Sun, 20 Nov 2022 21:35:27 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=E5=A2=9E=E5=8A=A0=E6=9C=AA=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=97=B6=E8=B7=B3=E8=BD=AC=E4=B8=8E=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=90=8E=E8=87=AA=E5=8A=A8=E5=9B=9E=E5=88=B0=E5=8E=9F=E7=95=8C?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pastecode-system/pom.xml | 7 ++++ .../com/mewhz/paste/common/ResultUtils.java | 4 +-- .../paste/controller/CodeController.java | 2 ++ .../paste/controller/CodeInfoController.java | 4 +-- .../paste/controller/UserController.java | 24 ++++++++++++-- .../exception/GlobalExceptionHandler.java | 19 +++++++++++ .../paste/model/vo/UserLoginResponse.java | 16 +++++++++ pastecode-ui/src/views/Code.vue | 31 ++++++++++++++--- pastecode-ui/src/views/Home.vue | 8 +++++ pastecode-ui/src/views/Login.vue | 33 ++++++++++++++----- 10 files changed, 127 insertions(+), 21 deletions(-) create mode 100644 pastecode-system/src/main/java/com/mewhz/paste/exception/GlobalExceptionHandler.java create mode 100644 pastecode-system/src/main/java/com/mewhz/paste/model/vo/UserLoginResponse.java diff --git a/pastecode-system/pom.xml b/pastecode-system/pom.xml index 4e0c1df..706e6d0 100644 --- a/pastecode-system/pom.xml +++ b/pastecode-system/pom.xml @@ -52,6 +52,13 @@ 5.8.6 + + + cn.dev33 + sa-token-spring-boot-starter + 1.32.0 + + diff --git a/pastecode-system/src/main/java/com/mewhz/paste/common/ResultUtils.java b/pastecode-system/src/main/java/com/mewhz/paste/common/ResultUtils.java index 50dc395..b92a2b0 100644 --- a/pastecode-system/src/main/java/com/mewhz/paste/common/ResultUtils.java +++ b/pastecode-system/src/main/java/com/mewhz/paste/common/ResultUtils.java @@ -33,7 +33,7 @@ public static BaseResponse codeError(String responseMessage) { * 未知错误 * @return 错误代码与未知错误 */ - public static BaseResponse error() { - return new BaseResponse<>(50000, "未知错误"); + public static BaseResponse error(String responseMessage) { + return new BaseResponse<>(50000, responseMessage); } } diff --git a/pastecode-system/src/main/java/com/mewhz/paste/controller/CodeController.java b/pastecode-system/src/main/java/com/mewhz/paste/controller/CodeController.java index 80e8026..3d34d55 100644 --- a/pastecode-system/src/main/java/com/mewhz/paste/controller/CodeController.java +++ b/pastecode-system/src/main/java/com/mewhz/paste/controller/CodeController.java @@ -1,6 +1,7 @@ package com.mewhz.paste.controller; +import cn.dev33.satoken.annotation.SaCheckLogin; import com.mewhz.paste.model.entity.Code; import com.mewhz.paste.service.CodeService; import org.springframework.beans.factory.annotation.Autowired; @@ -38,6 +39,7 @@ public Map save(@RequestBody Code code){ return result; } + @SaCheckLogin @GetMapping("/id/{id}") public Code findById(@PathVariable("id") Integer id){ return codeService.getById(id); diff --git a/pastecode-system/src/main/java/com/mewhz/paste/controller/CodeInfoController.java b/pastecode-system/src/main/java/com/mewhz/paste/controller/CodeInfoController.java index 2bc7d3a..c8191e1 100644 --- a/pastecode-system/src/main/java/com/mewhz/paste/controller/CodeInfoController.java +++ b/pastecode-system/src/main/java/com/mewhz/paste/controller/CodeInfoController.java @@ -13,7 +13,7 @@ import java.util.List; @RestController -@RequestMapping("codeInfo") +@RequestMapping("/codeInfo") public class CodeInfoController { @Resource @@ -24,7 +24,7 @@ public BaseResponse> selectAll() { return ResultUtils.success(codeInfoService.selectAll()); } - @GetMapping("/{codeId}") + @GetMapping("codeId/{codeId}") public BaseResponse selectByCodeId(@PathVariable Integer codeId) { return ResultUtils.success(codeInfoService.selectByCodeId(codeId)); } diff --git a/pastecode-system/src/main/java/com/mewhz/paste/controller/UserController.java b/pastecode-system/src/main/java/com/mewhz/paste/controller/UserController.java index 731d83f..90122f3 100644 --- a/pastecode-system/src/main/java/com/mewhz/paste/controller/UserController.java +++ b/pastecode-system/src/main/java/com/mewhz/paste/controller/UserController.java @@ -1,10 +1,13 @@ package com.mewhz.paste.controller; +import cn.dev33.satoken.stp.StpUtil; +import cn.hutool.core.bean.BeanUtil; import com.mewhz.paste.common.BaseResponse; import com.mewhz.paste.common.ResultUtils; import com.mewhz.paste.model.dto.UserLoginRequest; import com.mewhz.paste.model.entity.User; +import com.mewhz.paste.model.vo.UserLoginResponse; import com.mewhz.paste.service.UserService; import org.springframework.web.bind.annotation.*; @@ -27,12 +30,13 @@ public BaseResponse> findAll() { } @PostMapping("/login") - public BaseResponse userLogin(@RequestBody UserLoginRequest userLoginRequest) { + public BaseResponse userLogin(@RequestBody UserLoginRequest userLoginRequest) { String userAccount = userLoginRequest.getUserAccount(); String userPassword = userLoginRequest.getUserPassword(); User user = userService.userLogin(userAccount, userPassword); + UserLoginResponse userLoginResponse = new UserLoginResponse(); System.out.println("user = " + user); if (user == null) { @@ -42,10 +46,24 @@ else if (!user.getUserPassword().equals(userPassword)) { return ResultUtils.loginError("密码错误!!"); } else if (user.getUserAccount().equals(userAccount) && user.getUserPassword().equals(userPassword)) { - return ResultUtils.success(user); + System.out.println(user); + System.out.println("====="); + BeanUtil.copyProperties(user, userLoginResponse); + System.out.println(userLoginResponse); + StpUtil.login(userLoginResponse.getId()); + System.out.println("StpUtil.isLogin() = " + StpUtil.isLogin()); + userLoginResponse.setTokenValue(StpUtil.getTokenInfo().getTokenValue()); + userLoginResponse.setTokenName(StpUtil.getTokenInfo().getTokenName()); + + return ResultUtils.success(userLoginResponse); } else { - return ResultUtils.error(); + return ResultUtils.error("未知错误, 请联系管理员!!"); } } + + @GetMapping("/info") + public boolean info() { + return StpUtil.isLogin(); + } } diff --git a/pastecode-system/src/main/java/com/mewhz/paste/exception/GlobalExceptionHandler.java b/pastecode-system/src/main/java/com/mewhz/paste/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..d82f16c --- /dev/null +++ b/pastecode-system/src/main/java/com/mewhz/paste/exception/GlobalExceptionHandler.java @@ -0,0 +1,19 @@ +package com.mewhz.paste.exception; + +import cn.dev33.satoken.exception.NotLoginException; +import com.mewhz.paste.common.BaseResponse; +import com.mewhz.paste.common.ResultUtils; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * @author mewhz + */ +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(NotLoginException.class) + public BaseResponse handlerNotLoginException(NotLoginException e) { + return ResultUtils.error(e.getMessage()); + } +} diff --git a/pastecode-system/src/main/java/com/mewhz/paste/model/vo/UserLoginResponse.java b/pastecode-system/src/main/java/com/mewhz/paste/model/vo/UserLoginResponse.java new file mode 100644 index 0000000..d770f62 --- /dev/null +++ b/pastecode-system/src/main/java/com/mewhz/paste/model/vo/UserLoginResponse.java @@ -0,0 +1,16 @@ +package com.mewhz.paste.model.vo; + +import lombok.Data; + +/** + * @author mewhz + */ +@Data +public class UserLoginResponse { + private Integer id; + private String userName; + private String userAccount; + + private String tokenName; + private String tokenValue; +} diff --git a/pastecode-ui/src/views/Code.vue b/pastecode-ui/src/views/Code.vue index 59801bc..22903f6 100644 --- a/pastecode-ui/src/views/Code.vue +++ b/pastecode-ui/src/views/Code.vue @@ -24,19 +24,40 @@ export default { }, methods: { load() { - this.$axios.get('http://127.0.0.1:9090/code/id/' + this.id).then((res) => { - this.codeText = res.data.codeText; - this.codeClass = `language-${res.data.codeType} show-language`; + let tokenNames = localStorage.getItem("tokenName"); + let tokenValue = localStorage.getItem("tokenValue"); + + console.log("tokenValue:", localStorage.getItem("tokenName")) + let headers = {}; + + headers[tokenNames] = tokenValue; + + console.table(headers); + + this.$axios.get('http://127.0.0.1:9090/code/id/' + this.id, { + headers: headers, + }).then((res) => { + + res = res.data; + if (res.responseCode === 50000) { + console.log("path:", this.$route.path); + localStorage.setItem("path", this.$route.path); + this.$router.push("/login"); + return; + } + + this.codeText = res.codeText; + this.codeClass = `language-${res.codeType} show-language`; if (this.codeText === undefined || this.codeText === ""){ this.$router.push("/"); } - if (res.data.codeTitle === undefined || res.data.codeTitle === "") { + if (res.codeTitle === undefined || res.codeTitle === "") { this.codeTitle = ""; } else { - this.codeTitle += res.data.codeTitle; + this.codeTitle += res.codeTitle; } this.$nextTick(() => { diff --git a/pastecode-ui/src/views/Home.vue b/pastecode-ui/src/views/Home.vue index a11acff..b3bed2a 100644 --- a/pastecode-ui/src/views/Home.vue +++ b/pastecode-ui/src/views/Home.vue @@ -1,5 +1,8 @@