无法运行。增加注册逻辑
This commit is contained in:
@@ -14,7 +14,7 @@ public class HelloController {
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@GetMapping("/")
|
||||
@GetMapping("/public/hello")
|
||||
public Result getMethodName() {
|
||||
return Result.ok().message("Welcome to Yijiantong");
|
||||
}
|
||||
@@ -24,7 +24,7 @@ public class HelloController {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/set")
|
||||
@PostMapping("/public/set")
|
||||
public Result setRedis(@RequestParam(value = "username") String username) {
|
||||
//存储 key-value 键值对: "username"-"jaychou"
|
||||
stringRedisTemplate.opsForValue().set("username", username);
|
||||
@@ -36,7 +36,7 @@ public class HelloController {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/get")
|
||||
@GetMapping("/public/get")
|
||||
public Result getRedis(@RequestParam(value = "username") String username) {
|
||||
//通过 key 值读取 value
|
||||
String result = stringRedisTemplate.opsForValue().get(username);
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
package com.ttstd.videotablet.controller;
|
||||
|
||||
import com.ttstd.videotablet.entity.User;
|
||||
import com.ttstd.videotablet.service.UserService;
|
||||
import com.ttstd.videotablet.utils.TextUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@RestController
|
||||
public class LoginController {
|
||||
|
||||
private final UserService userService;
|
||||
private final AuthenticationManager authenticationManager;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Autowired
|
||||
public LoginController(UserService userService, AuthenticationManager authenticationManager) {
|
||||
this.userService = userService;
|
||||
this.authenticationManager = authenticationManager;
|
||||
}
|
||||
|
||||
@PostMapping("/public/register")
|
||||
public ResponseEntity<?> registerUser(@RequestBody RegisterRequest registerRequest) {
|
||||
try {
|
||||
userService.registerUser(registerRequest.getUsername(), registerRequest.getPassword());
|
||||
return new ResponseEntity<>("User registered successfully", HttpStatus.CREATED);
|
||||
} catch (RuntimeException e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/public/login")
|
||||
public ResponseEntity<?> login() {
|
||||
// 登录逻辑由Spring Security自动处理
|
||||
return ResponseEntity.ok("Login successful");
|
||||
}
|
||||
|
||||
// 注册请求参数类
|
||||
public static class RegisterRequest {
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
// Getters and Setters
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@PostMapping("/public/registerByPhone")
|
||||
public ResponseEntity<?> registerByPhone(@RequestBody PhoneRequest request) {
|
||||
String requestVerifyKey = request.getVerifyKey();
|
||||
if (TextUtils.isEmpty(requestVerifyKey)) {
|
||||
return new ResponseEntity<>("verify key is empty", HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
String phone = request.getPhone();
|
||||
Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
|
||||
if (map != null) {
|
||||
String verifyKey = (String) map.get("verifyKey");
|
||||
if (!Objects.equals(verifyKey, requestVerifyKey)) {
|
||||
return new ResponseEntity<>("verify key is not same", HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
String code = map.get("code").toString();
|
||||
if (!Objects.equals(code, request.getCode())) {
|
||||
return new ResponseEntity<>("code is not same", HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
try {
|
||||
User user = userService.registerByPhone(request.getPhone(), request.getCode(), new Date());
|
||||
return new ResponseEntity<>(user, HttpStatus.CREATED);
|
||||
} catch (RuntimeException e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
} else {
|
||||
return new ResponseEntity<>("verify key is expired", HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/public/loginByPhone")
|
||||
public ResponseEntity<?> loginByPhone(@RequestBody PhoneRequest request) {
|
||||
String requestVerifyKey = request.getVerifyKey();
|
||||
if (TextUtils.isEmpty(requestVerifyKey)) {
|
||||
return new ResponseEntity<>("verify key is empty", HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
try {
|
||||
User user = userService.loginByPhone(request.getPhone(), request.getCode());
|
||||
// 生成并返回JWT令牌(实际项目中需要实现JWT逻辑)
|
||||
|
||||
return ResponseEntity.ok("Login successful: " + user.getUsername());
|
||||
} catch (RuntimeException e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class PhoneRequest {
|
||||
private String phone;
|
||||
private String code;
|
||||
private String verifyKey;
|
||||
|
||||
// Getters and Setters
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getVerifyKey() {
|
||||
return verifyKey;
|
||||
}
|
||||
|
||||
public void setVerifyKey(String verifyKey) {
|
||||
this.verifyKey = verifyKey;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,7 +16,6 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@RestController
|
||||
public class SmsController {
|
||||
@@ -29,21 +28,31 @@ public class SmsController {
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@GetMapping("/verify_code")
|
||||
@GetMapping("/public/verify_code")
|
||||
public Result getMethodName(@RequestParam("phone") String phone) {
|
||||
if (TextUtils.isEmpty(phone)) {
|
||||
return Result.error().message("phone number is empty");
|
||||
}
|
||||
String oldCode = stringRedisTemplate.opsForValue().get(phone);
|
||||
if (TextUtils.isEmpty(oldCode)) {
|
||||
String code = SendSms.generatedcode(6);
|
||||
return sendCode(phone, code);
|
||||
Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
|
||||
if (map != null) {
|
||||
String oldCode = (String) map.get("code");
|
||||
long sentTime = (Long) map.get("sentAt");
|
||||
if (System.currentTimeMillis() - sentTime < Duration.ofMinutes(1).toMillis()) {
|
||||
return Result.error().message("code has been sent, please try again after 1 minute");
|
||||
}
|
||||
if (TextUtils.isEmpty(oldCode)) {
|
||||
String code = SendSms.generatedcode(6);
|
||||
return sendCode(phone, code, false);
|
||||
} else {
|
||||
return sendCode(phone, oldCode, true);
|
||||
}
|
||||
} else {
|
||||
return sendCode(phone, oldCode);
|
||||
String code = SendSms.generatedcode(6);
|
||||
return sendCode(phone, code, false);
|
||||
}
|
||||
}
|
||||
|
||||
private Result sendCode(String phone, String code) {
|
||||
private Result sendCode(String phone, String code, boolean sent) {
|
||||
try {
|
||||
com.aliyun.dysmsapi20170525.Client client = createClient();
|
||||
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
|
||||
@@ -54,20 +63,20 @@ public class SmsController {
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
||||
// System.out.println(sendSmsResponse.body.toMap());
|
||||
// System.out.println(sendSmsResponse.getBody().getMessage());
|
||||
// System.out.println(sendSmsResponse.getBody().getCode());
|
||||
if ("OK".equals(sendSmsResponse.getBody().getCode())) {
|
||||
String randomString = RandomStringUtils.randomAlphanumeric(32);
|
||||
long expireAt = System.currentTimeMillis() + Duration.ofMinutes(5).toMillis();
|
||||
long now = System.currentTimeMillis();
|
||||
long expireAt = now + Duration.ofMinutes(LOGIN_CODE_TTL).toMillis();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("phone", phone);
|
||||
map.put("code", code);
|
||||
map.put("sms", sendSmsResponse.getBody().getMessage());
|
||||
map.put("verifyKey", randomString);
|
||||
map.put("sentAt", now);
|
||||
map.put("expireAt", expireAt);
|
||||
//4.保存验证码到Redis,并且设置有效期5分钟
|
||||
redisTemplate.opsForValue().set(phone, map, Duration.ofMinutes(5));
|
||||
if (!sent) {
|
||||
redisTemplate.opsForValue().set(phone, map, Duration.ofMinutes(5));
|
||||
}
|
||||
return Result.ok().data(map);
|
||||
} else {
|
||||
return Result.error().message(sendSmsResponse.getBody().getMessage());
|
||||
|
||||
Reference in New Issue
Block a user