一些修改

1. 将register部分拆分
2. 更改了jwt的结构
3. 修改pom.xml文件以能够直接打包jar运行(war包暂时不处理)
4. 修改api文档描述
5. 修改前端的接口调用(未来前端可能会换用vue重写,但不是目前并不是首要)
spring-dev
lensfrex 3 years ago
parent 207a8caf21
commit a9fd0c1dfb
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 20
      API_doc.md
  2. 40
      front-end/js/events.js
  3. 18
      front-end/js/welcomepage_message.js
  4. 21
      pom.xml
  5. 70
      src/main/java/me/lensfrex/trailblazer/api/v1/beans/TokenBody.java
  6. 3
      src/main/java/me/lensfrex/trailblazer/api/v1/config/ConfigLoader.java
  7. 5
      src/main/java/me/lensfrex/trailblazer/api/v1/controllers/auth/login/Login.java
  8. 39
      src/main/java/me/lensfrex/trailblazer/api/v1/controllers/auth/register/Register.java
  9. 11
      src/main/java/me/lensfrex/trailblazer/api/v1/exceptions/user/UserNameAlreadyExistsException.java
  10. 7
      src/main/java/me/lensfrex/trailblazer/api/v1/service/auth/login/LoginService.java
  11. 4
      src/main/java/me/lensfrex/trailblazer/api/v1/service/auth/register/Register.java
  12. 39
      src/main/java/me/lensfrex/trailblazer/api/v1/service/auth/register/RegisterService.java
  13. 8
      src/main/java/me/lensfrex/trailblazer/api/v1/utils/jwt/JWTManager.java
  14. 2
      src/main/resources/application.yml
  15. 18
      src/main/webapp/WEB-INF/web.xml

@ -2,7 +2,13 @@
---
##### 本文档中,除非特殊说明,URL测试地址均为 https://lb.ciduid.top ,使用POST方法的API参数均为Body部分的参数,以json格式发送
##### 本文档中,除非特殊说明,URL地址前缀均为 http://[server_address]:[port]/ (本地运行),如果使用 lb.ciduid.top 提供的api服务,则应该为 https://lb.ciduid.top/api/v1/
##### 例如:登录接口`https://localhost:64888/user/login`(本地运行) `https://lb.ciduid.top/api/v1/user/login` (使用lb.ciduid.top的api)
##### POST方法的API参数均为Body部分的参数,以json格式发送
---
## 用户相关
@ -10,7 +16,7 @@
- 功能:登录一个已经存在的用户,获取会话token
- URL: `/api/v1/login`
- URL: `/user/login`
- 方法:`POST`
@ -46,7 +52,7 @@
- 功能:注册创建一个账号
- URL:`/api/v1/register`
- URL:`/user/register`
- 方法:`POST`
@ -80,11 +86,11 @@
### 3. 修改用户基本信息
// 这里的接口还没想好怎么设计
// 这里的接口还没想好怎么设计,所以没写完
- 功能:修改用户的基本信息,如密码、登录名等
- URL:`/api/v1/modifyUserBasicInfo?uid={uid}`
- URL:`/user/modifyUserBasicInfo?uid={uid}`
- 方法:`post`
@ -110,7 +116,7 @@
- 功能:修改用户的基本信息,如密码、登录名等
- URL:`/api/v1/modifyUserInfo?uid={uid}`
- URL:`/user/modifyUserInfo?uid={uid}`
- 方法:`post`
@ -134,7 +140,7 @@
- 功能:修改用户的基本信息,如密码、登录名等
- URL:`/api/v1/profile?uid={uid}`
- URL:`/user/profile?uid={uid}`
- 方法:`get`

