能跑,但是坑巨多,还有很多东西没做完...存档存档一下吧

main
lensfrex 3 years ago
parent 1ce37772d0
commit 5937135d8e
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 18
      pom.xml
  2. 51
      src/main/java/me/lensfrex/littlebusters/api/v1/Register.java
  3. 31
      src/main/java/me/lensfrex/littlebusters/api/v1/beans/requests/LoginRequestBody.java
  4. 31
      src/main/java/me/lensfrex/littlebusters/api/v1/beans/requests/RegisterRequestBody.java
  5. 21
      src/main/java/me/lensfrex/littlebusters/api/v1/beans/responses/LoginResponseData.java
  6. 2
      src/main/java/me/lensfrex/littlebusters/api/v1/beans/responses/ProfileResponseData.java
  7. 2
      src/main/java/me/lensfrex/littlebusters/api/v1/beans/responses/RegisterResponseData.java
  8. 4
      src/main/java/me/lensfrex/littlebusters/api/v1/beans/responses/general/ErrorResponse.java
  9. 2
      src/main/java/me/lensfrex/littlebusters/api/v1/beans/responses/general/ResponseBase.java
  10. 57
      src/main/java/me/lensfrex/littlebusters/api/v1/config/ConfigLoader.java
  11. 12
      src/main/java/me/lensfrex/littlebusters/api/v1/config/GlobalConfig.java
  12. 9
      src/main/java/me/lensfrex/littlebusters/api/v1/dao/KeyDatabase.java
  13. 10
      src/main/java/me/lensfrex/littlebusters/api/v1/dao/UserInformation.java
  14. 7
      src/main/java/me/lensfrex/littlebusters/api/v1/dao/UserOperators.java
  15. 11
      src/main/java/me/lensfrex/littlebusters/api/v1/exceptions/ConfigureFileInvalidException.java
  16. 81
      src/main/java/me/lensfrex/littlebusters/api/v1/login/Login.java
  17. 24
      src/main/java/me/lensfrex/littlebusters/api/v1/login/LoginIdentify.java
  18. 13
      src/main/java/me/lensfrex/littlebusters/api/v1/pojos/StoredKey.java
  19. 26
      src/main/java/me/lensfrex/littlebusters/api/v1/pojos/UserInformation.java
  20. 2
      src/main/java/me/lensfrex/littlebusters/api/v1/pojos/UserResult.java
  21. 50
      src/main/java/me/lensfrex/littlebusters/api/v1/utils/IOUtil.java
  22. 13
      src/main/java/me/lensfrex/littlebusters/api/v1/utils/Time.java
  23. 4
      src/main/java/me/lensfrex/littlebusters/api/v1/utils/database/MyBatisUtil.java
  24. 14
      src/main/java/me/lensfrex/littlebusters/api/v1/utils/database/User.java
  25. 55
      src/main/java/me/lensfrex/littlebusters/api/v1/utils/jwt/JWTManager.java
  26. 4
      src/main/java/me/lensfrex/littlebusters/api/v1/utils/key/KeyManager.java
  27. 50
      src/main/resources/mappers/DatabaseInitSqlMapper.xml
  28. 19
      src/main/resources/mappers/KeySqlMapper.xml
  29. 4
      src/main/resources/mappers/LoginSqlMapper.xml
  30. 2
      src/main/resources/mappers/RegisterSqlMapper.xml
  31. 8
      src/main/resources/me/lensfrex/littlebusters/DefaultConfig.conf

@ -24,18 +24,7 @@
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
@ -98,6 +87,11 @@
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>

