Explorar o código

框架已经搭建好,等待开发。

soft5566 %!s(int64=3) %!d(string=hai) anos
pai
achega
b5f3550514

+ 22 - 0
pom.xml

@@ -21,12 +21,34 @@
 			<groupId>org.springframework.boot</groupId>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-web</artifactId>
 			<artifactId>spring-boot-starter-web</artifactId>
 		</dependency>
 		</dependency>
+		<dependency>
+			<groupId>io.jsonwebtoken</groupId>
+			<artifactId>jjwt</artifactId>
+			<version>0.9.1</version>
+		</dependency>
 		<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
 		<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
 		<dependency>
 		<dependency>
 			<groupId>com.baomidou</groupId>
 			<groupId>com.baomidou</groupId>
 			<artifactId>mybatis-plus-boot-starter</artifactId>
 			<artifactId>mybatis-plus-boot-starter</artifactId>
 			<version>3.5.2</version>
 			<version>3.5.2</version>
 		</dependency>
 		</dependency>
+		<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
+		<dependency>
+			<groupId>com.alibaba.fastjson2</groupId>
+			<artifactId>fastjson2</artifactId>
+			<version>2.0.33</version>
+		</dependency>
+		<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
+		<dependency>
+			<groupId>com.github.pagehelper</groupId>
+			<artifactId>pagehelper-spring-boot-starter</artifactId>
+			<version>1.4.7</version>
+		</dependency>
+		<dependency>
+			<groupId>com.squareup.okhttp3</groupId>
+			<artifactId>okhttp</artifactId>
+			<version>4.9.1</version>
+		</dependency>
 		<dependency>
 		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-devtools</artifactId>
 			<artifactId>spring-boot-devtools</artifactId>

+ 0 - 19
src/main/java/com/ch/jiaoxuelou_houtai/config/MyBatisPlusConfig.java

@@ -1,19 +0,0 @@
-package com.ch.jiaoxuelou_houtai.config;
-
-import com.baomidou.mybatisplus.annotation.DbType;
-import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
-import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-@Configuration
-public class MyBatisPlusConfig {
-    @Bean
-    public MybatisPlusInterceptor paginationInterceptor() {
-        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
-        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
-        interceptor.addInnerInterceptor(paginationInterceptor);
-
-        return interceptor;
-    }
-}

+ 20 - 0
src/main/java/com/ch/jiaoxuelou_houtai/config/WebMvcConfig.java

@@ -0,0 +1,20 @@
+package com.ch.jiaoxuelou_houtai.config;
+
+import com.ch.jiaoxuelou_houtai.core.interceptor.JwtInterceptor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+@Configuration
+public class WebMvcConfig implements WebMvcConfigurer {
+    @Autowired
+    private JwtInterceptor jwtInterceptor;
+
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+        registry.addInterceptor(jwtInterceptor)
+                .addPathPatterns("/**")
+                .excludePathPatterns("/login");
+    }
+}

+ 27 - 0
src/main/java/com/ch/jiaoxuelou_houtai/controller/ApiController.java

@@ -0,0 +1,27 @@
+package com.ch.jiaoxuelou_houtai.controller;
+
+import com.alibaba.fastjson2.JSON;
+import com.ch.jiaoxuelou_houtai.core.ResponseResult;
+import com.ch.jiaoxuelou_houtai.service.ApiService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.IOException;
+
+@RestController
+@RequestMapping("/thapi")
+public class ApiController {
+
+    @Autowired
+    private ApiService apiService;
+
+    @GetMapping("/api")
+    public ResponseResult callApi(@RequestParam(value = "page", defaultValue = "1") int page,
+                          @RequestParam(value = "rows", defaultValue = "10") int rows) throws IOException {
+        String url = "https://chtech.ncjti.edu.cn/air-conditioner-control/airManage/buildqueryEnergyRank.action";
+        return ResponseResult.success(JSON.parse(apiService.callApi(url, page, rows)));
+    }
+}

+ 64 - 10
src/main/java/com/ch/jiaoxuelou_houtai/controller/TestController.java

@@ -1,13 +1,15 @@
 package com.ch.jiaoxuelou_houtai.controller;
 package com.ch.jiaoxuelou_houtai.controller;
 
 
