|
|
@ -4,35 +4,30 @@ import com.google.gson.Gson; |
|
|
|
import com.google.gson.JsonParseException; |
|
|
|
import com.google.gson.JsonParseException; |
|
|
|
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.general.ResponseBase; |
|
|
|
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.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.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.PostMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
|
|
|
|
|
|
|
|
import java.time.Instant; |
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.time.temporal.ChronoUnit; |
|
|
|
|
|
|
|
import java.util.Date; |
|
|
|
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RestController |
|
|
|
@RestController |
|
|
|
@RequestMapping("/user") |
|
|
|
@RequestMapping("/user") |
|
|
|
public class Register { |
|
|
|
public class Register { |
|
|
|
private static final JWTManager jwtManager = JWTManager.getInstance(); |
|
|
|
@Resource |
|
|
|
|
|
|
|
private RegisterService registerService; |
|
|
|
|
|
|
|
|
|
|
|
private static final Gson gson = new Gson(); |
|
|
|
private static final Gson gson = new Gson(); |
|
|
|
|
|
|
|
|
|
|
|
@PostMapping(value = "/register", produces = "application/json") |
|
|
|
@PostMapping(value = "/register", produces = "application/json") |
|
|
|
public String register(@RequestBody String request) { |
|
|
|
public String register(@RequestBody String request) { |
|
|
|
RegisterRequestBody registerRequestBody; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
registerRequestBody = gson.fromJson(request, RegisterRequestBody.class); |
|
|
|
RegisterRequestBody registerRequestBody = gson.fromJson(request, RegisterRequestBody.class); |
|
|
|
|
|
|
|
|
|
|
|
if (registerRequestBody == null || |
|
|
|
if (registerRequestBody == null || |
|
|
|
InputChecker.hasInvalidChar(registerRequestBody.getUserName()) || |
|
|
|
InputChecker.hasInvalidChar(registerRequestBody.getUserName()) || |
|
|
@ -41,25 +36,11 @@ public class Register { |
|
|
|
throw new RequestDataInvalidException(); |
|
|
|
throw new RequestDataInvalidException(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (UserDao.isUserAlreadyExist(registerRequestBody.getUserName())) { |
|
|
|
return gson.toJson(ResponseBase.success(registerService.register(registerRequestBody))); |
|
|
|
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)); |
|
|
|
|
|
|
|
} catch (JsonParseException | RequestDataInvalidException e) { |
|
|
|
} catch (JsonParseException | RequestDataInvalidException e) { |
|
|
|
return gson.toJson(ResponseBase.error(ResponseCode.REQUEST_FORMAT_INVALID, "请求的数据不正确")); |
|
|
|
return gson.toJson(ResponseBase.error(ResponseCode.REQUEST_FORMAT_INVALID, "请求的数据不正确")); |
|
|
|
|
|
|
|
} catch (UserNameAlreadyExistsException e) { |
|
|
|
|
|
|
|
return gson.toJson(ResponseBase.error(ResponseCode.USER_ALREADY_EXISTS, "用户名已经被使用")); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
return gson.toJson(ResponseBase.error(ResponseCode.SERVER_ERROR, "服务器程序发生错误,有个家伙又写bug了。Error:" + e.getMessage())); |
|
|
|
return gson.toJson(ResponseBase.error(ResponseCode.SERVER_ERROR, "服务器程序发生错误,有个家伙又写bug了。Error:" + e.getMessage())); |
|
|
|
} |
|
|
|
} |
|
|
|