diff --git a/unilife/.idea/.gitignore b/.idea/.gitignore similarity index 100% rename from unilife/.idea/.gitignore rename to .idea/.gitignore diff --git a/unilife/.idea/ApifoxUploaderProjectSetting.xml b/.idea/ApifoxUploaderProjectSetting.xml similarity index 100% rename from unilife/.idea/ApifoxUploaderProjectSetting.xml rename to .idea/ApifoxUploaderProjectSetting.xml diff --git a/unilife/.idea/compiler.xml b/.idea/compiler.xml similarity index 100% rename from unilife/.idea/compiler.xml rename to .idea/compiler.xml diff --git a/unilife/.idea/dataSources.xml b/.idea/dataSources.xml similarity index 100% rename from unilife/.idea/dataSources.xml rename to .idea/dataSources.xml diff --git a/unilife/.idea/encodings.xml b/.idea/encodings.xml similarity index 100% rename from unilife/.idea/encodings.xml rename to .idea/encodings.xml diff --git a/unilife/.idea/jarRepositories.xml b/.idea/jarRepositories.xml similarity index 100% rename from unilife/.idea/jarRepositories.xml rename to .idea/jarRepositories.xml diff --git a/unilife/.idea/misc.xml b/.idea/misc.xml similarity index 100% rename from unilife/.idea/misc.xml rename to .idea/misc.xml diff --git a/unilife/.idea/modules.xml b/.idea/modules.xml similarity index 100% rename from unilife/.idea/modules.xml rename to .idea/modules.xml diff --git a/unilife/.idea/unilife.iml b/.idea/unilife.iml similarity index 100% rename from unilife/.idea/unilife.iml rename to .idea/unilife.iml diff --git a/unilife/.idea/vcs.xml b/.idea/vcs.xml similarity index 100% rename from unilife/.idea/vcs.xml rename to .idea/vcs.xml diff --git a/unilife/Front/vue-unilife/.gitignore b/Front/vue-unilife/.gitignore similarity index 100% rename from unilife/Front/vue-unilife/.gitignore rename to Front/vue-unilife/.gitignore diff --git a/unilife/Front/vue-unilife/.vscode/extensions.json b/Front/vue-unilife/.vscode/extensions.json similarity index 100% rename from unilife/Front/vue-unilife/.vscode/extensions.json rename to Front/vue-unilife/.vscode/extensions.json diff --git a/unilife/Front/vue-unilife/README.md b/Front/vue-unilife/README.md similarity index 100% rename from unilife/Front/vue-unilife/README.md rename to Front/vue-unilife/README.md diff --git a/unilife/Front/vue-unilife/index.html b/Front/vue-unilife/index.html similarity index 100% rename from unilife/Front/vue-unilife/index.html rename to Front/vue-unilife/index.html diff --git a/unilife/Front/vue-unilife/package.json b/Front/vue-unilife/package.json similarity index 95% rename from unilife/Front/vue-unilife/package.json rename to Front/vue-unilife/package.json index cf6438a..6b23c2e 100644 --- a/unilife/Front/vue-unilife/package.json +++ b/Front/vue-unilife/package.json @@ -14,6 +14,7 @@ "element-plus": "^2.9.7", "vee-validate": "^4.15.0", "vue": "^3.5.13", + "vue-router": "^4.5.0", "yup": "^1.6.1" }, "devDependencies": { diff --git a/unilife/Front/vue-unilife/pnpm-lock.yaml b/Front/vue-unilife/pnpm-lock.yaml similarity index 98% rename from unilife/Front/vue-unilife/pnpm-lock.yaml rename to Front/vue-unilife/pnpm-lock.yaml index 9d06602..9a44cde 100644 --- a/unilife/Front/vue-unilife/pnpm-lock.yaml +++ b/Front/vue-unilife/pnpm-lock.yaml @@ -8,6 +8,9 @@ importers: .: dependencies: + '@vue/shared': + specifier: ^3.5.13 + version: 3.5.13 axios: specifier: ^1.8.3 version: 1.8.3 @@ -20,6 +23,9 @@ importers: vue: specifier: ^3.5.13 version: 3.5.13(typescript@5.7.3) + vue-router: + specifier: ^4.5.0 + version: 4.5.0(vue@3.5.13(typescript@5.7.3)) yup: specifier: ^1.6.1 version: 1.6.1 @@ -371,6 +377,9 @@ packages: '@vue/compiler-vue2@2.7.16': resolution: {integrity: sha512-qYC3Psj9S/mfu9uVi5WvNZIzq+xnXMhOwbTFKKDD7b1lhpnn71jXSFdTQ+WsIEk0ONCd7VV2IMm7ONl6tbQ86A==} + '@vue/devtools-api@6.6.4': + resolution: {integrity: sha512-sGhTPMuXqZ1rVOk32RylztWkfXTRhuS7vgAKv0zjqk8gbsHkJ7xfFf+jbySxt7tWObEJwyKaHMikV/WGDiQm8g==} + '@vue/devtools-api@7.7.2': resolution: {integrity: sha512-1syn558KhyN+chO5SjlZIwJ8bV/bQ1nOVTG66t2RbG66ZGekyiYNmRO7X9BJCXQqPsFHlnksqvPhce2qpzxFnA==} @@ -733,6 +742,11 @@ packages: '@vue/composition-api': optional: true + vue-router@4.5.0: + resolution: {integrity: sha512-HDuk+PuH5monfNuY+ct49mNmkCRK4xJAV9Ts4z9UFc4rzdDnxQLyCMGGc8pKhZhHTVzfanpNwB/lwqevcBwI4w==} + peerDependencies: + vue: ^3.2.0 + vue-tsc@2.2.8: resolution: {integrity: sha512-jBYKBNFADTN+L+MdesNX/TB3XuDSyaWynKMDgR+yCSln0GQ9Tfb7JS2lr46s2LiFUT1WsmfWsSvIElyxzOPqcQ==} hasBin: true @@ -980,6 +994,8 @@ snapshots: de-indent: 1.0.2 he: 1.2.0 + '@vue/devtools-api@6.6.4': {} + '@vue/devtools-api@7.7.2': dependencies: '@vue/devtools-kit': 7.7.2 @@ -1345,6 +1361,11 @@ snapshots: dependencies: vue: 3.5.13(typescript@5.7.3) + vue-router@4.5.0(vue@3.5.13(typescript@5.7.3)): + dependencies: + '@vue/devtools-api': 6.6.4 + vue: 3.5.13(typescript@5.7.3) + vue-tsc@2.2.8(typescript@5.7.3): dependencies: '@volar/typescript': 2.4.12 diff --git a/unilife/Front/vue-unilife/public/images/LogPage1.jpg b/Front/vue-unilife/public/images/LogPage1.jpg similarity index 100% rename from unilife/Front/vue-unilife/public/images/LogPage1.jpg rename to Front/vue-unilife/public/images/LogPage1.jpg diff --git a/unilife/Front/vue-unilife/public/images/LogPage2.jpg b/Front/vue-unilife/public/images/LogPage2.jpg similarity index 100% rename from unilife/Front/vue-unilife/public/images/LogPage2.jpg rename to Front/vue-unilife/public/images/LogPage2.jpg diff --git a/Front/vue-unilife/public/images/个人.png b/Front/vue-unilife/public/images/个人.png new file mode 100644 index 0000000..f63e5e8 Binary files /dev/null and b/Front/vue-unilife/public/images/个人.png differ diff --git a/Front/vue-unilife/public/images/默认头像.jpg b/Front/vue-unilife/public/images/默认头像.jpg new file mode 100644 index 0000000..11eead7 Binary files /dev/null and b/Front/vue-unilife/public/images/默认头像.jpg differ diff --git a/unilife/Front/vue-unilife/public/vite.svg b/Front/vue-unilife/public/vite.svg similarity index 100% rename from unilife/Front/vue-unilife/public/vite.svg rename to Front/vue-unilife/public/vite.svg diff --git a/unilife/Front/vue-unilife/src/App.vue b/Front/vue-unilife/src/App.vue similarity index 58% rename from unilife/Front/vue-unilife/src/App.vue rename to Front/vue-unilife/src/App.vue index 52b6107..4d17c70 100644 --- a/unilife/Front/vue-unilife/src/App.vue +++ b/Front/vue-unilife/src/App.vue @@ -1,10 +1,9 @@ \ No newline at end of file diff --git a/Front/vue-unilife/src/components/Personal/AcountManager.vue b/Front/vue-unilife/src/components/Personal/AcountManager.vue new file mode 100644 index 0000000..162b2a5 --- /dev/null +++ b/Front/vue-unilife/src/components/Personal/AcountManager.vue @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/unilife/Front/vue-unilife/src/main.ts b/Front/vue-unilife/src/main.ts similarity index 82% rename from unilife/Front/vue-unilife/src/main.ts rename to Front/vue-unilife/src/main.ts index e561a97..e0eb237 100644 --- a/unilife/Front/vue-unilife/src/main.ts +++ b/Front/vue-unilife/src/main.ts @@ -3,7 +3,10 @@ import './style.css' import App from './App.vue' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' +import router from './routers' const app = createApp(App) -app.mount('#app') + app.use(ElementPlus) +app.use(router) +app.mount('#app') diff --git a/Front/vue-unilife/src/routers.ts b/Front/vue-unilife/src/routers.ts new file mode 100644 index 0000000..43a18a4 --- /dev/null +++ b/Front/vue-unilife/src/routers.ts @@ -0,0 +1,32 @@ +import type { RouteRecord, RouteRecordRaw } from 'vue-router'; +import { createWebHashHistory, createRouter,createWebHistory } from 'vue-router'; +import LogPage from './components/LogPage.vue'; +import Personal from './components/Personal.vue'; +import Manager from './components/Personal/AcountManager.vue'; + +const routes:Array = [ + { + path:'/log', + name: 'LogPage', + component: LogPage + }, + { + path:'/personal', + name: 'Personal', + component: Personal, + children: [ + { + path:'manager', + name: 'Manager', + component:Manager, + } + ] + } +]; + +const router = createRouter({ + history: createWebHistory(import.meta.env.BASE_URL), + routes +}); + +export default router; diff --git a/unilife/Front/vue-unilife/src/shims-vue.d.ts b/Front/vue-unilife/src/shims-vue.d.ts similarity index 100% rename from unilife/Front/vue-unilife/src/shims-vue.d.ts rename to Front/vue-unilife/src/shims-vue.d.ts diff --git a/unilife/Front/vue-unilife/src/style.css b/Front/vue-unilife/src/style.css similarity index 100% rename from unilife/Front/vue-unilife/src/style.css rename to Front/vue-unilife/src/style.css diff --git a/unilife/Front/vue-unilife/src/utils/request.ts b/Front/vue-unilife/src/utils/request.ts similarity index 100% rename from unilife/Front/vue-unilife/src/utils/request.ts rename to Front/vue-unilife/src/utils/request.ts diff --git a/unilife/Front/vue-unilife/src/vite-env.d.ts b/Front/vue-unilife/src/vite-env.d.ts similarity index 100% rename from unilife/Front/vue-unilife/src/vite-env.d.ts rename to Front/vue-unilife/src/vite-env.d.ts diff --git a/unilife/Front/vue-unilife/tsconfig.app.json b/Front/vue-unilife/tsconfig.app.json similarity index 100% rename from unilife/Front/vue-unilife/tsconfig.app.json rename to Front/vue-unilife/tsconfig.app.json diff --git a/unilife/Front/vue-unilife/tsconfig.json b/Front/vue-unilife/tsconfig.json similarity index 100% rename from unilife/Front/vue-unilife/tsconfig.json rename to Front/vue-unilife/tsconfig.json diff --git a/unilife/Front/vue-unilife/tsconfig.node.json b/Front/vue-unilife/tsconfig.node.json similarity index 100% rename from unilife/Front/vue-unilife/tsconfig.node.json rename to Front/vue-unilife/tsconfig.node.json diff --git a/unilife/Front/vue-unilife/vite.config.ts b/Front/vue-unilife/vite.config.ts similarity index 100% rename from unilife/Front/vue-unilife/vite.config.ts rename to Front/vue-unilife/vite.config.ts diff --git a/unilife/Front/前端环境.md b/Front/前端环境.md similarity index 81% rename from unilife/Front/前端环境.md rename to Front/前端环境.md index a5ac5de..e2facf6 100644 --- a/unilife/Front/前端环境.md +++ b/Front/前端环境.md @@ -45,4 +45,11 @@ pnpm install yup ```cmd pnpm add element-plus ``` +组件 +### 安装VueRouter +```cmd +pnpm i vue-router +pnpm add vue-router@4 +``` +用来完成界面跳转,同时完成vuerouter与ts的适配 \ No newline at end of file diff --git a/unilife/README.md b/README.md similarity index 100% rename from unilife/README.md rename to README.md diff --git a/unilife/UniLife开发文档.md b/UniLife开发文档.md similarity index 100% rename from unilife/UniLife开发文档.md rename to UniLife开发文档.md diff --git a/unilife/unilife-server/.gitignore b/unilife-server/.gitignore similarity index 100% rename from unilife/unilife-server/.gitignore rename to unilife-server/.gitignore diff --git a/unilife/unilife-server/pom.xml b/unilife-server/pom.xml similarity index 96% rename from unilife/unilife-server/pom.xml rename to unilife-server/pom.xml index af6a712..b10e4b3 100644 --- a/unilife/unilife-server/pom.xml +++ b/unilife-server/pom.xml @@ -35,6 +35,13 @@ 1.18.36 true + + + org.lionsoul + ip2region + 2.7.0 + + org.springframework.boot spring-boot-starter-test @@ -50,29 +57,22 @@ hutool-all 5.8.16 - + + + io.jsonwebtoken + jjwt + 0.9.1 + org.springframework.boot spring-boot-starter-data-redis - - - redis.clients - jedis - 5.1.3 - org.springframework.boot spring-boot-starter-mail - - - org.lionsoul - ip2region - 2.7.0 - diff --git a/unilife/unilife-server/src/main/java/com/unilife/UniLifeApplication.java b/unilife-server/src/main/java/com/unilife/UniLifeApplication.java similarity index 100% rename from unilife/unilife-server/src/main/java/com/unilife/UniLifeApplication.java rename to unilife-server/src/main/java/com/unilife/UniLifeApplication.java diff --git a/unilife/unilife-server/src/main/java/com/unilife/common/constant/RedisConstant.java b/unilife-server/src/main/java/com/unilife/common/constant/RedisConstant.java similarity index 100% rename from unilife/unilife-server/src/main/java/com/unilife/common/constant/RedisConstant.java rename to unilife-server/src/main/java/com/unilife/common/constant/RedisConstant.java diff --git a/unilife/unilife-server/src/main/java/com/unilife/common/result/Result.java b/unilife-server/src/main/java/com/unilife/common/result/Result.java similarity index 95% rename from unilife/unilife-server/src/main/java/com/unilife/common/result/Result.java rename to unilife-server/src/main/java/com/unilife/common/result/Result.java index 8111a67..f16d1a5 100644 --- a/unilife/unilife-server/src/main/java/com/unilife/common/result/Result.java +++ b/unilife-server/src/main/java/com/unilife/common/result/Result.java @@ -40,10 +40,9 @@ public class Result{ } public static Result success(T data, String message) { - return new Result<>(200, message, data); + return new Result<>(200, message, null); } - /** * 失败返回结果 * @param code 状态码 diff --git a/unilife-server/src/main/java/com/unilife/config/WebMvcConfig.java b/unilife-server/src/main/java/com/unilife/config/WebMvcConfig.java new file mode 100644 index 0000000..e59ecf6 --- /dev/null +++ b/unilife-server/src/main/java/com/unilife/config/WebMvcConfig.java @@ -0,0 +1,40 @@ +package com.unilife.config; + +import com.unilife.interceptor.JwtInterceptor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebMvcConfig implements WebMvcConfigurer { + @Autowired + private JwtInterceptor jwtInterceptor; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(jwtInterceptor).addPathPatterns("/**") + .excludePathPatterns( + "/users/login", + "/users/register", + "/users/code", + "/users/login/code", + "/swagger-resources/**", + "/v2/api-docs/**", + "/doc.html", + "/webjars/**" + ); + } + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOriginPatterns("*") // 允许所有来源,生产环境建议限制为特定域名 + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") + .allowedHeaders("*") + .allowCredentials(true) + .maxAge(3600); + } + +} diff --git a/unilife/unilife-server/src/main/java/com/unilife/controller/UserController.java b/unilife-server/src/main/java/com/unilife/controller/UserController.java similarity index 60% rename from unilife/unilife-server/src/main/java/com/unilife/controller/UserController.java rename to unilife-server/src/main/java/com/unilife/controller/UserController.java index 2e7e822..8e6bd5f 100644 --- a/unilife/unilife-server/src/main/java/com/unilife/controller/UserController.java +++ b/unilife-server/src/main/java/com/unilife/controller/UserController.java @@ -2,20 +2,26 @@ package com.unilife.controller; import com.unilife.common.result.Result; import com.unilife.model.dto.EmailDTO; -import com.unilife.model.dto.LogDTO; import com.unilife.model.dto.LoginDTO; import com.unilife.model.dto.LoginEmailDTO; +import com.unilife.model.dto.RegisterDTO; +import com.unilife.model.vo.LoginVO; +import com.unilife.model.vo.RegisterVO; import com.unilife.service.UserService; +import com.unilife.utils.BaseContext; +import com.unilife.utils.JwtUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; +import java.time.Duration; @Api(tags = "用户管理") @@ -26,16 +32,34 @@ public class UserController { @Autowired private UserService userService; + @Autowired + private JwtUtil jwtUtil; + @Autowired + private StringRedisTemplate stringRedisTemplate; @ApiOperation(value = "用户注册") @PostMapping("register") - public Result register(@RequestBody LoginDTO loginDTO, HttpServletRequest request) { - return userService.register(loginDTO,request); + public Result register(@RequestBody RegisterDTO registerDTO, HttpServletRequest request) { + return userService.register(registerDTO,request); } @ApiOperation(value = "用户登录") @PostMapping("login") - public Result login(@RequestBody LogDTO logDTO,HttpServletRequest request) { return userService.login(logDTO,request); } + public Result login(@RequestBody LoginDTO loginDTO,HttpServletRequest request) { + Result login = userService.login(loginDTO,request); + //登陆成功后生成jwt令牌 + LoginVO vo=(LoginVO) login.getData(); + if (vo == null) { + return login; + } + Long id = vo.getId(); + String token = jwtUtil.generateToken(id); + vo.setToken(token); + + //Threadlocal保存当前用户id + BaseContext.setId(id); + return Result.success(vo); + } @ApiOperation(value = "获取邮箱验证码") @PostMapping("code") diff --git a/unilife-server/src/main/java/com/unilife/interceptor/JwtInterceptor.java b/unilife-server/src/main/java/com/unilife/interceptor/JwtInterceptor.java new file mode 100644 index 0000000..33bcea9 --- /dev/null +++ b/unilife-server/src/main/java/com/unilife/interceptor/JwtInterceptor.java @@ -0,0 +1,54 @@ +package com.unilife.interceptor; + +import cn.hutool.core.util.StrUtil; +import com.unilife.utils.BaseContext; +import com.unilife.utils.JwtUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Component +@Slf4j +public class JwtInterceptor implements HandlerInterceptor { + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private StringRedisTemplate stringRedisTemplate; + + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + + log.info("JwtInterceptor preHandle"); + String token = request.getHeader("Authorization"); + + if(StrUtil.isBlank(token)){ + response.setStatus(401); + return false; + } + + boolean verified = jwtUtil.verifyToken(token); + if (!verified) { + response.setStatus(401); + return false; + } + + //从token中获取userid并存入threadlocal + Long userId = jwtUtil.getUserIdFromToken(token); + BaseContext.setId(userId); + + + return true; + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + BaseContext.removeId(); + } +} diff --git a/unilife/unilife-server/src/main/java/com/unilife/mapper/UserMapper.java b/unilife-server/src/main/java/com/unilife/mapper/UserMapper.java similarity index 100% rename from unilife/unilife-server/src/main/java/com/unilife/mapper/UserMapper.java rename to unilife-server/src/main/java/com/unilife/mapper/UserMapper.java diff --git a/unilife/unilife-server/src/main/java/com/unilife/model/dto/EmailDTO.java b/unilife-server/src/main/java/com/unilife/model/dto/EmailDTO.java similarity index 100% rename from unilife/unilife-server/src/main/java/com/unilife/model/dto/EmailDTO.java rename to unilife-server/src/main/java/com/unilife/model/dto/EmailDTO.java diff --git a/unilife/unilife-server/src/main/java/com/unilife/model/dto/LogDTO.java b/unilife-server/src/main/java/com/unilife/model/dto/LoginDTO.java similarity index 91% rename from unilife/unilife-server/src/main/java/com/unilife/model/dto/LogDTO.java rename to unilife-server/src/main/java/com/unilife/model/dto/LoginDTO.java index 68e2c5a..57755af 100644 --- a/unilife/unilife-server/src/main/java/com/unilife/model/dto/LogDTO.java +++ b/unilife-server/src/main/java/com/unilife/model/dto/LoginDTO.java @@ -8,7 +8,7 @@ import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor -public class LogDTO { +public class LoginDTO { private String email; private String password; } diff --git a/unilife/unilife-server/src/main/java/com/unilife/model/dto/LoginEmailDTO.java b/unilife-server/src/main/java/com/unilife/model/dto/LoginEmailDTO.java similarity index 100% rename from unilife/unilife-server/src/main/java/com/unilife/model/dto/LoginEmailDTO.java rename to unilife-server/src/main/java/com/unilife/model/dto/LoginEmailDTO.java diff --git a/unilife/unilife-server/src/main/java/com/unilife/model/dto/LoginDTO.java b/unilife-server/src/main/java/com/unilife/model/dto/RegisterDTO.java similarity index 93% rename from unilife/unilife-server/src/main/java/com/unilife/model/dto/LoginDTO.java rename to unilife-server/src/main/java/com/unilife/model/dto/RegisterDTO.java index 7643b0b..dc85e7a 100644 --- a/unilife/unilife-server/src/main/java/com/unilife/model/dto/LoginDTO.java +++ b/unilife-server/src/main/java/com/unilife/model/dto/RegisterDTO.java @@ -7,7 +7,7 @@ import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor -public class LoginDTO { +public class RegisterDTO { private String username; private String email; private String password; diff --git a/unilife/unilife-server/src/main/java/com/unilife/model/entity/User.java b/unilife-server/src/main/java/com/unilife/model/entity/User.java similarity index 100% rename from unilife/unilife-server/src/main/java/com/unilife/model/entity/User.java rename to unilife-server/src/main/java/com/unilife/model/entity/User.java diff --git a/unilife-server/src/main/java/com/unilife/model/vo/LoginVO.java b/unilife-server/src/main/java/com/unilife/model/vo/LoginVO.java new file mode 100644 index 0000000..fbb63d9 --- /dev/null +++ b/unilife-server/src/main/java/com/unilife/model/vo/LoginVO.java @@ -0,0 +1,21 @@ +package com.unilife.model.vo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class LoginVO { + private Long id; + private String username; + private String nickname; + private String avatar; + private Byte role; + private Byte isVerified; + private Byte status; + private String token; + private String LoginIp; +} + diff --git a/unilife-server/src/main/java/com/unilife/model/vo/RegisterVO.java b/unilife-server/src/main/java/com/unilife/model/vo/RegisterVO.java new file mode 100644 index 0000000..dfc60ad --- /dev/null +++ b/unilife-server/src/main/java/com/unilife/model/vo/RegisterVO.java @@ -0,0 +1,15 @@ +package com.unilife.model.vo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class RegisterVO { + private Long id; + private String username; + private String nickname; + private String loginIp; +} diff --git a/unilife/unilife-server/src/main/java/com/unilife/service/IPLocationService.java b/unilife-server/src/main/java/com/unilife/service/IPLocationService.java similarity index 99% rename from unilife/unilife-server/src/main/java/com/unilife/service/IPLocationService.java rename to unilife-server/src/main/java/com/unilife/service/IPLocationService.java index 9f29fac..bed401e 100644 --- a/unilife/unilife-server/src/main/java/com/unilife/service/IPLocationService.java +++ b/unilife-server/src/main/java/com/unilife/service/IPLocationService.java @@ -1,5 +1,4 @@ package com.unilife.service; - import com.unilife.model.dto.LoginDTO; import javax.servlet.http.HttpServletRequest; diff --git a/unilife/unilife-server/src/main/java/com/unilife/service/UserService.java b/unilife-server/src/main/java/com/unilife/service/UserService.java similarity index 51% rename from unilife/unilife-server/src/main/java/com/unilife/service/UserService.java rename to unilife-server/src/main/java/com/unilife/service/UserService.java index cb4ca5f..6752f6f 100644 --- a/unilife/unilife-server/src/main/java/com/unilife/service/UserService.java +++ b/unilife-server/src/main/java/com/unilife/service/UserService.java @@ -1,22 +1,19 @@ package com.unilife.service; import com.unilife.common.result.Result; -import com.unilife.model.dto.LogDTO; import com.unilife.model.dto.LoginDTO; import com.unilife.model.dto.LoginEmailDTO; +import com.unilife.model.dto.RegisterDTO; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; public interface UserService { - Result register(LoginDTO loginDTO, HttpServletRequest request); + Result register(RegisterDTO registerDTO, HttpServletRequest request); - Result login(LogDTO logDTO, HttpServletRequest request); + Result login(LoginDTO loginDTO,HttpServletRequest request); Result sendVerificationCode(String email,HttpServletRequest request); - Result loginWithEmail(LoginEmailDTO loginEmailDTO, HttpServletRequest request); - - + Result loginWithEmail(LoginEmailDTO loginEmailDTO,HttpServletRequest request); } diff --git a/unilife/unilife-server/src/main/java/com/unilife/service/impl/IPLocationServicelmpl.java b/unilife-server/src/main/java/com/unilife/service/impl/IPLocationServicelmpl.java similarity index 100% rename from unilife/unilife-server/src/main/java/com/unilife/service/impl/IPLocationServicelmpl.java rename to unilife-server/src/main/java/com/unilife/service/impl/IPLocationServicelmpl.java diff --git a/unilife/unilife-server/src/main/java/com/unilife/service/impl/UserServiceImpl.java b/unilife-server/src/main/java/com/unilife/service/impl/UserServiceImpl.java similarity index 86% rename from unilife/unilife-server/src/main/java/com/unilife/service/impl/UserServiceImpl.java rename to unilife-server/src/main/java/com/unilife/service/impl/UserServiceImpl.java index f504040..6df5ada 100644 --- a/unilife/unilife-server/src/main/java/com/unilife/service/impl/UserServiceImpl.java +++ b/unilife-server/src/main/java/com/unilife/service/impl/UserServiceImpl.java @@ -2,18 +2,18 @@ package com.unilife.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.RandomUtil; -import cn.hutool.core.util.StrUtil; import com.unilife.common.constant.RedisConstant; import com.unilife.common.result.Result; import com.unilife.mapper.UserMapper; -import com.unilife.model.dto.LogDTO; import com.unilife.model.dto.LoginDTO; import com.unilife.model.dto.LoginEmailDTO; +import com.unilife.model.dto.RegisterDTO; import com.unilife.model.entity.User; -import com.unilife.model.vo.LogVO; import com.unilife.model.vo.LoginVO; +import com.unilife.model.vo.RegisterVO; import com.unilife.service.IPLocationService; import com.unilife.service.UserService; +import com.unilife.utils.JwtUtil; import com.unilife.utils.RegexUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -29,7 +29,6 @@ import javax.mail.internet.MimeMessage; import javax.servlet.http.HttpServletRequest; import java.time.Duration; -import java.time.LocalDateTime; import java.util.HashMap; import java.util.Map; @@ -39,6 +38,7 @@ import static com.unilife.common.constant.RedisConstant.LOGIN_EMAIL_KEY; @Component @Service public class UserServiceImpl implements UserService { + @Autowired private IPLocationService ipLocationService; @@ -56,53 +56,53 @@ public class UserServiceImpl implements UserService { final int CODE_EXPIRE_MINUTES = 10; final int LIMIT_SECONDS=60; - + @Autowired + private JwtUtil jwtUtil; @Override - public Result register(LoginDTO loginDTO, HttpServletRequest request) { - if(loginDTO.getEmail().isEmpty() || loginDTO.getPassword().isEmpty()) { + public Result register(RegisterDTO registerDTO, HttpServletRequest request) { + if(registerDTO.getEmail().isEmpty() || registerDTO.getPassword().isEmpty()) { return Result.error(400,"邮箱或密码不能为空"); } - if(loginDTO.getPassword().length() < 6) { + if(registerDTO.getPassword().length() < 6) { return Result.error(400,"密码长度过短!"); } - User getuser = userMapper.FindByOnlyEmail(loginDTO.getEmail()); + User getuser = userMapper.FindByOnlyEmail(registerDTO.getEmail()); if(getuser != null) { return Result.error(400,"用户已存在!"); } User user = new User(); - BeanUtil.copyProperties(loginDTO,user); + BeanUtil.copyProperties(registerDTO,user); String IPAddress = ipLocationService.getClientIP(request); String Location = ipLocationService.getIPLocation(IPAddress); user.setLoginIp(Location); userMapper.insert(user); - LoginVO loginVO = new LoginVO(Math.toIntExact(user.getId()),user.getUsername() + RegisterVO registerVO = new RegisterVO(user.getId(),user.getUsername() ,user.getNickname(),user.getLoginIp()); - return Result.success(loginVO); + return Result.success(registerVO); } @Override - public Result login(LogDTO logDTO,HttpServletRequest request) { + public Result login(LoginDTO loginDTO,HttpServletRequest request) { User user = new User(); - BeanUtil.copyProperties(logDTO,user);//将登录的前端传来的消息拷贝给这个user + BeanUtil.copyProperties(loginDTO,user);//将登录的前端传来的消息拷贝给这个user User getuser = userMapper.FindByEmail(user.getEmail(),user.getPassword()); if(getuser == null) { - return Result.error(logDTO,"用户不存在,登录失败!"); + return Result.error(loginDTO,"用户不存在,登录失败!"); } if(!user.getPassword().equals(getuser.getPassword())) { - return Result.error(logDTO,"密码错误,登录失败!"); + return Result.error(loginDTO,"密码错误,登录失败!"); } String LastLogIpLocation = getuser.getLoginIp(); String IPAddress = ipLocationService.getClientIP(request); String Location = ipLocationService.getIPLocation(IPAddress); getuser.setLoginIp(Location); userMapper.UpdateIPLocation(getuser.getEmail(), getuser.getLoginIp()); - LogVO logVO = new LogVO(Math.toIntExact(getuser.getId()), getuser.getUsername(), getuser.getNickname(), - getuser.getAvatar(), getuser.getRole(), getuser.getIsVerified(), getuser.getStatus(),getuser.getLoginIp()); - return Result.success(logVO,"上次登录IP归属地为" + LastLogIpLocation); + LoginVO loginVO = new LoginVO(); + return Result.success(loginVO,"上次登录IP归属地为" + LastLogIpLocation); } @Override @@ -202,9 +202,10 @@ public class UserServiceImpl implements UserService { //6.生成登录凭证 //TODO + String token = jwtUtil.generateToken(user.getId()); // 8. 返回用户信息和登录凭证 Map userInfo = new HashMap<>(); - //HashMap userInfo.put("token", token); + userInfo.put("token", token); userInfo.put("user", user); return Result.success(userInfo); diff --git a/unilife-server/src/main/java/com/unilife/utils/BaseContext.java b/unilife-server/src/main/java/com/unilife/utils/BaseContext.java new file mode 100644 index 0000000..dbfed21 --- /dev/null +++ b/unilife-server/src/main/java/com/unilife/utils/BaseContext.java @@ -0,0 +1,19 @@ +package com.unilife.utils; + +public class BaseContext { + + public static ThreadLocal threadLocal = new ThreadLocal<>(); + + public static void setId(Long id) { + threadLocal.set(id); + } + + public static Long getId() { + return threadLocal.get(); + } + + public static void removeId() { + threadLocal.remove(); + } + +} diff --git a/unilife-server/src/main/java/com/unilife/utils/JwtUtil.java b/unilife-server/src/main/java/com/unilife/utils/JwtUtil.java new file mode 100644 index 0000000..6d3974d --- /dev/null +++ b/unilife-server/src/main/java/com/unilife/utils/JwtUtil.java @@ -0,0 +1,45 @@ +package com.unilife.utils; + +import cn.hutool.core.date.DateTime; +import cn.hutool.jwt.JWTUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +@Component +public class JwtUtil { + @Value("${jwt.secret}") + private String secret; + + @Value("${jwt.expiration}") + public long expiration; + + public String generateToken(Long id) { + DateTime now = DateTime.now(); + DateTime expireTime = new DateTime(now.getTime() + expiration * 1000); + + Map payload = new HashMap<>(); + payload.put("userId", id); + payload.put("created",now.getTime()); + return JWTUtil.createToken(payload,secret.getBytes()); + } + + public boolean verifyToken(String token) { + try{ + JWTUtil.verify(token,secret.getBytes()); + return true; + }catch (Exception e){ + return false; + } + } + public Long getUserIdFromToken(String token) { + try { + return (Long)JWTUtil.parseToken(token).getPayload("userId"); + }catch (Exception e){ + return null; + } + } + +} diff --git a/unilife/unilife-server/src/main/java/com/unilife/utils/RegexPatterns.java b/unilife-server/src/main/java/com/unilife/utils/RegexPatterns.java similarity index 100% rename from unilife/unilife-server/src/main/java/com/unilife/utils/RegexPatterns.java rename to unilife-server/src/main/java/com/unilife/utils/RegexPatterns.java diff --git a/unilife/unilife-server/src/main/java/com/unilife/utils/RegexUtils.java b/unilife-server/src/main/java/com/unilife/utils/RegexUtils.java similarity index 100% rename from unilife/unilife-server/src/main/java/com/unilife/utils/RegexUtils.java rename to unilife-server/src/main/java/com/unilife/utils/RegexUtils.java diff --git a/unilife/unilife-server/src/main/resources/application.yml b/unilife-server/src/main/resources/application.yml similarity index 66% rename from unilife/unilife-server/src/main/resources/application.yml rename to unilife-server/src/main/resources/application.yml index 0eca316..a4cc4fe 100644 --- a/unilife/unilife-server/src/main/resources/application.yml +++ b/unilife-server/src/main/resources/application.yml @@ -1,9 +1,8 @@ server: port: 8087 - forward-headers-strategy: framework spring: datasource: - url: jdbc:mysql://localhost:3306/UniLife?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8 + url: jdbc:mysql://localhost:3306/UniLife?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8 username: root password: zhong20050428 driver-class-name: com.mysql.cj.jdbc.Driver @@ -24,10 +23,6 @@ spring: redis: host: 127.0.0.1 port: 6379 - #电脑上要下载redis客户端! - #每次使用发送邮件功能前,需要在redis目录下启动cmd,并执行redis-server.exe redis.windows.conf打开服务 - #若出现一个盒子状的东西和port参数,服务启动成功,此时不要关闭命令行窗口,否则会关闭redis服务!!! - #只有redis服务启动成功,邮件才能正常发送! knife4j: enable: true openapi: @@ -52,4 +47,7 @@ mybatis: map-underscore-to-camel-case: true logging: level: - com.unilife: debug \ No newline at end of file + com.unilife: debug +jwt: + secret: qwertyuiopasdfghjklzxcvbnm + expiration: 86400 \ No newline at end of file diff --git a/unilife/unilife-server/src/main/resources/ipdb/ip2region.xdb b/unilife-server/src/main/resources/ipdb/ip2region.xdb similarity index 100% rename from unilife/unilife-server/src/main/resources/ipdb/ip2region.xdb rename to unilife-server/src/main/resources/ipdb/ip2region.xdb diff --git a/unilife/unilife-server/src/main/resources/mappers/UserMapper.xml b/unilife-server/src/main/resources/mappers/UserMapper.xml similarity index 100% rename from unilife/unilife-server/src/main/resources/mappers/UserMapper.xml rename to unilife-server/src/main/resources/mappers/UserMapper.xml diff --git a/unilife/unilife-server/src/main/resources/static/index.html b/unilife-server/src/main/resources/static/index.html similarity index 100% rename from unilife/unilife-server/src/main/resources/static/index.html rename to unilife-server/src/main/resources/static/index.html diff --git a/unilife/unilife-server/src/test/java/com/unilife/BackendApplicationTests.java b/unilife-server/src/test/java/com/unilife/BackendApplicationTests.java similarity index 100% rename from unilife/unilife-server/src/test/java/com/unilife/BackendApplicationTests.java rename to unilife-server/src/test/java/com/unilife/BackendApplicationTests.java diff --git a/unilife/Front/vue-unilife/src/api/user.js b/unilife/Front/vue-unilife/src/api/user.js deleted file mode 100644 index 1160635..0000000 --- a/unilife/Front/vue-unilife/src/api/user.js +++ /dev/null @@ -1,37 +0,0 @@ -import request from '../utils/request'; - -// 用户注册 -export function register(data) { - return request({ - url: '/auth/register', - method: 'post', - data - }); -} - -// 用户密码登录 -export function login(data) { - return request({ - url: '/auth/login', - method: 'post', - data - }); -} - -// 获取邮箱验证码 -export function getEmailCode(data) { - return request({ - url: '/auth/email/code', - method: 'post', - data - }); -} - -// 邮箱验证码登录 -export function loginWithCode(data) { - return request({ - url: '/auth/login/code', - method: 'post', - data - }); -} \ No newline at end of file diff --git a/unilife/unilife-server/src/main/java/com/unilife/model/vo/LogVO.java b/unilife/unilife-server/src/main/java/com/unilife/model/vo/LogVO.java deleted file mode 100644 index 775aa1c..0000000 --- a/unilife/unilife-server/src/main/java/com/unilife/model/vo/LogVO.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.unilife.model.vo; - -import com.unilife.model.entity.User; -import lombok.Data; - -@Data -public class LogVO { - private Integer id; - private String username; - private String nickname; - private String avatar; - private Byte role; - private Byte isVerified; - private Byte status; - private String loginIp; - - public LogVO(Integer id,String username,String nickname,String avatar,Byte role,Byte isVerified,Byte status,String loginIp) - { - this.id = id; - this.username = username; - this.nickname = nickname; - this.avatar = avatar; - this.role = role; - this.isVerified = isVerified; - this.status = status; - this.loginIp = loginIp; - } - -} - diff --git a/unilife/unilife-server/src/main/java/com/unilife/model/vo/LoginVO.java b/unilife/unilife-server/src/main/java/com/unilife/model/vo/LoginVO.java deleted file mode 100644 index 56dff50..0000000 --- a/unilife/unilife-server/src/main/java/com/unilife/model/vo/LoginVO.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.unilife.model.vo; - -import lombok.Data; - -@Data -public class LoginVO { - private Integer id; - private String username; - private String nickname; - private String loginIp; - - public LoginVO(Integer id, String username, String nickname,String loginIp) - { - this.id = id; - this.username = username; - this.nickname = nickname; - this.loginIp = loginIp; - } -} diff --git a/unilife/图片素材/1.jpg b/图片素材/1.jpg similarity index 100% rename from unilife/图片素材/1.jpg rename to 图片素材/1.jpg diff --git a/unilife/文档/v1.md b/文档/v1.md similarity index 100% rename from unilife/文档/v1.md rename to 文档/v1.md diff --git a/unilife/文档/v2.md b/文档/v2.md similarity index 100% rename from unilife/文档/v2.md rename to 文档/v2.md diff --git a/unilife/文档/大模型互动过程.md b/文档/大模型互动过程.md similarity index 100% rename from unilife/文档/大模型互动过程.md rename to 文档/大模型互动过程.md diff --git a/unilife/文档/焦点访谈报告.md b/文档/焦点访谈报告.md similarity index 100% rename from unilife/文档/焦点访谈报告.md rename to 文档/焦点访谈报告.md