-import com.baomidou.mybatisplus.core.metadata.IPage;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.alibaba.fastjson2.JSONObject;
+import com.ch.jiaoxuelou_houtai.core.ResponseResult;
 import com.ch.jiaoxuelou_houtai.entity.User;
 import com.ch.jiaoxuelou_houtai.entity.User;
 import com.ch.jiaoxuelou_houtai.service.UserService;
 import com.ch.jiaoxuelou_houtai.service.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+import java.util.Map;
+
 
 
 @RestController
 @RestController
 @RequestMapping("/api")
 @RequestMapping("/api")
@@ -16,12 +18,64 @@ public class TestController {
     @Autowired
     @Autowired
     private UserService userService;
     private UserService userService;
 
 
-    @GetMapping("/hello")
-    public IPage hello() {
-        Page<User> userPage = new Page<>(2, 2);
-        Page<User> page = userService.page(userPage);
 
 
-        return page;
+    @RequestMapping(value = "/hello", method = RequestMethod.GET)
+    public ResponseResult hello(@RequestBody JSONObject jsonObject) {
+        int pageNum = jsonObject.getInteger("pageNum");
+        int pageSize = jsonObject.getInteger("pageSize");
+
+        return ResponseResult.success(userService.findAll(pageNum, pageSize));
+    }
+
+    /*
+     * 第一种:以RequestParam接收
+     * http://localhost:8080/test1?id=1
+     * */
+    @RequestMapping(value = "/test1", method = RequestMethod.GET)
+    public ResponseResult test1(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
+                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
+
+        return ResponseResult.success(userService.findAll(pageNum, pageSize));
+    }
+
+    /*
+     * 第二种:以实体类接收
+     * {"username": "zhangsan","id":"2"}
+     * */
+    @RequestMapping(value = "/test2", method = RequestMethod.POST)
+    public void test1(@RequestBody User user) {
+        System.out.println("username:" + user.getUsername());
     }
     }
 
 
+    /*
+     * 第三种:以Map接收
+     * {"username": "zhangsan","id":"2"}
+     * */
+    @RequestMapping(value = "/test3", method = RequestMethod.POST)
+    public void test3(@RequestBody Map<String, String> map) {
+        System.out.println("username:" + map.get("username"));
+    }
+
+    /*
+     * 第四种:以List接收
+     * [{"username": "zhangsan","id":"2"},{"username": "lisi","id":"1"}]
+     * */
+    @RequestMapping(value = "/test4", method = RequestMethod.POST)
+    public void test4(@RequestBody List<User> list) {
+        for (User user : list) {
+            System.out.println("username:" + user.getUsername());
+        }
+    }
+
+    /*
+     * 第五种:以JSON对象接收
+     * {"username": "zhangsan","id":"2","role":{"rolename":"admin"}}
+     * */
+    @RequestMapping(value = "/test5", method = RequestMethod.POST)
+    public void test5(@RequestBody JSONObject json) {
+        System.out.println("username:" + json.getString("username"));
+        System.out.println("rolename:" + json.getJSONObject("role").getString("rolename"));
+    }
+
+
 }
 }

+ 54 - 0
src/main/java/com/ch/jiaoxuelou_houtai/controller/UserController.java

@@ -0,0 +1,54 @@
+package com.ch.jiaoxuelou_houtai.controller;
+
+import com.ch.jiaoxuelou_houtai.core.ResponseResult;
+import com.ch.jiaoxuelou_houtai.entity.User;
+import com.ch.jiaoxuelou_houtai.mapper.UserMapper;
+import com.ch.jiaoxuelou_houtai.util.JwtUtil;
+import com.ch.jiaoxuelou_houtai.util.PasswordEncryptionUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@RestController
+public class UserController {
+    @Autowired
+    private JwtUtil jwtUtil;
+
+    @Autowired
+    private UserMapper userMapper;
+
+    @PostMapping("/login")
+    public ResponseResult login(@RequestBody User user) {
+        User u = new User();
+        u.setUsername(user.getUsername());
+        // 检验用户是否在数据库中
+        List<User> login = userMapper.login(u);
+        if (login.size() > 0) {
+            String s = PasswordEncryptionUtil.md5Hash(PasswordEncryptionUtil.sha2Hash(user.getPassword()));
+            if (s.equals(login.get(0).getPassword())) {
+                // 生成token
+                String token = jwtUtil.generateToken(user.getUsername());
+                Map<String, String> strMap = new HashMap<>();
+                strMap.put("token", token);
+
+                return ResponseResult.success(strMap);
+            } else {
+                return ResponseResult.failed("用户名或密码错误!");
+            }
+        } else {
+            return ResponseResult.failed("用户名或密码错误!");
+        }
+    }
+
+    @GetMapping("/secure")
+    public boolean secureEndpoint(@RequestHeader("token") String authHeader) {
+        String token = authHeader.substring(10); // 移除 "CHUANGHAI " 前缀
+        User user = new User();
+        user.setUsername(jwtUtil.getUsernameFromToken(token));
+        // 检验 token 和 用户名
+        return jwtUtil.validateToken(token, user);
+    }
+}

