Initial commit
This commit is contained in:
commit
894f8741e7
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea/modules.xml
|
||||
.idea/jarRepositories.xml
|
||||
.idea/compiler.xml
|
||||
.idea/libraries/
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
30
.idea/dataSources.xml
generated
Normal file
30
.idea/dataSources.xml
generated
Normal file
@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="whys" uuid="f20c1fd2-0003-46db-b2a0-3833884c9c44">
|
||||
<driver-ref>mysql.8</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:mysql://114.115.137.102:3306</jdbc-url>
|
||||
<jdbc-additional-properties>
|
||||
<property name="com.intellij.clouds.kubernetes.db.host.port" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.container.port" />
|
||||
</jdbc-additional-properties>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
</data-source>
|
||||
<data-source source="LOCAL" name="ALMA" uuid="828b7b17-6754-4f9e-98a1-9f79aa309307">
|
||||
<driver-ref>mysql.8</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:mysql://10.11.25.79:3306</jdbc-url>
|
||||
<jdbc-additional-properties>
|
||||
<property name="com.intellij.clouds.kubernetes.db.host.port" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.resource.type" value="Deployment" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.container.port" />
|
||||
</jdbc-additional-properties>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
||||
7
.idea/encodings.xml
generated
Normal file
7
.idea/encodings.xml
generated
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
14
.idea/misc.xml
generated
Normal file
14
.idea/misc.xml
generated
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="graalvm-ce-17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
67
pom.xml
Normal file
67
pom.xml
Normal file
@ -0,0 +1,67 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.1.3</version>
|
||||
</parent>
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>big-event</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>big-event</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!--web依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!--mybatis依赖-->
|
||||
<!-- MyBatis Spring Boot Starter 依赖,用于集成 MyBatis 和 Spring Boot -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--lombok依赖-->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.auth0</groupId>
|
||||
<artifactId>java-jwt</artifactId>
|
||||
<version>3.14.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
20
src/main/java/org/example/BigEventApplication.java
Normal file
20
src/main/java/org/example/BigEventApplication.java
Normal file
@ -0,0 +1,20 @@
|
||||
package org.example;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class BigEventApplication
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
// SpringApplication.run(BigEventApplication.class,args);
|
||||
SpringApplication.run(BigEventApplication.class,args);
|
||||
|
||||
}
|
||||
}
|
||||
25
src/main/java/org/example/config/WebConfig.java
Normal file
25
src/main/java/org/example/config/WebConfig.java
Normal file
@ -0,0 +1,25 @@
|
||||
package org.example.config;
|
||||
|
||||
import org.example.interceptors.LoginInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Configurable;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
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 WebConfig implements WebMvcConfigurer {
|
||||
|
||||
|
||||
@Autowired
|
||||
private LoginInterceptor loginInterceptor;
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
System.out.println("Registering LoginInterceptor...");
|
||||
//login page and login api don't need to be intercepted
|
||||
registry.addInterceptor(loginInterceptor)
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns("/user/login", "/user/register");
|
||||
}
|
||||
}
|
||||
32
src/main/java/org/example/controller/ArticleController.java
Normal file
32
src/main/java/org/example/controller/ArticleController.java
Normal file
@ -0,0 +1,32 @@
|
||||
package org.example.controller;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.example.pojo.Result;
|
||||
import org.example.utils.JwtUtil;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/article")
|
||||
public class ArticleController {
|
||||
@GetMapping("/list")
|
||||
public Result<String> list(/*@RequestHeader(name = "Authorization") String token, HttpServletResponse response*/) {
|
||||
//验证token
|
||||
// try {
|
||||
// Map<String,Object> clains = JwtUtil.parseToken(token);
|
||||
//
|
||||
// }catch (Exception e) {
|
||||
// response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||
// return Result.error("login error");
|
||||
// }
|
||||
//
|
||||
// }
|
||||
return Result.success("all");
|
||||
}
|
||||
}
|
||||
62
src/main/java/org/example/controller/UserController.java
Normal file
62
src/main/java/org/example/controller/UserController.java
Normal file
@ -0,0 +1,62 @@
|
||||
package org.example.controller;
|
||||
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import org.example.pojo.Result;
|
||||
import org.example.pojo.User;
|
||||
import org.example.service.UserService;
|
||||
import org.example.utils.JwtUtil;
|
||||
import org.example.utils.Md5Util;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
@Validated
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@PostMapping("/register")
|
||||
public Result register(@Pattern(regexp = "^[a-zA-Z0-9_-]{6,16}$")String username, @Pattern(regexp = "^[a-zA-Z0-9_-]{6,16}$") String password) {
|
||||
//查询用户
|
||||
System.out.println("Register - Username: " + username);
|
||||
User u = userService.findByUsername(username);
|
||||
if (u != null)
|
||||
return Result.error("用户名已存在");
|
||||
//注册用户
|
||||
userService.register(username, password);
|
||||
return Result.success();
|
||||
}
|
||||
@PostMapping("/login")
|
||||
public Result<String> login(@Pattern(regexp = "^[a-zA-Z0-9_-]{3,16}$") String username, @Pattern(regexp = "^[a-zA-Z0-9_-]{6,16}$") String password) {
|
||||
//查询用户
|
||||
User u = userService.findByUsername(username);
|
||||
if (u == null)
|
||||
return Result.error("用户名不存在");
|
||||
//校验密码
|
||||
if (!u.getPassword().equals(Md5Util.getMD5String(password)))
|
||||
return Result.error("密码错误");
|
||||
//登录成功
|
||||
Map<String,Object> claims = new HashMap<>();
|
||||
claims.put("id", u.getId());
|
||||
claims.put("username", u.getUsername());
|
||||
String token = JwtUtil.genToken(claims);
|
||||
return Result.success(token);
|
||||
}
|
||||
|
||||
@GetMapping("/info")
|
||||
public Result<User> info(@RequestHeader(name = "Authorization") String token) {
|
||||
Map<String, Object> map = JwtUtil.parseToken(token);
|
||||
if (map == null)
|
||||
return Result.error("token无效");
|
||||
User u = userService.findByUsername(map.get("username").toString());
|
||||
if (u == null)
|
||||
return Result.error("用户不存在");
|
||||
return Result.success(u);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package org.example.exception;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.example.pojo.Result;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(Exception.class)
|
||||
public Result handleException(Exception e) {
|
||||
e.printStackTrace();
|
||||
// 提取 e.getMessage() 的值到局部变量,减少重复调用
|
||||
String errorMessage = e.getMessage();
|
||||
// 检查 errorMessage 是否为空或长度为 0,提供默认值 "操作失败"
|
||||
errorMessage = StringUtils.hasLength(errorMessage) ? errorMessage : "操作失败";
|
||||
// 返回结果
|
||||
return Result.error(errorMessage);
|
||||
|
||||
}
|
||||
}
|
||||
33
src/main/java/org/example/interceptors/LoginInterceptor.java
Normal file
33
src/main/java/org/example/interceptors/LoginInterceptor.java
Normal file
@ -0,0 +1,33 @@
|
||||
package org.example.interceptors;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.example.pojo.Result;
|
||||
import org.example.utils.JwtUtil;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class LoginInterceptor implements HandlerInterceptor {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
System.out.println("LoginInterceptor has been initialized.");
|
||||
//令牌验证
|
||||
String token = request.getHeader("Authorization");
|
||||
//token 验证
|
||||
try {
|
||||
|
||||
Map<String,Object> clains = JwtUtil.parseToken(token);
|
||||
System.out.println("Interceptor");
|
||||
return true;
|
||||
|
||||
}catch (Exception e)
|
||||
{
|
||||
response.setStatus(401);
|
||||
System.out.println("Interceptor");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/main/java/org/example/mapper/UserMapper.java
Normal file
16
src/main/java/org/example/mapper/UserMapper.java
Normal file
@ -0,0 +1,16 @@
|
||||
package org.example.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.example.pojo.User;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
//search the username
|
||||
@Select("select * from user where username = #{username}")
|
||||
User findByUsername(String username);
|
||||
|
||||
@Insert("insert into user(username, password,create_time, update_time) values(#{username}, #{password},now(),now())")
|
||||
void add(String username, String password);
|
||||
}
|
||||
20
src/main/java/org/example/pojo/Article.java
Normal file
20
src/main/java/org/example/pojo/Article.java
Normal file
@ -0,0 +1,20 @@
|
||||
package org.example.pojo;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
|
||||
public class Article {
|
||||
private Integer id;//主键ID
|
||||
private String title;//文章标题
|
||||
private String content;//文章内容
|
||||
private String coverImg;//封面图像
|
||||
private String state;//发布状态 已发布|草稿
|
||||
private Integer categoryId;//文章分类id
|
||||
private Integer createUser;//创建人ID
|
||||
private LocalDateTime createTime;//创建时间
|
||||
private LocalDateTime updateTime;//更新时间
|
||||
}
|
||||
15
src/main/java/org/example/pojo/Category.java
Normal file
15
src/main/java/org/example/pojo/Category.java
Normal file
@ -0,0 +1,15 @@
|
||||
package org.example.pojo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class Category {
|
||||
private Integer id;//主键ID
|
||||
private String categoryName;//分类名称
|
||||
private String categoryAlias;//分类别名
|
||||
private Integer createUser;//创建人ID
|
||||
private LocalDateTime createTime;//创建时间
|
||||
private LocalDateTime updateTime;//更新时间
|
||||
}
|
||||
31
src/main/java/org/example/pojo/Result.java
Normal file
31
src/main/java/org/example/pojo/Result.java
Normal file
@ -0,0 +1,31 @@
|
||||
package org.example.pojo;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
//统一响应结果
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
|
||||
public class Result<T> {
|
||||
private Integer code;//业务状态码 0-成功 1-失败
|
||||
private String message;//提示信息
|
||||
private T data;//响应数据
|
||||
|
||||
//快速返回操作成功响应结果(带响应数据)
|
||||
public static <E> Result<E> success(E data) {
|
||||
return new Result<>(0, "操作成功", data);
|
||||
}
|
||||
|
||||
//快速返回操作成功响应结果
|
||||
public static Result success() {
|
||||
return new Result(0, "操作成功", null);
|
||||
}
|
||||
|
||||
public static Result error(String message) {
|
||||
return new Result(1, message, null);
|
||||
}
|
||||
}
|
||||
20
src/main/java/org/example/pojo/User.java
Normal file
20
src/main/java/org/example/pojo/User.java
Normal file
@ -0,0 +1,20 @@
|
||||
package org.example.pojo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
//lombok插件 在编译阶段 为实体类自动生成setter getter toString
|
||||
//pom文件中引入依赖 在实体类上添加注解
|
||||
@Data
|
||||
public class User {
|
||||
private Integer id;//主键ID
|
||||
private String username;//用户名
|
||||
@JsonIgnore
|
||||
private String password;//密码
|
||||
private String nickname;//昵称
|
||||
private String email;//邮箱
|
||||
private String userPic;//用户头像地址
|
||||
private LocalDateTime createTime;//创建时间
|
||||
private LocalDateTime updateTime;//更新时间
|
||||
}
|
||||
11
src/main/java/org/example/service/UserService.java
Normal file
11
src/main/java/org/example/service/UserService.java
Normal file
@ -0,0 +1,11 @@
|
||||
package org.example.service;
|
||||
|
||||
import org.example.pojo.User;
|
||||
|
||||
public interface UserService {
|
||||
//根据用户名查询用户
|
||||
User findByUsername(String username);
|
||||
|
||||
//注册
|
||||
void register(String username, String password);
|
||||
}
|
||||
31
src/main/java/org/example/service/impl/UserServiceImpl.java
Normal file
31
src/main/java/org/example/service/impl/UserServiceImpl.java
Normal file
@ -0,0 +1,31 @@
|
||||
package org.example.service.impl;
|
||||
|
||||
import org.example.mapper.UserMapper;
|
||||
import org.example.pojo.User;
|
||||
import org.example.service.UserService;
|
||||
import org.example.utils.Md5Util;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Override
|
||||
public User findByUsername(String username) {
|
||||
System.out.println("findByUsername - username: " + username);
|
||||
User user = userMapper.findByUsername(username);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(String username, String password) {
|
||||
//加密
|
||||
String md5String = Md5Util.getMD5String(password);
|
||||
//添加
|
||||
userMapper.add(username,md5String);
|
||||
|
||||
}
|
||||
}
|
||||
30
src/main/java/org/example/utils/JwtUtil.java
Normal file
30
src/main/java/org/example/utils/JwtUtil.java
Normal file
@ -0,0 +1,30 @@
|
||||
package org.example.utils;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
public class JwtUtil {
|
||||
|
||||
private static final String KEY = "itheima";
|
||||
|
||||
//接收业务数据,生成token并返回
|
||||
public static String genToken(Map<String, Object> claims) {
|
||||
return JWT.create()
|
||||
.withClaim("claims", claims)
|
||||
.withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 12))
|
||||
.sign(Algorithm.HMAC256(KEY));
|
||||
}
|
||||
|
||||
//接收token,验证token,并返回业务数据
|
||||
public static Map<String, Object> parseToken(String token) {
|
||||
return JWT.require(Algorithm.HMAC256(KEY))
|
||||
.build()
|
||||
.verify(token)
|
||||
.getClaim("claims")
|
||||
.asMap();
|
||||
}
|
||||
|
||||
}
|
||||
73
src/main/java/org/example/utils/Md5Util.java
Normal file
73
src/main/java/org/example/utils/Md5Util.java
Normal file
@ -0,0 +1,73 @@
|
||||
package org.example.utils;
|
||||
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class Md5Util {
|
||||
/**
|
||||
* 默认的密码字符串组合,用来将字节转换成 16 进制表示的字符,apache校验下载的文件的正确性用的就是默认的这个组合
|
||||
*/
|
||||
protected static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
||||
|
||||
protected static MessageDigest messagedigest = null;
|
||||
|
||||
static {
|
||||
try {
|
||||
messagedigest = MessageDigest.getInstance("MD5");
|
||||
} catch (NoSuchAlgorithmException nsaex) {
|
||||
System.err.println(Md5Util.class.getName() + "初始化失败,MessageDigest不支持MD5Util。");
|
||||
nsaex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成字符串的md5校验值
|
||||
*
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public static String getMD5String(String s) {
|
||||
return getMD5String(s.getBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串的md5校验码是否与一个已知的md5码相匹配
|
||||
*
|
||||
* @param password 要校验的字符串
|
||||
* @param md5PwdStr 已知的md5校验码
|
||||
* @return
|
||||
*/
|
||||
public static boolean checkPassword(String password, String md5PwdStr) {
|
||||
String s = getMD5String(password);
|
||||
return s.equals(md5PwdStr);
|
||||
}
|
||||
|
||||
|
||||
public static String getMD5String(byte[] bytes) {
|
||||
messagedigest.update(bytes);
|
||||
return bufferToHex(messagedigest.digest());
|
||||
}
|
||||
|
||||
private static String bufferToHex(byte bytes[]) {
|
||||
return bufferToHex(bytes, 0, bytes.length);
|
||||
}
|
||||
|
||||
private static String bufferToHex(byte bytes[], int m, int n) {
|
||||
StringBuffer stringbuffer = new StringBuffer(2 * n);
|
||||
int k = m + n;
|
||||
for (int l = m; l < k; l++) {
|
||||
appendHexPair(bytes[l], stringbuffer);
|
||||
}
|
||||
return stringbuffer.toString();
|
||||
}
|
||||
|
||||
private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
|
||||
char c0 = hexDigits[(bt & 0xf0) >> 4];// 取字节中高 4 位的数字转换, >>>
|
||||
// 为逻辑右移,将符号位一起右移,此处未发现两种符号有何不同
|
||||
char c1 = hexDigits[bt & 0xf];// 取字节中低 4 位的数字转换
|
||||
stringbuffer.append(c0);
|
||||
stringbuffer.append(c1);
|
||||
}
|
||||
|
||||
}
|
||||
10
src/main/resources/application.yml
Normal file
10
src/main/resources/application.yml
Normal file
@ -0,0 +1,10 @@
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://114.115.137.102:3306/big_event?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
|
||||
username: root
|
||||
password: 123456
|
||||
|
||||
mybatis:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
38
src/test/java/org/example/AppTest.java
Normal file
38
src/test/java/org/example/AppTest.java
Normal file
@ -0,0 +1,38 @@
|
||||
package org.example;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user