添加验证码支持
This commit is contained in:
parent
b7ede5652c
commit
ae0417c77f
@ -6,20 +6,21 @@ import org.cmh.backend.Utils.JwtVerify;
|
|||||||
import org.cmh.backend.authentication.dto.*;
|
import org.cmh.backend.authentication.dto.*;
|
||||||
import org.cmh.backend.authentication.model.UserHS;
|
import org.cmh.backend.authentication.model.UserHS;
|
||||||
import org.cmh.backend.authentication.service.UserService;
|
import org.cmh.backend.authentication.service.UserService;
|
||||||
|
import org.cmh.backend.authentication.service.VerificationCodeService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
class AuthenticationController {
|
class AuthenticationController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private VerificationCodeService verificationCodeService;
|
||||||
|
|
||||||
@GetMapping("/hello")
|
@GetMapping("/hello")
|
||||||
public String hello() {
|
public String hello() {
|
||||||
return "Hello SpringBoot!";
|
return "Hello SpringBoot!";
|
||||||
@ -99,18 +100,9 @@ class AuthenticationController {
|
|||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/sendVerificationCode")
|
@GetMapping("/getVerificationCode")
|
||||||
public ResponseEntity<Map<String, Integer>> sendVerificationCode(@RequestBody Map<String, String> request) {
|
public ResponseEntity<VerificationCodeResponse> getVerificationCode() {
|
||||||
String contactInfo = request.get("contactInfo");
|
return new ResponseEntity<>(verificationCodeService.provideVerificationCode(), HttpStatus.OK);
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,13 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
@ -0,0 +1,51 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user