init
This commit is contained in:
41
ski-dashboard-common/pom.xml
Normal file
41
ski-dashboard-common/pom.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>com.ski</groupId>
|
||||
<artifactId>ski-dashboard</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.ski.lichuan</groupId>
|
||||
<artifactId>ski-dashboard-common</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.auth0</groupId>
|
||||
<artifactId>java-jwt</artifactId>
|
||||
<version>4.4.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.ski</groupId>
|
||||
<artifactId>ski-dashboard-model</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>3.0.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.ski.lichuan.common;
|
||||
|
||||
//TIP 要<b>运行</b>代码,请按 <shortcut actionId="Run"/> 或
|
||||
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//TIP 当文本光标位于高亮显示的文本处时按 <shortcut actionId="ShowIntentionActions"/>
|
||||
// 查看 IntelliJ IDEA 建议如何修正。
|
||||
System.out.printf("Hello and welcome!");
|
||||
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
//TIP 按 <shortcut actionId="Debug"/> 开始调试代码。我们已经设置了一个 <icon src="AllIcons.Debugger.Db_set_breakpoint"/> 断点
|
||||
// 但您始终可以通过按 <shortcut actionId="ToggleLineBreakpoint"/> 添加更多断点。
|
||||
System.out.println("i = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.ski.lichuan.common.utils;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
@Component
|
||||
public class JwtUtils {
|
||||
// 密钥 application.properties 配置
|
||||
@Value("${JWT_SECRET:your-secret-key}")
|
||||
private String SECRET;
|
||||
// 过期时间(72小时,单位:毫秒)
|
||||
private static final long EXPIRATION = 72 * 60 * 60 * 1000;
|
||||
|
||||
// 生成 Token
|
||||
public String generateToken(String username) {
|
||||
Date expirationDate = new Date(System.currentTimeMillis() + EXPIRATION);
|
||||
return JWT.create()
|
||||
.withSubject(username) // 存储用户名
|
||||
.withExpiresAt(expirationDate) // 设置过期时间
|
||||
.sign(Algorithm.HMAC256(SECRET)); // 签名算法
|
||||
}
|
||||
|
||||
// 从 Token 中获取用户名
|
||||
public String getUsernameFromToken(String token) {
|
||||
return JWT.require(Algorithm.HMAC256(SECRET))
|
||||
.build()
|
||||
.verify(token)
|
||||
.getSubject();
|
||||
}
|
||||
|
||||
// 验证 Token 有效性
|
||||
public boolean validateToken(String token) {
|
||||
try {
|
||||
JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false; // 无效或过期
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.ski.lichuan.mapper;
|
||||
|
||||
import com.ski.lichuan.model.SysUser;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
|
||||
@Select("SELECT id, username, password, nickname, status FROM sys_user WHERE username = #{username}")
|
||||
SysUser selectByUsername(@Param("username") String username);
|
||||
|
||||
@Insert("INSERT INTO sys_user(username, password, nickname, status) VALUES(#{username}, #{password}, #{nickname}, #{status})")
|
||||
@Options(useGeneratedKeys = true, keyProperty = "id")
|
||||
int insertUser(SysUser user);
|
||||
|
||||
@Select("SELECT id, username, password, nickname, status FROM sys_user WHERE id = #{id}")
|
||||
SysUser selectById(@Param("id") Long id);
|
||||
}
|
||||
Reference in New Issue
Block a user