@ -20,9 +20,10 @@ function login() {
function sendLogin(postData) {
$.ajax({
url: "/api/v1/login",
url: "/api/v1/user/login",
type: "post",
dataType: "json",
contentType: "application/json",
data: JSON.stringify(postData),
success: (result) => {
console.log("获取到数据:" + JSON.stringify(result));
@ -31,6 +32,7 @@ function sendLogin(postData) {
case 1:
showMessage("好啦");
storeToken(result.data.access_token);
showDialog("LoginTest", "获得数据:" + JSON.stringify(result.data));
break;
case 2:
showMessage("请求的数据不对哦");
@ -68,9 +70,10 @@ function register() {
};
$.ajax({
url: "/api/v1/register",
url: "/api/v1/user/register",
type: "post",
dataType: "json",
contentType: "application/json",
data: JSON.stringify(postData),
success: (result) => {
console.log("获取到数据:" + JSON.stringify(result));
@ -79,6 +82,7 @@ function register() {
case 1:
showMessage("好啦");
storeToken(result.data.access_token);
showDialog("RegisterTest", "获得数据:" + JSON.stringify(result.data));
break;
case 2:
showMessage("请求的数据不对哦");
@ -102,4 +106,36 @@ function register() {
function jumpToFillInformation() {
}
function checkInputDataCorrect(username, password) {
if (username == "" || passwd == "") {
showMessage("用户名和密码都不可以是空的哦");
return false;
}
if (!isLetter(username)) {
showMessage("用户名只能是大小写字母和数字哦");
return false;
}
if (!isNormalCharacter(passwd)) {
showMessage("不知道你往密码框都搞了点什么东西...");
return false;
}
if (username.length > 32) {
showMessage("用户名太长啦(应该小于32个字符)");
return;
}
if (password.length < 8) {
showMessage("密码长度太短啦(大于等于8小于等于32)");
return;
} else if (password > 32){
showMessage("密码长度太长啦(大于等于8小于等于32)");
return;
}
return true;
}

@ -6,24 +6,6 @@ function closeDialog() {
$("#dialogMask").fadeOut(400);
}
function checkInputDataCorrect(username, password) {
if (username == "" || passwd == "") {
showMessage("用户名和密码都不可以是空的哦");
return false;
}
if (!isLetter(username)) {
showMessage("用户名只能是大小写字母和数字哦");
return false;
}
if (!isNormalCharacter(passwd)) {
showMessage("不知道你往密码框都搞了点什么东西...");
return false;
}
return true;
}
function showMessage(message) {
$("#errMsgBox")[0].innerHTML = `<p>${message}</p>`;
}

@ -8,8 +8,7 @@
<artifactId>Trailblazer</artifactId>
<version>0.0.1-dev</version>
<name>Trailblazer</name>
<packaging>war</packaging>
<packaging>jar</packaging>
<properties>
<start-class>me.lensfrex.trailblazer.api.v1.ServerMain</start-class>
@ -17,6 +16,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<junit.version>5.8.1</junit.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
@ -88,21 +88,30 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

@ -0,0 +1,70 @@
package me.lensfrex.trailblazer.api.v1.beans;
import com.google.gson.annotations.SerializedName;
import java.util.Date;
public class TokenBody {
@SerializedName("api_ver")
private String apiVersion;
@SerializedName("user")
private String username;
@SerializedName("uid")
private long uid;
@SerializedName("uuid")
private String UUID;
@SerializedName("exp")
private Date invalidDate;
public TokenBody(String apiVersion, String username, long uid, String UUID, Date invalidDate) {
this.apiVersion = apiVersion;
this.username = username;
this.uid = uid;
this.UUID = UUID;
this.invalidDate = invalidDate;
}
public String getApiVersion() {
return apiVersion;
}
public void setApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public long getUid() {
return uid;
}
public void setUid(long uid) {
this.uid = uid;
}
public String getUUID() {
return UUID;
}
public void setUUID(String UUID) {
this.UUID = UUID;
}
public Date getInvalidDate() {
return invalidDate;
}
public void setInvalidDate(Date invalidDate) {
this.invalidDate = invalidDate;
}
}

@ -7,9 +7,6 @@ import java.io.*;
import java.nio.charset.StandardCharsets;
public class ConfigLoader {
private final String globalConfigFilePath;
public ConfigLoader(String globalConfigFilePath) {

@ -9,7 +9,6 @@ import me.lensfrex.trailblazer.api.v1.exceptions.RequestDataInvalidException;
import me.lensfrex.trailblazer.api.v1.exceptions.user.LoginInfoWrongException;
import me.lensfrex.trailblazer.api.v1.service.auth.login.LoginService;
import me.lensfrex.trailblazer.api.v1.utils.InputChecker;
import me.lensfrex.trailblazer.api.v1.utils.jwt.JWTManager;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.web.bind.annotation.*;
@ -19,7 +18,6 @@ import javax.annotation.Resource;
@RequestMapping("/user")
public class Login {
private static final Gson gson = new Gson();
private static final JWTManager jwtManager = JWTManager.getInstance();
@Resource
private LoginService loginService;
@ -34,12 +32,13 @@ public class Login {
throw new RequestDataInvalidException();
}
return gson.toJson(loginService.getLoginResponseBody(loginRequestBody));
return gson.toJson(loginService.checkLogin(loginRequestBody));
} catch (JsonParseException | RequestDataInvalidException e) {
return gson.toJson(ResponseBase.error(ResponseCode.REQUEST_FORMAT_INVALID, "请求的数据格式不对"));
} catch (LoginInfoWrongException e) {
return gson.toJson(ResponseBase.error(ResponseCode.PASSWORD_WRONG, "用户名或密码错误"));
} catch (Exception e) {
System.err.println(e.getMessage());
return gson.toJson(ResponseBase.error(ResponseCode.SERVER_ERROR, "服务器内部错误,请联系那个背锅的家伙"));
}
}

@ -4,35 +4,30 @@ import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import me.lensfrex.trailblazer.api.v1.beans.requests.RegisterRequestBody;
import me.lensfrex.trailblazer.api.v1.beans.responses.general.ResponseCode;
import me.lensfrex.trailblazer.api.v1.beans.responses.RegisterResponseData;
import me.lensfrex.trailblazer.api.v1.beans.responses.general.ResponseBase;
import me.lensfrex.trailblazer.api.v1.dao.UserDao;
import me.lensfrex.trailblazer.api.v1.exceptions.RequestDataInvalidException;
import me.lensfrex.trailblazer.api.v1.exceptions.user.UserNameAlreadyExistsException;
import me.lensfrex.trailblazer.api.v1.service.auth.register.RegisterService;
import me.lensfrex.trailblazer.api.v1.utils.InputChecker;
import me.lensfrex.trailblazer.api.v1.utils.jwt.JWTManager;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.UUID;
import javax.annotation.Resource;
@RestController
@RequestMapping("/user")
public class Register {
private static final JWTManager jwtManager = JWTManager.getInstance();
@Resource
private RegisterService registerService;
private static final Gson gson = new Gson();
@PostMapping(value = "/register", produces = "application/json")
public String register(@RequestBody String request) {
RegisterRequestBody registerRequestBody;
try {
registerRequestBody = gson.fromJson(request, RegisterRequestBody.class);
RegisterRequestBody registerRequestBody = gson.fromJson(request, RegisterRequestBody.class);
if (registerRequestBody == null ||
InputChecker.hasInvalidChar(registerRequestBody.getUserName()) ||
@ -41,25 +36,11 @@ public class Register {
throw new RequestDataInvalidException();
}
if (UserDao.isUserAlreadyExist(registerRequestBody.getUserName())) {
return gson.toJson(ResponseBase.error(ResponseCode.USER_ALREADY_EXISTS, "申请注册的用户已经存在"));
}
String userUUID = UUID.randomUUID().toString();
String userBcryptPasswd = BCrypt.hashpw(registerRequestBody.getPassword(), BCrypt.gensalt());
int newUid = UserDao.addUser(userUUID, registerRequestBody.getUserName(), userBcryptPasswd);
Date expireDate = Date.from(Instant.now().plus(JWTManager.TOKEN_DEFAULT_EXPIRE_DAY, ChronoUnit.DAYS));
RegisterResponseData registerResponseBody = new RegisterResponseData(
newUid,
userUUID,
jwtManager.createNewJWT(registerRequestBody.getUserName(), expireDate),
expireDate.getTime());
return gson.toJson(ResponseBase.success(registerResponseBody));
return gson.toJson(ResponseBase.success(registerService.register(registerRequestBody)));
} catch (JsonParseException | RequestDataInvalidException e) {
return gson.toJson(ResponseBase.error(ResponseCode.REQUEST_FORMAT_INVALID, "请求的数据不正确"));
} catch (UserNameAlreadyExistsException e) {
return gson.toJson(ResponseBase.error(ResponseCode.USER_ALREADY_EXISTS, "用户名已经被使用"));
} catch (Exception e) {
return gson.toJson(ResponseBase.error(ResponseCode.SERVER_ERROR, "服务器程序发生错误,有个家伙又写bug了。Error:" + e.getMessage()));
}

@ -0,0 +1,11 @@
package me.lensfrex.trailblazer.api.v1.exceptions.user;
public class UserNameAlreadyExistsException extends Exception {
public UserNameAlreadyExistsException(String message) {
super(message);
}
public UserNameAlreadyExistsException() {
super("用户名已经被使用");
}
}

@ -18,7 +18,7 @@ import java.util.Date;
public class LoginService {
private static final JWTManager jwtManager = JWTManager.getInstance();
public ResponseBase<LoginResponseData> getLoginResponseBody(LoginRequestBody loginRequestBody) throws LoginInfoWrongException {
public ResponseBase<LoginResponseData> checkLogin(LoginRequestBody loginRequestBody) throws LoginInfoWrongException {
UserInformation userDatabaseInformation = UserDao.getUser(loginRequestBody.getUserName());
if (userDatabaseInformation == null) {
@ -30,7 +30,10 @@ public class LoginService {
}
Date expireDate = Date.from(Instant.now().plus(JWTManager.TOKEN_DEFAULT_EXPIRE_DAY, ChronoUnit.DAYS));
String userToken = jwtManager.createNewJWT(loginRequestBody.getUserName(), expireDate);
String userToken = jwtManager.createNewJWT(loginRequestBody.getUserName(),
userDatabaseInformation.uuid,
userDatabaseInformation.uid,
expireDate);
LoginResponseData loginResponseData = new LoginResponseData(
userDatabaseInformation.uid,

@ -1,4 +0,0 @@
package me.lensfrex.trailblazer.api.v1.service.auth.register;
public class Register {
}

@ -0,0 +1,39 @@
package me.lensfrex.trailblazer.api.v1.service.auth.register;
import me.lensfrex.trailblazer.api.v1.beans.requests.RegisterRequestBody;
import me.lensfrex.trailblazer.api.v1.beans.responses.RegisterResponseData;
import me.lensfrex.trailblazer.api.v1.dao.UserDao;
import me.lensfrex.trailblazer.api.v1.exceptions.user.UserNameAlreadyExistsException;
import me.lensfrex.trailblazer.api.v1.utils.jwt.JWTManager;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.stereotype.Service;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.UUID;
@Service
public class RegisterService {
private static final JWTManager jwtManager = JWTManager.getInstance();
public RegisterResponseData register(RegisterRequestBody registerRequestBody) throws UserNameAlreadyExistsException {
if (UserDao.isUserAlreadyExist(registerRequestBody.getUserName())) {
throw new UserNameAlreadyExistsException("用户名已经被使用");
}
String userUUID = UUID.randomUUID().toString();
String userBcryptPasswd = BCrypt.hashpw(registerRequestBody.getPassword(), BCrypt.gensalt());
int newUid = UserDao.addUser(userUUID, registerRequestBody.getUserName(), userBcryptPasswd);
Date expireDate = Date.from(Instant.now().plus(JWTManager.TOKEN_DEFAULT_EXPIRE_DAY, ChronoUnit.DAYS));
return new RegisterResponseData(
newUid,
userUUID,
jwtManager.createNewJWT(registerRequestBody.getUserName(), userUUID, newUid, expireDate),
expireDate.getTime());
}
}

@ -23,15 +23,17 @@ public class JWTManager {
return self;
}
public String createNewJWT(String user, Date invalidDate) {
public String createNewJWT(String user, String uuid, long uid, Date invalidDate) {
Map<String, Object> header = new HashMap<>();
header.put("alg", "HS256");
header.put("typ", "JWT");
Map<String, Object> payload = new HashMap<>();
payload.put("user", user);
payload.put("uuid", uuid);
payload.put("uid", uid);
payload.put("api_ver", "1");
// payload.put("iss", machineId);
// payload.put("blame", machineId);
return Jwts.builder()
.setHeader(header)
@ -46,7 +48,7 @@ public class JWTManager {
public boolean verifyToken(String token) {
try {
Jws<Claims> jws = Jwts.parserBuilder()
Jwts.parserBuilder()
.setSigningKey(key)
.build()
.parseClaimsJws(token);

@ -0,0 +1,2 @@
server:
port: 64888

@ -6,17 +6,17 @@
<servlet>
<servlet-name>TrailblazerApiV1</servlet-name>
<!-- <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>me.lensfrex.trailblazer</param-value>
</init-param>
<!-- <init-param>-->
<!-- <param-name>jersey.config.server.provider.packages</param-name>-->
<!-- <param-value>me.lensfrex.trailblazer</param-value>-->
<!-- </init-param>-->
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<!-- <init-param>-->
<!-- <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>-->
<!-- <param-value>true</param-value>-->
<!-- </init-param>-->
</servlet>
<servlet-mapping>

Loading…
Cancel
Save