Compare commits
No commits in common. "af4a0f9684b4e8602a533d382e9b800a15f0ba64" and "3137c223137c2725d4ff5d7066bee0c8449753a8" have entirely different histories.
af4a0f9684
...
3137c22313
@ -6,21 +6,20 @@ import org.cmh.backend.Utils.JwtVerify;
|
||||
import org.cmh.backend.authentication.dto.*;
|
||||
import org.cmh.backend.authentication.model.UserHS;
|
||||
import org.cmh.backend.authentication.service.UserService;
|
||||
import org.cmh.backend.authentication.service.VerificationCodeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
class AuthenticationController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private VerificationCodeService verificationCodeService;
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String hello() {
|
||||
return "Hello SpringBoot!";
|
||||
@ -60,7 +59,8 @@ class AuthenticationController {
|
||||
if (JwtUtil.isTokenValid(token)) {
|
||||
UserHS user = userService.getUserByUsername(JwtUtil.extractUsername(token));
|
||||
if (user != null) {
|
||||
UserProfileResponse response = new UserProfileResponse(
|
||||
|
||||
return new ResponseEntity<>(new UserProfileResponse(
|
||||
user.getUsername(),
|
||||
user.getNickname(),
|
||||
user.getGender(),
|
||||
@ -69,12 +69,7 @@ class AuthenticationController {
|
||||
user.getDepartment(),
|
||||
user.getRole(),
|
||||
user.getCreatedAt()
|
||||
);
|
||||
if (user.getSuperAdmin()) {
|
||||
response.setDepartment("超级管理员");
|
||||
response.setRole("超级管理员");
|
||||
}
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
@ -100,9 +95,18 @@ class AuthenticationController {
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@GetMapping("/getVerificationCode")
|
||||
public ResponseEntity<VerificationCodeResponse> getVerificationCode() {
|
||||
return new ResponseEntity<>(verificationCodeService.provideVerificationCode(), HttpStatus.OK);
|
||||
@PostMapping("/sendVerificationCode")
|
||||
public ResponseEntity<Map<String, Integer>> sendVerificationCode(@RequestBody Map<String, String> request) {
|
||||
String contactInfo = request.get("contactInfo");
|
||||
// TODO:发送验证码功能有待添加。 boolean isSent = userService.sendVerificationCode(contactInfo);
|
||||
boolean isSent = true;
|
||||
if (isSent) {
|
||||
HashMap<String, Integer> response = new HashMap<>();
|
||||
response.put("code", 1234);
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
} else {
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
package org.cmh.backend.authentication.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class VerificationCodeResponse {
|
||||
private String code;
|
||||
private String path;
|
||||
}
|
||||
@ -25,5 +25,4 @@ public class UserHS {
|
||||
private String department;
|
||||
private String role;
|
||||
private LocalDateTime createdAt;
|
||||
private Boolean superAdmin = false;
|
||||
}
|
||||
|
||||
@ -1,51 +0,0 @@
|
||||
package org.cmh.backend.authentication.service;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.Getter;
|
||||
import org.cmh.backend.authentication.dto.VerificationCodeResponse;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Service
|
||||
public class VerificationCodeService {
|
||||
|
||||
@Value("${verification.code.images.path}")
|
||||
private String verificationCodeImagesPath;
|
||||
|
||||
@Getter
|
||||
private List<String> verificationCodeList = new ArrayList<>();
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
loadVerificationCodeImages();
|
||||
}
|
||||
|
||||
private void loadVerificationCodeImages() {
|
||||
try (Stream<Path> paths = Files.list(Paths.get(verificationCodeImagesPath))) {
|
||||
paths.filter(path -> path.toString().endsWith(".png"))
|
||||
.forEach(path -> verificationCodeList.add(path.getFileName().toString()));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public VerificationCodeResponse provideVerificationCode() {
|
||||
if (verificationCodeList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Random random = new Random();
|
||||
int code = random.nextInt(random.nextInt(verificationCodeList.size()));
|
||||
String selectedCode = verificationCodeList.get(code);
|
||||
return new VerificationCodeResponse(selectedCode.split("\\.")[0], "/verificationCodeImages/" + selectedCode);
|
||||
}
|
||||
|
||||
}
|
||||
@ -20,7 +20,5 @@ spring.datasource.hikari.connection-timeout=30000
|
||||
server.servlet.encoding.enabled=true
|
||||
server.servlet.encoding.force=true
|
||||
server.servlet.encoding.charset=utf-8
|
||||
# verificationCode
|
||||
verification.code.images.path=src/main/resources/static/verificationCodeImages
|
||||
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.3 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.0 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 2.3 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 3.0 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 3.4 KiB |