+ 49 - 0
src/main/java/com/ch/jiaoxuelou_houtai/core/ResponseResult.java

@@ -0,0 +1,49 @@
+package com.ch.jiaoxuelou_houtai.core;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+
+/**
+ * 请求响应体
+ *
+ * @author zrx
+ */
+@Data
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+public class ResponseResult implements Serializable {
+
+	private static final long serialVersionUID = 8041766238120354183L;
+
+	private int code;
+	private String status;
+	private String msg;
+	private Object data;
+
+	public static ResponseResult success(Object data) {
+		return ResponseResult.builder()
+				.status(ResponseStatus.SUCCESS.valueEn)
+				.msg(ResponseStatus.SUCCESS.valueZh)
+				.code(ResponseStatus.SUCCESS.bCode)
+				.data(data)
+				.build();
+	}
+
+	public static ResponseResult failed(String msg) {
+		return failed(ResponseStatus.FAILED.bCode, msg);
+	}
+
+	public static ResponseResult failed(int code, String msg) {
+		return ResponseResult.builder()
+				.status(ResponseStatus.FAILED.valueEn)
+				.msg(msg)
+				.code(code)
+				.build();
+	}
+
+}

+ 149 - 0
src/main/java/com/ch/jiaoxuelou_houtai/core/ResponseStatus.java

