试图用spring boot重写了一下大部分,login部分尝试参考了一下其他项目进行拆分为controller和service部分

spring-dev
lensfrex 3 years ago
parent 2a4228d0e1
commit 207a8caf21
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 36
      pom.xml
  2. 17
      src/main/java/me/lensfrex/trailblazer/api/v1/ServerMain.java
  3. 18
      src/main/java/me/lensfrex/trailblazer/api/v1/controllers/FeedBack.java
  4. 15
      src/main/java/me/lensfrex/trailblazer/api/v1/controllers/Index.java
  5. 50
      src/main/java/me/lensfrex/trailblazer/api/v1/controllers/auth/login/Login.java
  6. 18
      src/main/java/me/lensfrex/trailblazer/api/v1/controllers/auth/register/Register.java
  7. 16
      src/main/java/me/lensfrex/trailblazer/api/v1/controllers/auth/token/TokenChecker.java
  8. 16
      src/main/java/me/lensfrex/trailblazer/api/v1/controllers/auth/token/TokenRefresher.java
  9. 15
      src/main/java/me/lensfrex/trailblazer/api/v1/controllers/profile/Profile.java
  10. 20
      src/main/java/me/lensfrex/trailblazer/api/v1/service/FeedBack.java
  11. 16
      src/main/java/me/lensfrex/trailblazer/api/v1/service/Index.java
  12. 48
      src/main/java/me/lensfrex/trailblazer/api/v1/service/auth/login/LoginService.java
  13. 4
      src/main/java/me/lensfrex/trailblazer/api/v1/service/auth/register/Register.java
  14. 10
      src/main/java/me/lensfrex/trailblazer/api/v1/service/auth/token/TokenChecker.java
  15. 7
      src/main/java/me/lensfrex/trailblazer/api/v1/service/auth/token/TokenRefresher.java
  16. 78
      src/main/java/me/lensfrex/trailblazer/api/v1/service/login/Login.java
  17. 19
      src/main/java/me/lensfrex/trailblazer/api/v1/service/profile/Profile.java
  18. 16
      src/main/java/me/lensfrex/trailblazer/api/v1/service/token/TokenRefresher.java
  19. 2
      src/main/webapp/WEB-INF/web.xml

@ -1,21 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>me.lensfrex</groupId> <groupId>me.lensfrex</groupId>
<artifactId>Trailblazer</artifactId> <artifactId>Trailblazer</artifactId>
<version>0.0.1-dev</version> <version>0.0.1-dev</version>
<name>Trailblazer</name> <name>Trailblazer</name>
<packaging>war</packaging> <packaging>war</packaging>
<properties> <properties>
<start-class>me.lensfrex.trailblazer.api.v1.ServerMain</start-class>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<junit.version>5.8.1</junit.version> <junit.version>5.8.1</junit.version>
</properties> </properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/>
</parent>
<dependencies> <dependencies>
<dependency> <dependency>
@ -25,22 +33,6 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>3.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
@ -93,6 +85,16 @@
<version>2.11.0</version> <version>2.11.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -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);
}
}

@ -1,11 +1,7 @@
package me.lensfrex.trailblazer.api.v1.service.register; package me.lensfrex.trailblazer.api.v1.controllers.auth.register;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonParseException; 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.RegisterRequestBody; 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.general.ResponseCode;
import me.lensfrex.trailblazer.api.v1.beans.responses.RegisterResponseData; import me.lensfrex.trailblazer.api.v1.beans.responses.RegisterResponseData;
@ -15,20 +11,24 @@ import me.lensfrex.trailblazer.api.v1.exceptions.RequestDataInvalidException;
import me.lensfrex.trailblazer.api.v1.utils.InputChecker; import me.lensfrex.trailblazer.api.v1.utils.InputChecker;
import me.lensfrex.trailblazer.api.v1.utils.jwt.JWTManager; import me.lensfrex.trailblazer.api.v1.utils.jwt.JWTManager;
import org.mindrot.jbcrypt.BCrypt; 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.Instant;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Date; import java.util.Date;
import java.util.UUID; import java.util.UUID;
@Path("/register") @RestController
@RequestMapping("/user")
public class Register { public class Register {
private static final JWTManager jwtManager = JWTManager.getInstance(); private static final JWTManager jwtManager = JWTManager.getInstance();
private static final Gson gson = new Gson(); private static final Gson gson = new Gson();
@POST @PostMapping(value = "/register", produces = "application/json")
@Produces(MediaType.APPLICATION_JSON) public String register(@RequestBody String request) {
public String register(String request) {
RegisterRequestBody registerRequestBody; RegisterRequestBody registerRequestBody;
try { try {

@ -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; import me.lensfrex.trailblazer.api.v1.utils.jwt.JWTManager;
@Path("/token")
public class TokenChecker { public class TokenChecker {
public static final JWTManager jwtManager = JWTManager.getInstance(); public static final JWTManager jwtManager = JWTManager.getInstance();
@POST
@Path("/check")
@Produces(MediaType.APPLICATION_JSON)
public String checkTokenAvailable(String request) { public String checkTokenAvailable(String request) {
return String.valueOf(jwtManager.verifyToken(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;
}
}

@ -6,7 +6,7 @@
<servlet> <servlet>
<servlet-name>TrailblazerApiV1</servlet-name> <servlet-name>TrailblazerApiV1</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <!-- <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>-->
<init-param> <init-param>
<param-name>jersey.config.server.provider.packages</param-name> <param-name>jersey.config.server.provider.packages</param-name>

Loading…
Cancel
Save