parent
1ce37772d0
commit
5937135d8e
@ -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 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; |
||||
|
||||
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); |
||||
} |
||||
} |
@ -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() {} |
||||
} |
@ -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); |
||||
} |
||||
} |
@ -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> |
@ -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…
Reference in new issue