@@ -0,0 +1,149 @@
+package com.ch.jiaoxuelou_houtai.core;
+
+/**
+ * 响应状态枚举
+ *
+ * @author zrx
+ */
+public enum ResponseStatus {
+
+	/**
+	 * 成功
+	 */
+	SUCCESS(200, 200, "yes", "成功", "返回成功"),
+
+	/**
+	 * 失败
+	 */
+	FAILED(-200, 417, "no", "失败", "异常失败"),
+
+	/**
+	 * 业务异常
+	 */
+	BUSINESS_EXCEPTION(2000, 200, "no", "网络连接不稳~请稍后再试", "业务异常"),
+
+	/**
+	 * 网络请求异常
+	 */
+	HTTP_CLIENT_EXCEPTION(4000, 400, "no", "网络连接不稳~请稍后再试", "网络请求异常: 第三方业务内部异常,请检查所访问的第三方业务是否正常"),
+
+	/**
+	 * 网络请求异常:第三方服务连接异常:请检查访问路径是否正常
+	 */
+	HTTP_CLIENT_CONNECTION_EXCEPTION(4004, 400, "no", "网络连接不稳~请稍后再试", "网络请求异常:第三方服务连接异常,请检查第三方访问路径是否正常"),
+
+	/**
+	 * 数据库异常
+	 */
+	DATA_ACCESS_EXCEPTION(6000, 500, "no", "服务器开小差~请稍后再试", "sql异常"),
+
+	/**
+	 * Valid参数校验异常
+	 */
+	VALID_EXCEPTION(5000, 400, "no", "服务器开小差~请稍后再试", "Valid参数校验异常"),
+
+	/**
+	 * 栈溢出
+	 */
+	STACK_OVERFLOW_ERROR(5001, 500, "no", "服务器开小差~请稍后再试", "栈溢出"),
+
+	/**
+	 * 其他错误
+	 */
+	OTHER_EXCEPTION(5002, 500, "no", "服务器开小差~请稍后再试", "其他错误"),
+
+	/**
+	 * 类型转换不支持异常
+	 */
+	CONVERSION_NOT_SUPPORTED_EXCEPTION(5003, 500, "no", "服务器开小差~请稍后再试", "类型转换不支持异常"),
+
+	/**
+	 * io异常
+	 */
+	IOException(5004, 500, "no", "服务器开小差~请稍后再试", "io异常"),
+
+	/**
+	 * 空指针异常
+	 */
+	NULL_POINTER_EXCEPTION(5005, 500, "no", "服务器开小差~请稍后再试", "空指针异常,请检查参数是否为空"),
+
+	/**
+	 * 未知方法异常
+	 */
+	NO_SUCH_METHOD_EXCEPTION(5006, 500, "no", "服务器开小差~请稍后再试", "未知方法异常"),
+
+	/**
+	 * 数组越界异常
+	 */
+	INDEX_OUT_OF_BOUNDS_EXCEPTION(5007, 500, "no", "服务器开小差~请稍后再试", "数组越界异常,请检查数组大小"),
+
+	/**
+	 * 无法找到该资源
+	 */
+	NOT_FOUND_EXCEPTION(5008, 500, "no", "无法找到该资源", "无法找到该资源"),
+
+	/**
+	 * 无授权访问,请先登录
+	 */
+	NOT_AUTHORIZED_EXCEPTION(401, 401, "no", "无授权访问,请先登录", "无授权访问,请先登录"),
+
+	/**
+	 * 类型不匹配异常
+	 */
+	TYPE_MISMATCH_EXCEPTION(400, 400, "no", "服务器开小差~请稍后再试", "类型不匹配异常"),
+
+	/**
+	 * 类型不匹配异常
+	 */
+	MISSING_SERVLET_REQUEST_PARAMETER_EXCEPTION(400, 400, "no", "服务器开小差~请稍后再试", "类型不匹配异常"),
+
+	/**
+	 * http媒体类型不支持异常
+	 */
+	NOT_ACCEPTABLE(406, 406, "no", "服务器开小差~请稍后再试", "http媒体类型不支持异常"),
+
+	/**
+	 * 方法不允许
+	 */
+	METHOD_NOT_ALLOWED(405, 405, "no", "方法不允许", "方法不允许"),
+
+	/**
+	 * 运行时异常
+	 */
+	RUNTIME_EXCEPTION(500, 500, "no", "服务器开小差~请稍后再试", "运行时异常"),
+
+	/**
+	 * 类型转换异常
+	 */
+	CLASS_CAST_EXCEPTION(5009, 500, "no", "服务器开小差~请稍后再试", "类型转换异常");
+
+	/**
+	 * 业务code
+	 */
+	public Integer bCode;
+	/**
+	 * http code
+	 */
+	public Integer hCode;
+	/**
+	 * 英文状态值
+	 */
+	public String valueEn;
+	/**
+	 * 返回给前端的中文说明
+	 */
+	public String valueZh;
+	/**
+	 * 存储日志的中文说明
+	 */
+	public String valueLog;
+
+	ResponseStatus(Integer bCode, Integer hCode, String valueEn, String valueZh, String valueLog) {
+		this.bCode = bCode;
+		this.hCode = hCode;
+		this.valueEn = valueEn;
+		this.valueZh = valueZh;
+		this.valueLog = valueLog;
+	}
+
+}

+ 37 - 0
src/main/java/com/ch/jiaoxuelou_houtai/core/interceptor/JwtInterceptor.java

@@ -0,0 +1,37 @@
+package com.ch.jiaoxuelou_houtai.core.interceptor;
+
+import com.ch.jiaoxuelou_houtai.entity.User;
+import com.ch.jiaoxuelou_houtai.util.JwtUtil;
+import org.jetbrains.annotations.NotNull;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Component;
+import org.springframework.web.servlet.HandlerInterceptor;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@Component
+public class JwtInterceptor implements HandlerInterceptor {
+    @Autowired
+    private JwtUtil jwtUtil;
+
+    @Override
+    public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) throws Exception {
+        String authHeader = request.getHeader("token");
+        if (authHeader != null && authHeader.startsWith("CHUANGHAI ")) {
+            String token = authHeader.substring(10);
+            User user = new User();
+            user.setUsername(jwtUtil.getUsernameFromToken(token));
+            if (jwtUtil.validateToken(token, user)) {
+                return true;
+            } else {
+                response.setStatus(HttpStatus.UNAUTHORIZED.value());
+                return false;
+            }
+        } else {
+            response.setStatus(HttpStatus.UNAUTHORIZED.value());
+            return false;
+        }
+    }
+}