@ -1,18 +1,17 @@
package me.lensfrex.littlebusters.api.v1;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import me.lensfrex.littlebusters.api.v1.dao.UserInformation;
import me.lensfrex.littlebusters.api.v1.dao.UserRegister;
import me.lensfrex.littlebusters.api.v1.responses.RegisterResponseData;
import me.lensfrex.littlebusters.api.v1.responses.general.ErrorResponse;
import me.lensfrex.littlebusters.api.v1.responses.general.ResponseBase;
import me.lensfrex.littlebusters.api.v1.beans.requests.RegisterRequestBody;
import me.lensfrex.littlebusters.api.v1.dao.UserOperators;
import me.lensfrex.littlebusters.api.v1.beans.responses.RegisterResponseData;
import me.lensfrex.littlebusters.api.v1.beans.responses.general.ErrorResponse;
import me.lensfrex.littlebusters.api.v1.beans.responses.general.ResponseBase;
import me.lensfrex.littlebusters.api.v1.utils.InputChecker;
import me.lensfrex.littlebusters.api.v1.utils.MyBatisUtil;
import me.lensfrex.littlebusters.api.v1.utils.database.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.mindrot.jbcrypt.BCrypt;
@ -29,8 +28,8 @@ public class Register {
RegisterRequestBody registerRequestBody;
try {
registerRequestBody = gson.fromJson(request, RegisterRequestBody.class);
if (InputChecker.hasInvalidChar(registerRequestBody.userName) ||
InputChecker.hasInvisibleChar(registerRequestBody.password)) {
if (InputChecker.hasInvalidChar(registerRequestBody.getUserName()) ||
InputChecker.hasInvisibleChar(registerRequestBody.getPassword())) {
ErrorResponse errorResponse = new ErrorResponse(1, "用户名或密码非法");
return gson.toJson(errorResponse);
@ -43,44 +42,16 @@ public class Register {
}
String userUUID = UUID.randomUUID().toString();
String userBcryptPasswd = BCrypt.hashpw(registerRequestBody.password, BCrypt.gensalt());
String userBcryptPasswd = BCrypt.hashpw(registerRequestBody.getPassword(), BCrypt.gensalt());
SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
UserRegister userRegister = sqlSession.getMapper(UserRegister.class);
UserOperators userOperators = sqlSession.getMapper(UserOperators.class);
userRegister.addRegisterInfoIntoDb(userUUID, registerRequestBody.userName, userBcryptPasswd);
userOperators.addRegisterInfoIntoDb(userUUID, registerRequestBody.getUserName(), userBcryptPasswd);
RegisterResponseData registerResponseBody = new RegisterResponseData(10101, userUUID, userBcryptPasswd);
ResponseBase<RegisterResponseData> response = new ResponseBase<>(200, "success", registerResponseBody);
return gson.toJson(response);
}
public static class RegisterRequestBody {
@SerializedName("user_name")
private String userName;
private String password;
public RegisterRequestBody(String userName, String password) {
this.userName = userName;
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
}

@ -0,0 +1,31 @@
package me.lensfrex.littlebusters.api.v1.beans.requests;
import com.google.gson.annotations.SerializedName;
public class LoginRequestBody {
@SerializedName("user_name")
private String userName;
private String password;
public LoginRequestBody(String userName, String password) {
this.userName = userName;
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

@ -0,0 +1,31 @@
package me.lensfrex.littlebusters.api.v1.beans.requests;
import com.google.gson.annotations.SerializedName;
public class RegisterRequestBody {
@SerializedName("user_name")
private String userName;
private String password;
public RegisterRequestBody(String userName, String password) {
this.userName = userName;
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

@ -1,4 +1,4 @@
package me.lensfrex.littlebusters.api.v1.responses;
package me.lensfrex.littlebusters.api.v1.beans.responses;
import com.google.gson.annotations.SerializedName;
@ -9,7 +9,9 @@ public class LoginResponseData {
/**
* 登录用户的uid
*/
private int uid;
private long uid;
private String uuid;
/**
* 用户本次登录得到的token
@ -23,20 +25,29 @@ public class LoginResponseData {
@SerializedName("expired_at")
private String expiredAt;
public LoginResponseData(int uid, String accessToken, String expiredAt) {
public LoginResponseData(long uid, String uuid, String accessToken, String expiredAt) {
this.uid = uid;
this.uuid = uuid;
this.accessToken = accessToken;
this.expiredAt = expiredAt;
}
public int getUid() {
public long getUid() {
return uid;
}
public void setUid(int uid) {
public void setUid(long uid) {
this.uid = uid;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getAccessToken() {
return accessToken;
}

@ -1,4 +1,4 @@
package me.lensfrex.littlebusters.api.v1.responses;
package me.lensfrex.littlebusters.api.v1.beans.responses;
import me.lensfrex.littlebusters.api.v1.beans.ProfileItem;

@ -1,4 +1,4 @@
package me.lensfrex.littlebusters.api.v1.responses;
package me.lensfrex.littlebusters.api.v1.beans.responses;
import com.google.gson.annotations.SerializedName;

@ -1,4 +1,4 @@
package me.lensfrex.littlebusters.api.v1.responses.general;
package me.lensfrex.littlebusters.api.v1.beans.responses.general;
/**
* 错误响应类
@ -11,6 +11,6 @@ public class ErrorResponse extends ResponseBase<ErrorData> {
class ErrorData {}
class ErrorCode {
class ErrorCodes {
public static final int REQUEST_PARAM_INVALID = 100;
}

@ -1,4 +1,4 @@
package me.lensfrex.littlebusters.api.v1.responses.general;
package me.lensfrex.littlebusters.api.v1.beans.responses.general;
import com.google.gson.annotations.SerializedName;

@ -0,0 +1,57 @@
package me.lensfrex.littlebusters.api.v1.config;
import me.lensfrex.littlebusters.api.v1.exceptions.ConfigureFileInvalidException;
import me.lensfrex.littlebusters.api.v1.utils.IOUtil;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class ConfigLoader {
private final String globalConfigFilePath;
public ConfigLoader(String globalConfigFilePath) {
this.globalConfigFilePath = globalConfigFilePath;
}
public static String toSpecificPathForm(String uniformFilepath) {
return uniformFilepath.replace("/", File.separator);
}
// todo: 脑子有点乱了,等以后再去重写一遍吧
public void load() throws ConfigureFileInvalidException {
try {
File globalConfigFile = new File(globalConfigFilePath);
if (!globalConfigFile.exists()) {
globalConfigFile.getParentFile().mkdirs();
globalConfigFile.createNewFile();
InputStream defaultConfigInoutStream = getClass().getResourceAsStream("me/lensfrex/littlebusters/DefaultConfig.conf");
if (defaultConfigInoutStream == null) {
throw new NullPointerException("Can't find the default config file in program.");
}
GlobalConfig.configProperties.load(defaultConfigInoutStream);
String defaultConfig = IOUtil.inputStreamToString(defaultConfigInoutStream, StandardCharsets.UTF_8);
IOUtil.writeFile(defaultConfig.getBytes(StandardCharsets.UTF_8), globalConfigFile);
return;
}
FileInputStream configFileInputStream = new FileInputStream(globalConfigFile);
GlobalConfig.configProperties.load(configFileInputStream);
} catch (FileNotFoundException e) {
// todo: 这里应该换成使用日志输出
System.err.printf("Error: Config file: %s doesn't exists.%n", globalConfigFilePath);
e.printStackTrace();
} catch (Exception e) {
// todo: 这里应该换成使用日志输出
System.err.printf("Error: Some errors happened while reading config file: %s %n", globalConfigFilePath);
System.err.printf("Error: %s%n", e.getMessage());
e.printStackTrace();
}
}
}

@ -0,0 +1,12 @@
package me.lensfrex.littlebusters.api.v1.config;
import java.util.Properties;
public class GlobalConfig {
protected static final Properties configProperties = new Properties();
private GlobalConfig() {}
public static String get(String key) {
return configProperties.getProperty(key);
}
}

@ -0,0 +1,9 @@
package me.lensfrex.littlebusters.api.v1.dao;
import me.lensfrex.littlebusters.api.v1.pojos.StoredKey;
public interface KeyDatabase {
StoredKey getKeyContentByKeyName(String keyName);
int addKeyIntoDatabase(String keyContent, String keyName, int keyType);
}

@ -1,10 +0,0 @@
package me.lensfrex.littlebusters.api.v1.dao;
import org.apache.ibatis.annotations.Param;
import java.util.Map;
public interface UserInformation {
Map<String, Object> getBasicInfoByUserName(@Param("userName") String userName);
}

@ -1,9 +1,12 @@
package me.lensfrex.littlebusters.api.v1.dao;
import me.lensfrex.littlebusters.api.v1.pojos.UserInformation;
import org.apache.ibatis.annotations.Param;
public interface UserRegister {
public interface UserOperators {
int addRegisterInfoIntoDb(@Param("uuid") String UUID,
@Param("userName") String userName,
@Param("password") String password);
}
UserInformation getBasicInfoByUserName(@Param("userName") String userName);
}

@ -0,0 +1,11 @@
package me.lensfrex.littlebusters.api.v1.exceptions;
public class ConfigureFileInvalidException extends Exception {
public ConfigureFileInvalidException() {
super("Error: Some errors happened while parsing configure file.");
}
public ConfigureFileInvalidException(String message) {
super(message);
}
}

@ -2,29 +2,28 @@ package me.lensfrex.littlebusters.api.v1.login;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.annotations.SerializedName;
import com.sun.org.apache.xalan.internal.xsltc.trax.XSLTCSource;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import me.lensfrex.littlebusters.api.v1.dao.UserInformation;
import me.lensfrex.littlebusters.api.v1.responses.LoginResponseData;
import me.lensfrex.littlebusters.api.v1.responses.general.ErrorResponse;
import me.lensfrex.littlebusters.api.v1.responses.general.ResponseBase;
import me.lensfrex.littlebusters.api.v1.beans.requests.LoginRequestBody;
import me.lensfrex.littlebusters.api.v1.beans.responses.LoginResponseData;
import me.lensfrex.littlebusters.api.v1.beans.responses.general.ErrorResponse;
import me.lensfrex.littlebusters.api.v1.beans.responses.general.ResponseBase;
import me.lensfrex.littlebusters.api.v1.pojos.UserInformation;
import me.lensfrex.littlebusters.api.v1.utils.InputChecker;
import me.lensfrex.littlebusters.api.v1.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import me.lensfrex.littlebusters.api.v1.utils.database.User;
import me.lensfrex.littlebusters.api.v1.utils.jwt.JWTManager;
import org.mindrot.jbcrypt.BCrypt;
import java.math.BigInteger;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.time.LocalDateTime;
@Path("/login")
public class Login {
private final static int TOKEN_EXPIRE_DAY = 15;
private final static Gson gson = new Gson();
private final static JWTManager jwtManager = new JWTManager();
User userTools = new User();
@POST
@Produces(MediaType.APPLICATION_JSON)
@ -32,15 +31,35 @@ public class Login {
LoginRequestBody loginRequestBody;
try {
loginRequestBody = gson.fromJson(request, LoginRequestBody.class);
if (InputChecker.hasInvalidChar(loginRequestBody.userName) ||
InputChecker.hasInvisibleChar(loginRequestBody.password)) {
if (InputChecker.hasInvalidChar(loginRequestBody.getUserName()) ||
InputChecker.hasInvisibleChar(loginRequestBody.getPassword())) {
ErrorResponse errorResponse = new ErrorResponse(100, "请求的用户名或密码非法");
return gson.toJson(errorResponse);
}
if ()
LoginResponseData loginResponseData = new LoginResponseData(((BigInteger) selectResult.get("uid")).intValue(), (String) selectResult.get("password"), LocalDate.now().toString());
UserInformation userDatabaseInformation = userTools.getUser(loginRequestBody.getUserName());
// todo 这些情况用异常处理会好点吧 分散在代码里面很难看
// 过会改改
if (userDatabaseInformation == null) {
ErrorResponse errorResponse = new ErrorResponse(400, "用户名或密码错误");
return gson.toJson(errorResponse);
}
if (!identifyPassword(loginRequestBody.getPassword(), userDatabaseInformation.password)) {
ErrorResponse errorResponse = new ErrorResponse(400, "用户名或密码错误");
return gson.toJson(errorResponse);
}
String userToken = jwtManager.createNewJWT("login", loginRequestBody.getUserName(), TOKEN_EXPIRE_DAY);
LoginResponseData loginResponseData = new LoginResponseData(
userDatabaseInformation.uid,
userDatabaseInformation.uuid,
userToken,
LocalDateTime.now().plusDays(TOKEN_EXPIRE_DAY).toString());
ResponseBase<LoginResponseData> response = new ResponseBase<>(200, "success", loginResponseData);
return gson.toJson(response);
@ -54,36 +73,12 @@ public class Login {
System.err.println(request);
System.err.println(e.getMessage());
ErrorResponse errorResponse = new ErrorResponse(400, "服务器内部错误,请联系那个背锅的家伙");
ErrorResponse errorResponse = new ErrorResponse(400, "服务器内部错误,请联系那个背锅的家伙");
return gson.toJson(errorResponse);
}
}
public static class LoginRequestBody {
@SerializedName("user_name")
private String userName;
private String password;
public LoginRequestBody(String userName, String password) {
this.userName = userName;
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean identifyPassword(String originPassword, String bcryptPassword) {
return BCrypt.checkpw(originPassword, bcryptPassword);
}
}

@ -1,24 +0,0 @@
package me.lensfrex.littlebusters.api.v1.login;
import me.lensfrex.littlebusters.api.v1.dao.UserInformation;
import me.lensfrex.littlebusters.api.v1.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.mindrot.jbcrypt.BCrypt;
import java.util.Map;
public class LoginIdentify {
public boolean identifyLoginUserName(String userName, String passwordSHA256) {
SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
UserInformation mapper = sqlSession.getMapper(UserInformation.class);
Map<String, Object> selectResult = mapper.getBasicInfoByUserName(userName);
if (selectResult == null) {
return false;
}
return BCrypt.checkpw(passwordSHA256, (String) selectResult.get("password"));
}
}

@ -0,0 +1,13 @@
package me.lensfrex.littlebusters.api.v1.pojos;
public class StoredKey {
public String keyContent;
public String keyName;
public int keyType;
public StoredKey(String keyContent, String keyName, int keyType) {
this.keyContent = keyContent;
this.keyName = keyName;
this.keyType = keyType;
}
}

@ -0,0 +1,26 @@
package me.lensfrex.littlebusters.api.v1.pojos;
public class UserInformation {
public long uid;
public String uuid;
public String userName;
public String password;
public int accountStatus;
public int accountType;
public String phoneNumber;
public boolean deleted;
public UserInformation(long uid, String uuid, String userName, String password, int accountStatus,
int accountType, String phoneNumber, boolean deleted) {
this.uid = uid;
this.uuid = uuid;
this.userName = userName;
this.password = password;
this.accountStatus = accountStatus;
this.accountType = accountType;
this.phoneNumber = phoneNumber;
this.deleted = deleted;
}
public UserInformation() {}
}

@ -14,4 +14,4 @@ public class UserResult {
this.deleted = deleted;
this.accountStatus = accountStatus;
}
}
}

@ -0,0 +1,50 @@
package me.lensfrex.littlebusters.api.v1.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
public class IOUtil {
public static String inputStreamToString(InputStream inputStream, Charset charSet) {
byte[] bytes = readDataFromInputStream(inputStream);
if (bytes != null) {
return new String(bytes, charSet);
} else {
return null;
}
}
public static byte[] readDataFromInputStream(InputStream inputStream) {
return readDataFromInputStream(inputStream, 5);// read every 5kb in default
}
public static byte[] readDataFromInputStream(InputStream inputStream, int byteAllocation) {
try {
ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024 * byteAllocation];
for (int length; (length = inputStream.read(bytes)) != -1; ) {
byteArrayInputStream.write(bytes, 0, length);
}
byteArrayInputStream.flush();
inputStream.close();
byteArrayInputStream.close();
return byteArrayInputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void writeFile(byte[] bytes, File file) throws Exception {
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(bytes);
fileOutputStream.close();
}
}

@ -0,0 +1,13 @@
package me.lensfrex.littlebusters.api.v1.utils;
import java.time.LocalDateTime;
public class Time {
public static LocalDateTime getNowDayTime() {
return LocalDateTime.now();
}
public static LocalDateTime getDateTimeAfterDays(int day) {
return LocalDateTime.now().plusDays(day);
}
}

@ -1,4 +1,4 @@
package me.lensfrex.littlebusters.api.v1.utils;
package me.lensfrex.littlebusters.api.v1.utils.database;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
@ -9,11 +9,11 @@ import java.io.IOException;
import java.io.InputStream;
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
// todo: 读入配置文件来初始化MyBatis,但是应为配置文件加载部分还没好所以就只能先这样了
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

@ -0,0 +1,14 @@
package me.lensfrex.littlebusters.api.v1.utils.database;
import me.lensfrex.littlebusters.api.v1.dao.UserOperators;
import me.lensfrex.littlebusters.api.v1.pojos.UserInformation;
import org.apache.ibatis.session.SqlSession;
public class User {
public UserInformation getUser(String userName) {
SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
UserOperators mapper = sqlSession.getMapper(UserOperators.class);
return mapper.getBasicInfoByUserName(userName);
}
}

@ -0,0 +1,55 @@
package me.lensfrex.littlebusters.api.v1.utils.jwt;
import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;
import me.lensfrex.littlebusters.api.v1.utils.Time;
import java.security.Key;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
public class JWTManager {
// todo 时间有限先这样写能跑起来吧
private final static Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
public String createNewJWT(String subject, String user, int invalidDays) {
Map<String, Object> header = new HashMap<>();
header.put("alg", "HS256");
header.put("typ", "JWT");
LocalDateTime now = Time.getNowDayTime();
Map<String, Object> payload = new HashMap<>();
payload.put("user", user);
payload.put("start", now.toString());
payload.put("exp", now.plusDays(invalidDays).toString());
payload.put("api_ver", "1");
// payload.put("iss", machineId);
return Jwts.builder()
.setHeader(header)
.setClaims(payload)
.signWith(key)
.compact();
// 这里生成token之后还是放在redis里边缓存一下吧
// 但是考虑到各种因素就先不实现吧(懒)
}
public boolean verifyToken(String token) {
try {
Jws<Claims> jws = Jwts.parserBuilder()
.setSigningKey(key)
.build()
.parseClaimsJws(token);
System.out.println(jws);
} catch (JwtException e) {
System.err.println("Error: token is wrong: " + e.getMessage());
return false;
}
return true;
}
}

@ -0,0 +1,4 @@
package me.lensfrex.littlebusters.api.v1.utils.key;
public class KeyManager {
}

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="me.lensfrex.littlebusters.api.v1.dao.KeyDatabase">
<sql id="createDatabaseTables">
CREATE TABLE `account_basic` (
`uid` int(18) unsigned unique NOT NULL AUTO_INCREMENT COMMENT '用户id,1起,给人看的',
`uuid` varchar(36) unique NOT NULL COMMENT '用户uuid,给某些api用的,目前并没有这些api',
`user_name` varchar(32) unique NOT NULL COMMENT '用户名,可用于登录,最长不超过32位',
`passwd` varchar(60) NOT NULL COMMENT '用户密码,明文密码经前端两次sha256初步加密后再经过后端进行bcrypt加密存储(round 10)\r\nbcrypto长度不应超过72位',
`account_status` tinyint(1) unsigned NOT NULL DEFAULT 1 COMMENT '账户状态,分为 正常(0), 未激活(1), 已封禁(2)',
`account_type` tinyint(1) NOT NULL DEFAULT 0 COMMENT '账号类型',
`phone_number` varchar(11) DEFAULT NULL COMMENT '注册时使用的电话信息,目前暂不使用,可空,默认11位数字',
`deleted` tinyint(1) NOT NULL DEFAULT 0 COMMENT '账号是否注销',
`create_time` datetime NOT NULL DEFAULT current_timestamp() COMMENT '注册日期',
`edit_time` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT '账号信息修改时间',
PRIMARY KEY (`uid`,`uuid`) USING BTREE,
UNIQUE KEY `user_name` (`user_name`),
UNIQUE KEY `uid` (`uid`),
UNIQUE KEY `uuid` (`uuid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `account_info` (
`uid` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户id,1000000起',
`nick_name` varchar(16) NOT NULL COMMENT '用户昵称,长度限制为16个字',
`desc` varchar(255) DEFAULT NULL COMMENT '用户留言信息',
`sign` varchar(16) DEFAULT NULL COMMENT '用户的“个性签名”',
`birthday` date DEFAULT NULL COMMENT '用户生日',
`sex` int(1) DEFAULT NULL COMMENT '用户性别,null:未指定,1:男,2:女,其他值当未指定',
`email` varchar(255) DEFAULT NULL COMMENT '注册时使用的电子邮件,仅用于展示,不用做登录使用,可空',
`create_time` datetime NOT NULL COMMENT '用户信息创建时间',
`edit_time` datetime NOT NULL COMMENT '用户信息修改时间',
PRIMARY KEY (`uid`),
UNIQUE KEY `uid` (`uid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `keys` (
`id` int(11) NOT NULL,
`key` varchar(4028) NOT NULL,
`key_name` varchar(255) NOT NULL,
`create_time` datetime NOT NULL DEFAULT current_timestamp(),
`edit_time` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`key_type` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
</sql>
</mapper>

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="me.lensfrex.littlebusters.api.v1.dao.KeyDatabase">
<select id="getKeyContentByKeyName" resultType="me.lensfrex.littlebusters.api.v1.pojos.StoredKey"
parameterType="java.lang.String">
select key_content as keyContent, key_name as keyName, key_type as keyType
from `LittleBusters`.`keys`
where key_name = #{keyName};
</select>
<insert id="addKeyIntoDatabase" parameterType="me.lensfrex.littlebusters.api.v1.pojos.StoredKey">
insert into `LittleBusters`.`keys` (`key_content`, `key_name`, `key_type`)
values (#{keyContent}, #{keyName}, #{keyType});
</insert>
</mapper>

@ -4,8 +4,8 @@
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="me.lensfrex.littlebusters.api.v1.dao.UserInformation">
<select id="getBasicInfoByUserName" resultType="Map" parameterType="java.lang.String">
<mapper namespace="me.lensfrex.littlebusters.api.v1.dao.UserOperators">
<select id="getBasicInfoByUserName" resultType="me.lensfrex.littlebusters.api.v1.pojos.UserInformation" parameterType="java.lang.String">
select uid, uuid, passwd as password, deleted, account_status as accountStstus
from `account_basic`
where user_name = #{userName}

@ -4,7 +4,7 @@
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="me.lensfrex.littlebusters.api.v1.dao.UserRegister">
<mapper namespace="me.lensfrex.littlebusters.api.v1.dao.UserOperators">
<insert id="addRegisterInfoIntoDb">
insert into `LittleBusters`.`account_basic` (`uuid`, `user_name`, `passwd`)
values (#{uuid}, #{userName}, #{password})

@ -0,0 +1,8 @@
# 数据库的类型,目前仅支持mysql和mariadb两种sql服务器
database.dbType = "mariadb"
database.url = "jdbc:mariadb://localhost:3386/LittleBusters?characterEncoding=utf-8"
database.userName = "root"
database.password = "321654mid"
JWT.signAlgo = "HS256"
JWT.secretKey = "asdifusaydiuasdczxcvmnbvamhdfgasjdhfuweygfiwqueygf"
Loading…
Cancel
Save