feat(bookmarks): 集成VFS服务并优化用户注册与删除逻辑
新增VFSService用于与VFS系统交互,实现用户创建和删除功能。 在用户注册时调用VFS创建用户并获取token,在删除用户时同步删除VFS中的用户。 移除了原有使用UUID生成token的逻辑,改为由VFS返回token。 添加RestTemplate Bean配置以支持HTTP请求。 注释掉pom.xml中未使用的vfs-client代码生成插件配置。
This commit is contained in:
22
pom.xml
22
pom.xml
@ -116,28 +116,6 @@
|
|||||||
</configOptions>
|
</configOptions>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
<!-- <execution>
|
|
||||||
<id>vfs-client</id>
|
|
||||||
<goals>
|
|
||||||
<goal>generate</goal>
|
|
||||||
</goals>
|
|
||||||
<configuration>
|
|
||||||
<inputSpec>${project.basedir}/src/main/resources/config/vfs/vfs.yaml</inputSpec>
|
|
||||||
<generatorName>java</generatorName>
|
|
||||||
<configOptions>
|
|
||||||
<library>webclient</library>
|
|
||||||
<dateLibrary>java</dateLibrary>
|
|
||||||
<useBeanValidation>true</useBeanValidation>
|
|
||||||
<withJakartaValidation>true</withJakartaValidation>
|
|
||||||
<openApiNullable>false</openApiNullable>
|
|
||||||
<groupId>com.zzyxyz.api</groupId>
|
|
||||||
<artifactId>vfs-client</artifactId>
|
|
||||||
<artifactVersion>1.0.0</artifactVersion>
|
|
||||||
<modelPackage>com.zzyxyz.api.vfs.model</modelPackage>
|
|
||||||
<apiPackage>com.zzyxyz.api.vfs.api</apiPackage>
|
|
||||||
</configOptions>
|
|
||||||
</configuration>
|
|
||||||
</execution> -->
|
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package com.zzyxyz.api.bookmarks;
|
|||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
import org.springframework.web.cors.CorsConfiguration;
|
import org.springframework.web.cors.CorsConfiguration;
|
||||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||||
import org.springframework.web.filter.CorsFilter;
|
import org.springframework.web.filter.CorsFilter;
|
||||||
@ -40,4 +41,10 @@ public class CorsConfig {
|
|||||||
|
|
||||||
return new CorsFilter(source);
|
return new CorsFilter(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 添加RestTemplate Bean配置
|
||||||
|
@Bean
|
||||||
|
public RestTemplate restTemplate() {
|
||||||
|
return new RestTemplate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -10,7 +10,6 @@ import org.springframework.web.bind.annotation.CrossOrigin;
|
|||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@CrossOrigin(origins = "*")
|
@CrossOrigin(origins = "*")
|
||||||
@ -19,6 +18,9 @@ public class UserNPController implements DefaultApi, AuthApi {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserNPDAO userDAO;
|
private UserNPDAO userDAO;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private VFSService vfsService;
|
||||||
|
|
||||||
// 初始化数据库表
|
// 初始化数据库表
|
||||||
public UserNPController() {
|
public UserNPController() {
|
||||||
try {
|
try {
|
||||||
@ -38,6 +40,10 @@ public class UserNPController implements DefaultApi, AuthApi {
|
|||||||
return ResponseEntity.ok().build();
|
return ResponseEntity.ok().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean ret = vfsService.deleteUser(user.getUsername(), user.getToken());
|
||||||
|
if (!ret) {
|
||||||
|
return ResponseEntity.status(500).build();
|
||||||
|
}
|
||||||
userDAO.delete(user.getId());
|
userDAO.delete(user.getId());
|
||||||
return ResponseEntity.ok().build();
|
return ResponseEntity.ok().build();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -84,14 +90,11 @@ public class UserNPController implements DefaultApi, AuthApi {
|
|||||||
return ResponseEntity.status(401).build(); // 认证失败
|
return ResponseEntity.status(401).build(); // 认证失败
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成token
|
|
||||||
String token = UUID.randomUUID().toString();
|
|
||||||
user.setToken(token);
|
|
||||||
user.setUpdatedAt(OffsetDateTime.now());
|
user.setUpdatedAt(OffsetDateTime.now());
|
||||||
userDAO.update(user);
|
userDAO.update(user);
|
||||||
|
|
||||||
LoginResponse response = new LoginResponse();
|
LoginResponse response = new LoginResponse();
|
||||||
response.setToken(token);
|
response.setToken(user.getToken());
|
||||||
response.setUserId(user.getId());
|
response.setUserId(user.getId());
|
||||||
|
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
@ -114,6 +117,12 @@ public class UserNPController implements DefaultApi, AuthApi {
|
|||||||
UserNP user = new UserNP(username, "", registerRequest.getEmail());
|
UserNP user = new UserNP(username, "", registerRequest.getEmail());
|
||||||
user.hashPassword(registerRequest.getPassword());
|
user.hashPassword(registerRequest.getPassword());
|
||||||
|
|
||||||
|
String ret = vfsService.createUser(username, "random_token");
|
||||||
|
if (ret == null) {
|
||||||
|
return ResponseEntity.status(500).build();
|
||||||
|
}
|
||||||
|
user.setToken(ret);
|
||||||
|
|
||||||
// 保存到数据库
|
// 保存到数据库
|
||||||
UserNP savedUser = userDAO.insert(user);
|
UserNP savedUser = userDAO.insert(user);
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,76 @@
|
|||||||
|
package com.zzyxyz.api.bookmarks.user_np;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class VFSService {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(VFSService.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
private final String vfsBaseUrl = "http://localhost:8080/api"; // 根据实际情况调整
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建用户
|
||||||
|
*/
|
||||||
|
public String createUser(String username, String apiKey) {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
headers.set("X-VFS-Token", apiKey); // 根据API要求设置认证头
|
||||||
|
|
||||||
|
HttpEntity<String> entity = new HttpEntity<>("{}", headers);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ResponseEntity<String> response = restTemplate.postForEntity(
|
||||||
|
vfsBaseUrl + "/vfs/v1/users/" + username,
|
||||||
|
entity,
|
||||||
|
String.class
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.getStatusCode().is2xxSuccessful()) {
|
||||||
|
return response.getHeaders().getFirst("X-VFS-Token");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除用户
|
||||||
|
*/
|
||||||
|
public boolean deleteUser(String username, String token) {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.set("X-VFS-Token", token);
|
||||||
|
|
||||||
|
HttpEntity<String> entity = new HttpEntity<>(headers);
|
||||||
|
|
||||||
|
logger.info("deletUserToken: " + token);
|
||||||
|
try {
|
||||||
|
ResponseEntity<Void> response = restTemplate.exchange(
|
||||||
|
vfsBaseUrl + "/vfs/v1/users/" + username,
|
||||||
|
HttpMethod.DELETE,
|
||||||
|
entity,
|
||||||
|
Void.class
|
||||||
|
);
|
||||||
|
if (response.getStatusCode().is2xxSuccessful()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user