Initialize patbond microservice modules
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.patbond.patbond</groupId>
|
||||
<artifactId>patbond-api</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>patbond-auth</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>patbond-auth</name>
|
||||
<description>Authentication service for Patbond</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.patbond.patbond</groupId>
|
||||
<artifactId>patbond-common</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.patbond.patbond.auth;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@EnableFeignClients
|
||||
@SpringBootApplication
|
||||
public class AuthApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AuthApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.patbond.patbond.auth.client;
|
||||
|
||||
import com.patbond.patbond.common.response.ApiResponse;
|
||||
import com.patbond.patbond.common.user.CreateUserRequest;
|
||||
import com.patbond.patbond.common.user.UserProfile;
|
||||
import com.patbond.patbond.common.user.VerifyPasswordRequest;
|
||||
import com.patbond.patbond.common.user.VerifyPasswordResponse;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
@FeignClient(name = "patbond-user")
|
||||
public interface UserClient {
|
||||
|
||||
@PostMapping("/internal/users")
|
||||
ApiResponse<UserProfile> createUser(@RequestBody CreateUserRequest request);
|
||||
|
||||
@PostMapping("/internal/users/verify-password")
|
||||
ApiResponse<VerifyPasswordResponse> verifyPassword(@RequestBody VerifyPasswordRequest request);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.patbond.patbond.auth.controller;
|
||||
|
||||
import com.patbond.patbond.auth.dto.AuthTokenResponse;
|
||||
import com.patbond.patbond.auth.dto.LoginRequest;
|
||||
import com.patbond.patbond.auth.dto.RegisterRequest;
|
||||
import com.patbond.patbond.auth.service.AuthService;
|
||||
import com.patbond.patbond.common.response.ApiResponse;
|
||||
import javax.validation.Valid;
|
||||
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;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/auth")
|
||||
public class AuthController {
|
||||
|
||||
private final AuthService authService;
|
||||
|
||||
public AuthController(AuthService authService) {
|
||||
this.authService = authService;
|
||||
}
|
||||
|
||||
@PostMapping("/register")
|
||||
public ApiResponse<AuthTokenResponse> register(@Valid @RequestBody RegisterRequest request) {
|
||||
return ApiResponse.success(authService.register(request));
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ApiResponse<AuthTokenResponse> login(@Valid @RequestBody LoginRequest request) {
|
||||
return ApiResponse.success(authService.login(request));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.patbond.patbond.auth.dto;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class AuthTokenResponse {
|
||||
|
||||
private String tokenType;
|
||||
private String accessToken;
|
||||
private LocalDateTime expiresAt;
|
||||
private Long userId;
|
||||
private String username;
|
||||
private String nickname;
|
||||
|
||||
public AuthTokenResponse(String tokenType, String accessToken, LocalDateTime expiresAt,
|
||||
Long userId, String username, String nickname) {
|
||||
this.tokenType = tokenType;
|
||||
this.accessToken = accessToken;
|
||||
this.expiresAt = expiresAt;
|
||||
this.userId = userId;
|
||||
this.username = username;
|
||||
this.nickname = nickname;
|
||||
}
|
||||
|
||||
public String getTokenType() {
|
||||
return tokenType;
|
||||
}
|
||||
|
||||
public String getAccessToken() {
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
public LocalDateTime getExpiresAt() {
|
||||
return expiresAt;
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getNickname() {
|
||||
return nickname;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.patbond.patbond.auth.dto;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
public class LoginRequest {
|
||||
|
||||
@NotBlank(message = "用户名不能为空")
|
||||
private String username;
|
||||
|
||||
@NotBlank(message = "密码不能为空")
|
||||
private String 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,53 @@
|
||||
package com.patbond.patbond.auth.dto;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
public class RegisterRequest {
|
||||
|
||||
@NotBlank(message = "用户名不能为空")
|
||||
@Size(min = 3, max = 32, message = "用户名长度必须在3到32位之间")
|
||||
private String username;
|
||||
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@Size(min = 6, max = 64, message = "密码长度必须在6到64位之间")
|
||||
private String password;
|
||||
|
||||
@Size(max = 32, message = "昵称长度不能超过32位")
|
||||
private String nickname;
|
||||
|
||||
@Size(max = 20, message = "手机号长度不能超过20位")
|
||||
private String phone;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getNickname() {
|
||||
return nickname;
|
||||
}
|
||||
|
||||
public void setNickname(String nickname) {
|
||||
this.nickname = nickname;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.patbond.patbond.auth.service;
|
||||
|
||||
import com.patbond.patbond.auth.client.UserClient;
|
||||
import com.patbond.patbond.auth.dto.AuthTokenResponse;
|
||||
import com.patbond.patbond.auth.dto.LoginRequest;
|
||||
import com.patbond.patbond.auth.dto.RegisterRequest;
|
||||
import com.patbond.patbond.common.response.ApiResponse;
|
||||
import com.patbond.patbond.common.user.CreateUserRequest;
|
||||
import com.patbond.patbond.common.user.UserProfile;
|
||||
import com.patbond.patbond.common.user.VerifyPasswordRequest;
|
||||
import com.patbond.patbond.common.user.VerifyPasswordResponse;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class AuthService {
|
||||
|
||||
private final UserClient userClient;
|
||||
|
||||
public AuthService(UserClient userClient) {
|
||||
this.userClient = userClient;
|
||||
}
|
||||
|
||||
public AuthTokenResponse register(RegisterRequest request) {
|
||||
ApiResponse<UserProfile> response = userClient.createUser(new CreateUserRequest(
|
||||
request.getUsername(),
|
||||
request.getPassword(),
|
||||
request.getNickname(),
|
||||
request.getPhone()
|
||||
));
|
||||
UserProfile user = requireData(response, "注册失败");
|
||||
return buildToken(user.getId(), user.getUsername(), user.getNickname());
|
||||
}
|
||||
|
||||
public AuthTokenResponse login(LoginRequest request) {
|
||||
ApiResponse<VerifyPasswordResponse> response = userClient.verifyPassword(
|
||||
new VerifyPasswordRequest(request.getUsername(), request.getPassword())
|
||||
);
|
||||
VerifyPasswordResponse user = requireData(response, "登录失败");
|
||||
return buildToken(user.getUserId(), user.getUsername(), user.getNickname());
|
||||
}
|
||||
|
||||
private AuthTokenResponse buildToken(Long userId, String username, String nickname) {
|
||||
return new AuthTokenResponse(
|
||||
"Bearer",
|
||||
UUID.randomUUID().toString().replace("-", ""),
|
||||
LocalDateTime.now().plusHours(2),
|
||||
userId,
|
||||
username,
|
||||
nickname
|
||||
);
|
||||
}
|
||||
|
||||
private <T> T requireData(ApiResponse<T> response, String defaultMessage) {
|
||||
if (response == null || !response.isSuccess() || response.getData() == null) {
|
||||
String message = response == null || response.getMessage() == null ? defaultMessage : response.getMessage();
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, message);
|
||||
}
|
||||
return response.getData();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
server:
|
||||
port: 8081
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: patbond-auth
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: ${NACOS_SERVER_ADDR:127.0.0.1:8848}
|
||||
config:
|
||||
server-addr: ${NACOS_SERVER_ADDR:127.0.0.1:8848}
|
||||
@@ -0,0 +1,12 @@
|
||||
server:
|
||||
port: 8081
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: patbond-auth
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: ${NACOS_SERVER_ADDR:127.0.0.1:8848}
|
||||
config:
|
||||
server-addr: ${NACOS_SERVER_ADDR:127.0.0.1:8848}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+7
@@ -0,0 +1,7 @@
|
||||
com/patbond/patbond/auth/dto/RegisterRequest.class
|
||||
com/patbond/patbond/auth/AuthApplication.class
|
||||
com/patbond/patbond/auth/dto/AuthTokenResponse.class
|
||||
com/patbond/patbond/auth/client/UserClient.class
|
||||
com/patbond/patbond/auth/controller/AuthController.class
|
||||
com/patbond/patbond/auth/dto/LoginRequest.class
|
||||
com/patbond/patbond/auth/service/AuthService.class
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
/home/zyx/workspace/duoduo/patbond/patbond-api/patbond-auth/src/main/java/com/patbond/patbond/auth/AuthApplication.java
|
||||
/home/zyx/workspace/duoduo/patbond/patbond-api/patbond-auth/src/main/java/com/patbond/patbond/auth/client/UserClient.java
|
||||
/home/zyx/workspace/duoduo/patbond/patbond-api/patbond-auth/src/main/java/com/patbond/patbond/auth/controller/AuthController.java
|
||||
/home/zyx/workspace/duoduo/patbond/patbond-api/patbond-auth/src/main/java/com/patbond/patbond/auth/dto/AuthTokenResponse.java
|
||||
/home/zyx/workspace/duoduo/patbond/patbond-api/patbond-auth/src/main/java/com/patbond/patbond/auth/dto/LoginRequest.java
|
||||
/home/zyx/workspace/duoduo/patbond/patbond-api/patbond-auth/src/main/java/com/patbond/patbond/auth/dto/RegisterRequest.java
|
||||
/home/zyx/workspace/duoduo/patbond/patbond-api/patbond-auth/src/main/java/com/patbond/patbond/auth/service/AuthService.java
|
||||
Reference in New Issue
Block a user