parent
2a4228d0e1
commit
207a8caf21
@ -0,0 +1,17 @@ |
||||
package me.lensfrex.trailblazer.api.v1; |
||||
|
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
import org.springframework.boot.builder.SpringApplicationBuilder; |
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; |
||||
|
||||
@SpringBootApplication |
||||
public class ServerMain extends SpringBootServletInitializer { |
||||
@Override |
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { |
||||
return application.sources(ServerMain.class); |
||||
} |
||||
public static void main(String[] args) { |
||||
SpringApplication.run(ServerMain.class, args); |
||||
} |
||||
} |
@ -0,0 +1,18 @@ |
||||
package me.lensfrex.trailblazer.api.v1.controllers; |
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.PostMapping; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
||||
/** |
||||
* 只是方便前端测试自己到底发了什么数据给后端 |
||||
* 返回数据不是很详细,只是把数据体原样给返回了 |
||||
* 但是header信息之类的并没有提供 |
||||
*/ |
||||
@RequestMapping("/test") |
||||
public class FeedBack { |
||||
@PostMapping() |
||||
public String returnPostRequest(String string) { |
||||
return string; |
||||
} |
||||
} |
@ -0,0 +1,15 @@ |
||||
package me.lensfrex.trailblazer.api.v1.controllers; |
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
@RestController |
||||
@RequestMapping("/") |
||||
public class Index { |
||||
@GetMapping() |
||||
public String index() { |
||||
return "Here is the API root of Trailblazer.</br>\n" + |
||||
"To use the API currently, please see the Trailblazer RESTFul API document."; |
||||
} |
||||
} |
@ -0,0 +1,50 @@ |
||||
package me.lensfrex.trailblazer.api.v1.controllers.auth.login; |
||||
|
||||
import com.google.gson.Gson; |
||||
import com.google.gson.JsonParseException; |
||||
import me.lensfrex.trailblazer.api.v1.beans.requests.LoginRequestBody; |
||||
import me.lensfrex.trailblazer.api.v1.beans.responses.general.ResponseBase; |
||||
import me.lensfrex.trailblazer.api.v1.beans.responses.general.ResponseCode; |
||||
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.*; |
||||
|
||||
import javax.annotation.Resource; |
||||
|
||||
@RestController |
||||
@RequestMapping("/user") |
||||
public class Login { |
||||
private static final Gson gson = new Gson(); |
||||
private static final JWTManager jwtManager = JWTManager.getInstance(); |
||||
|
||||
@Resource |
||||
private LoginService loginService; |
||||
|
||||
@PostMapping(value = "/login", produces = "application/json") |
||||
public String login(@RequestBody String request) { |
||||
LoginRequestBody loginRequestBody; |
||||
try { |
||||
loginRequestBody = gson.fromJson(request, LoginRequestBody.class); |
||||
if (InputChecker.hasInvalidChar(loginRequestBody.getUserName()) || |
||||
InputChecker.hasInvisibleChar(loginRequestBody.getPassword())) { |
||||
throw new RequestDataInvalidException(); |
||||
} |
||||
|
||||
return gson.toJson(loginService.getLoginResponseBody(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) { |
||||
return gson.toJson(ResponseBase.error(ResponseCode.SERVER_ERROR, "服务器内部错误,请联系那个背锅的家伙")); |
||||
} |
||||
} |
||||
|
||||
public boolean identifyPassword(String originPassword, String bcryptPassword) { |
||||
return BCrypt.checkpw(originPassword, bcryptPassword); |
||||
} |
||||
} |
@ -0,0 +1,16 @@ |
||||
package me.lensfrex.trailblazer.api.v1.controllers.auth.token; |
||||
|
||||
|
||||
import me.lensfrex.trailblazer.api.v1.utils.jwt.JWTManager; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
@RestController |
||||
@RequestMapping("/token") |
||||
public class TokenChecker { |
||||
public static final JWTManager jwtManager = JWTManager.getInstance(); |
||||
|
||||
@PostMapping(value = "/check", produces = "application/json") |
||||
public String checkTokenAvailable(@RequestBody String request) { |
||||
return String.valueOf(jwtManager.verifyToken(request)); |
||||
} |
||||
} |
@ -0,0 +1,16 @@ |
||||
package me.lensfrex.trailblazer.api.v1.controllers.auth.token; |
||||
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestBody; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RequestMethod; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
@RestController |
||||
@RequestMapping("/token") |
||||
public class TokenRefresher { |
||||
@RequestMapping(value = "/refresh", method = RequestMethod.POST, produces = "application/json") |
||||
public String refreshToken(@RequestBody String request) { |
||||
return request; |
||||
} |
||||
} |
@ -0,0 +1,15 @@ |
||||
package me.lensfrex.trailblazer.api.v1.controllers.profile; |
||||
|
||||
import org.springframework.web.bind.annotation.RequestBody; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
||||
@RequestMapping("/profile") |
||||
public class Profile { |
||||
|
||||
// @Path("/getItems/{uid}")
|
||||
// @Produces(MediaType.APPLICATION_JSON)
|
||||
public String getProfiles(@RequestBody String uid) { |
||||
return "unfinished feature..."; |
||||
} |
||||
|
||||
} |
@ -1,20 +0,0 @@ |
||||
package me.lensfrex.trailblazer.api.v1.service; |
||||
|
||||
import jakarta.ws.rs.POST; |
||||
import jakarta.ws.rs.Path; |
||||
import jakarta.ws.rs.Produces; |
||||
import jakarta.ws.rs.core.MediaType; |
||||
|
||||
/** |
||||
* 只是方便前端测试自己到底发了什么数据给后端 |
||||
* 返回数据不是很详细,只是把数据体原样给返回了 |
||||
* 但是header信息之类的并没有提供 |
||||
*/ |
||||
@Path("/test") |
||||
public class FeedBack { |
||||
@POST |
||||
@Produces(MediaType.TEXT_PLAIN) |
||||
public String returnRequest(String string) { |
||||
return string; |
||||
} |
||||
} |
@ -1,16 +0,0 @@ |
||||
package me.lensfrex.trailblazer.api.v1.service; |
||||
|
||||
import jakarta.ws.rs.GET; |
||||
import jakarta.ws.rs.Path; |
||||
import jakarta.ws.rs.Produces; |
||||
import jakarta.ws.rs.core.MediaType; |
||||
|
||||
@Path("/") |
||||
public class Index { |
||||
@GET |
||||
@Produces(MediaType.TEXT_HTML) |
||||
public String index() { |
||||
return "Here is the API root of Trailblazer.</br>\n" + |
||||
"To use the API currently, please see the Trailblazer RESTFul API document."; |
||||
} |
||||
} |
@ -0,0 +1,48 @@ |
||||
package me.lensfrex.trailblazer.api.v1.service.auth.login; |
||||
|
||||
import me.lensfrex.trailblazer.api.v1.beans.requests.LoginRequestBody; |
||||
import me.lensfrex.trailblazer.api.v1.beans.responses.LoginResponseData; |
||||
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.user.LoginInfoWrongException; |
||||
import me.lensfrex.trailblazer.api.v1.pojos.UserInformation; |
||||
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; |
||||
|
||||
@Service |
||||
public class LoginService { |
||||
private static final JWTManager jwtManager = JWTManager.getInstance(); |
||||
|
||||
public ResponseBase<LoginResponseData> getLoginResponseBody(LoginRequestBody loginRequestBody) throws LoginInfoWrongException { |
||||
UserInformation userDatabaseInformation = UserDao.getUser(loginRequestBody.getUserName()); |
||||
|
||||
if (userDatabaseInformation == null) { |
||||
throw new LoginInfoWrongException(); |
||||
} |
||||
|
||||
if (!identifyPassword(loginRequestBody.getPassword(), userDatabaseInformation.password)) { |
||||
throw new LoginInfoWrongException(); |
||||
} |
||||
|
||||
Date expireDate = Date.from(Instant.now().plus(JWTManager.TOKEN_DEFAULT_EXPIRE_DAY, ChronoUnit.DAYS)); |
||||
String userToken = jwtManager.createNewJWT(loginRequestBody.getUserName(), expireDate); |
||||
|
||||
LoginResponseData loginResponseData = new LoginResponseData( |
||||
userDatabaseInformation.uid, |
||||
userDatabaseInformation.uuid, |
||||
userDatabaseInformation.accountStatus, |
||||
userToken, |
||||
expireDate.getTime()); |
||||
|
||||
return ResponseBase.success(loginResponseData); |
||||
} |
||||
|
||||
private boolean identifyPassword(String originPassword, String bcryptPassword) { |
||||
return BCrypt.checkpw(originPassword, bcryptPassword); |
||||
} |
||||
} |
@ -0,0 +1,4 @@ |
||||
package me.lensfrex.trailblazer.api.v1.service.auth.register; |
||||
|
||||
public class Register { |
||||
} |
@ -1,18 +1,10 @@ |
||||
package me.lensfrex.trailblazer.api.v1.service.token; |
||||
package me.lensfrex.trailblazer.api.v1.service.auth.token; |
||||
|
||||
import jakarta.ws.rs.POST; |
||||
import jakarta.ws.rs.Path; |
||||
import jakarta.ws.rs.Produces; |
||||
import jakarta.ws.rs.core.MediaType; |
||||
import me.lensfrex.trailblazer.api.v1.utils.jwt.JWTManager; |
||||
|
||||
@Path("/token") |
||||
public class TokenChecker { |
||||
public static final JWTManager jwtManager = JWTManager.getInstance(); |
||||
|
||||
@POST |
||||
@Path("/check") |
||||
@Produces(MediaType.APPLICATION_JSON) |
||||
public String checkTokenAvailable(String request) { |
||||
return String.valueOf(jwtManager.verifyToken(request)); |
||||
} |
@ -0,0 +1,7 @@ |
||||
package me.lensfrex.trailblazer.api.v1.service.auth.token; |
||||
|
||||
public class TokenRefresher { |
||||
public String refreshToken(String request) { |
||||
return request; |
||||
} |
||||
} |
@ -1,78 +0,0 @@ |
||||
package me.lensfrex.trailblazer.api.v1.service.login; |
||||
|
||||
import com.google.gson.Gson; |
||||
import com.google.gson.JsonParseException; |
||||
import jakarta.ws.rs.POST; |
||||
import jakarta.ws.rs.Path; |
||||
import jakarta.ws.rs.Produces; |
||||
import jakarta.ws.rs.core.MediaType; |
||||
import me.lensfrex.trailblazer.api.v1.beans.requests.LoginRequestBody; |
||||
import me.lensfrex.trailblazer.api.v1.beans.responses.LoginResponseData; |
||||
import me.lensfrex.trailblazer.api.v1.beans.responses.general.ErrorResponse; |
||||
import me.lensfrex.trailblazer.api.v1.beans.responses.general.ResponseBase; |
||||
import me.lensfrex.trailblazer.api.v1.beans.responses.general.ResponseCode; |
||||
import me.lensfrex.trailblazer.api.v1.exceptions.user.LoginInfoWrongException; |
||||
import me.lensfrex.trailblazer.api.v1.pojos.UserInformation; |
||||
import me.lensfrex.trailblazer.api.v1.utils.InputChecker; |
||||
import me.lensfrex.trailblazer.api.v1.dao.UserDao; |
||||
import me.lensfrex.trailblazer.api.v1.utils.jwt.JWTManager; |
||||
import org.mindrot.jbcrypt.BCrypt; |
||||
|
||||
import java.time.Instant; |
||||
import java.time.temporal.ChronoUnit; |
||||
import java.util.Date; |
||||
|
||||
@Path("/login") |
||||
public class Login { |
||||
private static final Gson gson = new Gson(); |
||||
private static final JWTManager jwtManager = JWTManager.getInstance(); |
||||
|
||||
@POST |
||||
@Produces(MediaType.APPLICATION_JSON) |
||||
public String login(String request) { |
||||
LoginRequestBody loginRequestBody; |
||||
try { |
||||
loginRequestBody = gson.fromJson(request, LoginRequestBody.class); |
||||
if (InputChecker.hasInvalidChar(loginRequestBody.getUserName()) || |
||||
InputChecker.hasInvisibleChar(loginRequestBody.getPassword())) { |
||||
ErrorResponse errorResponse = new ErrorResponse(100, "请求的用户名或密码非法"); |
||||
|
||||
return gson.toJson(errorResponse); |
||||
} |
||||
|
||||
UserInformation userDatabaseInformation = UserDao.getUser(loginRequestBody.getUserName()); |
||||
|
||||
if (userDatabaseInformation == null) { |
||||
throw new LoginInfoWrongException(); |
||||
} |
||||
|
||||
if (!identifyPassword(loginRequestBody.getPassword(), userDatabaseInformation.password)) { |
||||
throw new LoginInfoWrongException(); |
||||
} |
||||
|
||||
Date expireDate = Date.from(Instant.now().plus(JWTManager.TOKEN_DEFAULT_EXPIRE_DAY, ChronoUnit.DAYS)); |
||||
String userToken = jwtManager.createNewJWT(loginRequestBody.getUserName(), expireDate); |
||||
|
||||
LoginResponseData loginResponseData = new LoginResponseData( |
||||
userDatabaseInformation.uid, |
||||
userDatabaseInformation.uuid, |
||||
userDatabaseInformation.accountStatus, |
||||
userToken, |
||||
expireDate.getTime()); |
||||
|
||||
ResponseBase<LoginResponseData> response = ResponseBase.success(loginResponseData); |
||||
|
||||
return gson.toJson(response); |
||||
} catch (JsonParseException e) { |
||||
return gson.toJson(ResponseBase.error(ResponseCode.REQUEST_FORMAT_INVALID, "请求的数据格式不对")); |
||||
} catch (LoginInfoWrongException e) { |
||||
return gson.toJson(ResponseBase.error(ResponseCode.PASSWORD_WRONG, "用户名或密码错误")); |
||||
} catch (Exception e) { |
||||
return gson.toJson(ResponseBase.error(ResponseCode.SERVER_ERROR, "服务器内部错误,请联系那个背锅的家伙")); |
||||
} |
||||
} |
||||
|
||||
public boolean identifyPassword(String originPassword, String bcryptPassword) { |
||||
return BCrypt.checkpw(originPassword, bcryptPassword); |
||||
} |
||||
} |
@ -1,19 +0,0 @@ |
||||
package me.lensfrex.trailblazer.api.v1.service.profile; |
||||
|
||||
import jakarta.ws.rs.GET; |
||||
import jakarta.ws.rs.Path; |
||||
import jakarta.ws.rs.PathParam; |
||||
import jakarta.ws.rs.Produces; |
||||
import jakarta.ws.rs.core.MediaType; |
||||
|
||||
@Path("/profile") |
||||
public class Profile { |
||||
|
||||
@GET |
||||
@Path("/getItems/{uid}") |
||||
@Produces(MediaType.APPLICATION_JSON) |
||||
public String getProfiles(@PathParam("uid") String uid) { |
||||
return "unfinished feature..."; |
||||
} |
||||
|
||||
} |
@ -1,16 +0,0 @@ |
||||
package me.lensfrex.trailblazer.api.v1.service.token; |
||||
|
||||
import jakarta.ws.rs.POST; |
||||
import jakarta.ws.rs.Path; |
||||
import jakarta.ws.rs.Produces; |
||||
import jakarta.ws.rs.core.MediaType; |
||||
|
||||
@Path("/token") |
||||
public class TokenRefresher { |
||||
@Path("/refresh") |
||||
@POST |
||||
@Produces(MediaType.APPLICATION_JSON) |
||||
public String refreshToken(String request) { |
||||
return request; |
||||
} |
||||
} |
Loading…
Reference in new issue