+ 1 - 2
src/main/java/com/ch/jiaoxuelou_houtai/entity/User.java

@@ -5,12 +5,11 @@ import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.Data;
 import lombok.Data;
 
 
-import java.io.Serializable;
 import java.sql.Timestamp;
 import java.sql.Timestamp;
 
 
 @TableName("user")
 @TableName("user")
 @Data
 @Data
-public class User implements Serializable {
+public class User {
     @TableId(value = "id", type = IdType.AUTO)
     @TableId(value = "id", type = IdType.AUTO)
     private Integer id;
     private Integer id;
     private String username;
     private String username;

+ 9 - 4
src/main/java/com/ch/jiaoxuelou_houtai/mapper/UserMapper.java

@@ -1,9 +1,14 @@
 package com.ch.jiaoxuelou_houtai.mapper;
 package com.ch.jiaoxuelou_houtai.mapper;
 
 
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.ch.jiaoxuelou_houtai.entity.User;
 import com.ch.jiaoxuelou_houtai.entity.User;
-import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
 
 
-@Mapper
-public interface UserMapper extends BaseMapper<User> {
+import java.util.List;
+
+public interface UserMapper {
+    @Select("SELECT * FROM user")
+    List<User> findAll();
+
+    @Select("SELECT username, password FROM user WHERE username = #{username} limit 1")
+    List<User> login(User user);
 }
 }

+ 33 - 0
src/main/java/com/ch/jiaoxuelou_houtai/service/ApiService.java

@@ -0,0 +1,33 @@
+package com.ch.jiaoxuelou_houtai.service;
+
+import okhttp3.HttpUrl;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
+import org.springframework.stereotype.Service;
+
+import java.io.IOException;
+import java.util.Objects;
+
+@Service
+public class ApiService {
+    private final OkHttpClient httpClient = new OkHttpClient();
+    public String callApi(String url, int page, int rows) throws IOException {
+        HttpUrl.Builder httpBuilder = Objects.requireNonNull(HttpUrl.parse(url)).newBuilder();
+        httpBuilder.addQueryParameter("page", String.valueOf(page));
+        httpBuilder.addQueryParameter("rows", String.valueOf(rows));
+        String strUrl = httpBuilder.build().toString();
+
+        Request request = new Request.Builder()
+                .url(strUrl)
+                .build();
+
+        try (Response response = httpClient.newCall(request).execute()) {
+            if (response.body() != null) {
+                return response.body().string();
+            } else {
+                return null;
+            }
+        }
+    }
+}

+ 7 - 2
src/main/java/com/ch/jiaoxuelou_houtai/service/UserService.java

@@ -1,7 +1,12 @@
 package com.ch.jiaoxuelou_houtai.service;
 package com.ch.jiaoxuelou_houtai.service;
 
 
-import com.baomidou.mybatisplus.extension.service.IService;
 import com.ch.jiaoxuelou_houtai.entity.User;
 import com.ch.jiaoxuelou_houtai.entity.User;
+import com.github.pagehelper.PageInfo;
 
 
-public interface UserService extends IService<User> {
+import java.util.List;
+
+public interface UserService {
+    PageInfo<User> findAll(int pageNum, int pageSize);
+
+    List<User> login(User user);
 }
 }

+ 21 - 2
src/main/java/com/ch/jiaoxuelou_houtai/service/impl/UserServiceImpl.java

@@ -1,11 +1,30 @@
 package com.ch.jiaoxuelou_houtai.service.impl;
 package com.ch.jiaoxuelou_houtai.service.impl;
 
 
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ch.jiaoxuelou_houtai.entity.User;
 import com.ch.jiaoxuelou_houtai.entity.User;
 import com.ch.jiaoxuelou_houtai.mapper.UserMapper;
 import com.ch.jiaoxuelou_houtai.mapper.UserMapper;
 import com.ch.jiaoxuelou_houtai.service.UserService;
 import com.ch.jiaoxuelou_houtai.service.UserService;
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
+import java.util.List;
+
 @Service
 @Service
-public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
+public class UserServiceImpl implements UserService {
+
+    @Autowired
+    private UserMapper userMapper;
+
+    @Override
+    public PageInfo<User> findAll(int pageNum, int pageSize) {
+        PageHelper.startPage(pageNum, pageSize);
+        List<User> userList = userMapper.findAll();
+        return new PageInfo<>(userList);
+    }
+
+    @Override
+    public List<User> login(User user) {
+        return userMapper.login(user);
+    }
 }
 }

+ 48 - 0
src/main/java/com/ch/jiaoxuelou_houtai/util/JwtUtil.java

@@ -0,0 +1,48 @@
+package com.ch.jiaoxuelou_houtai.util;
+
+import com.ch.jiaoxuelou_houtai.entity.User;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+
+@Component
+public class JwtUtil {
+    private final String secret = "ch!@#123";
+
+    public String generateToken(String username) {
+        Date now = new Date();
+        Date expiryDate = new Date(now.getTime() + 3600000 * 24); // 7天
+        String token = Jwts.builder()
+                .setSubject(username)
+                .setIssuedAt(now)
+                .setExpiration(expiryDate)
+                .signWith(SignatureAlgorithm.HS512, secret)
+                .compact();
+        String prefix = "CHUANGHAI ";
+        return prefix + token;
+    }
+
+    public String getUsernameFromToken(String token) {
+        return Jwts.parser()
+                .setSigningKey(secret)
+                .parseClaimsJws(token)
+                .getBody()
+                .getSubject();
+    }
+
+    public boolean validateToken(String token, User user) {
+        String username = getUsernameFromToken(token);
+        return (username.equals(user.getUsername()) && !isTokenExpired(token));
+    }
+
+    private boolean isTokenExpired(String token) {
+        Date expiryDate = Jwts.parser()
+                .setSigningKey(secret)
+                .parseClaimsJws(token)
+                .getBody()
+                .getExpiration();
+        return expiryDate.before(new Date());
+    }
+}

+ 43 - 0
src/main/java/com/ch/jiaoxuelou_houtai/util/PasswordEncryptionUtil.java

@@ -0,0 +1,43 @@
+package com.ch.jiaoxuelou_houtai.util;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+public class PasswordEncryptionUtil {
+    final static String salt = "ch!@#qwe";
+
+    public static String md5Hash(String password) {
+        String input = password + salt;
+        StringBuilder md5Hash = new StringBuilder();
+
+        try {
+            MessageDigest md = MessageDigest.getInstance("MD5");
+            byte[] messageDigest = md.digest(input.getBytes());
+
+            for (byte b : messageDigest) {
+                md5Hash.append(String.format("%02x", b));
+            }
+        } catch (NoSuchAlgorithmException e) {
+            System.out.println(e.getMessage());
+        }
+
+        return md5Hash.toString();
+    }
+
+    public static String sha2Hash(String input) {
+        StringBuilder sha2Hash = new StringBuilder();
+
+        try {
+            MessageDigest md = MessageDigest.getInstance("SHA-256");
+            byte[] messageDigest = md.digest(input.getBytes());
+
+            for (byte b : messageDigest) {
+                sha2Hash.append(String.format("%02x", b));
+            }
+        } catch (NoSuchAlgorithmException e) {
+            System.out.println(e.getMessage());
+        }
+
+        return sha2Hash.toString();
+    }
+}

+ 8 - 9
src/main/resources/application.yml

@@ -7,15 +7,6 @@ server:
 
 
 
 
 spring:
 spring:
-  redis:
-    host: 127.0.0.1
-    port: 6379
-    password:
-    timeout: 10000
-    lettuce.pool.max-active: 10
-    lettuce.pool.max-wait: 5
-    lettuce.pool.max-idle: 8
-    lettuce.pool.min-idle: 0
   datasource:
   datasource:
     username: root
     username: root
     password: 123456
     password: 123456
@@ -40,5 +31,13 @@ spring:
       enabled: true
       enabled: true
     additional-paths: src/main/java
     additional-paths: src/main/java
 
 
+#分页pageHelper
+pagehelper:
+  helperDialect: mysql
+  reasonable: true  #为了使用输入页数为负或者超出最大页时候使页数为最小或最大值
+  supportMethodsArguments: true
+  params: count=countSql
+  pageSizeZero: true
+