基本实现手机验证码,账号密码登录。腾讯云短信后台显示成功,但是收不到,也没有报错
This commit is contained in:
@@ -2,22 +2,25 @@ package com.onekeycall.videotablet.controller;
|
||||
|
||||
import com.onekeycall.videotablet.dto.TokenPair;
|
||||
import com.onekeycall.videotablet.entity.User;
|
||||
import com.onekeycall.videotablet.result.Result;
|
||||
import com.onekeycall.videotablet.service.UserService;
|
||||
import com.onekeycall.videotablet.utils.JwtUtil;
|
||||
import com.onekeycall.videotablet.utils.TextUtils;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
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.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
|
||||
@RestController
|
||||
public class LoginController {
|
||||
@@ -47,9 +50,21 @@ public class LoginController {
|
||||
}
|
||||
|
||||
@PostMapping("/public/login")
|
||||
public ResponseEntity<?> login() {
|
||||
// 登录逻辑由Spring Security自动处理
|
||||
return ResponseEntity.ok("Login successful");
|
||||
public ResponseEntity<?> login(
|
||||
@RequestParam(value = "user_id") String userId, @RequestParam String password,
|
||||
@RequestParam(value = "device_id", required = false) String deviceId) {
|
||||
// 1. 创建认证令牌
|
||||
Authentication authenticationToken = new UsernamePasswordAuthenticationToken(userId, password);
|
||||
|
||||
// 2. 使用 AuthenticationManager 进行认证(核心步骤)
|
||||
Authentication authentication = authenticationManager.authenticate(authenticationToken);
|
||||
|
||||
// 3. 认证成功后生成 JWT
|
||||
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
|
||||
TokenPair tokenPair = jwtUtil.generateTokenPair(userDetails.getUsername(), deviceId);
|
||||
|
||||
// 4. 返回 Token
|
||||
return ResponseEntity.ok(Collections.singletonMap("token", tokenPair.toMap()));
|
||||
}
|
||||
|
||||
// 注册请求参数类
|
||||
@@ -77,84 +92,123 @@ public class LoginController {
|
||||
}
|
||||
|
||||
@PostMapping("/public/registerByPhone")
|
||||
public ResponseEntity<?> registerByPhone(
|
||||
public Result registerByPhone(
|
||||
@RequestParam String phone, @RequestParam String code,
|
||||
@RequestParam(value = "verify_key") String verifyKey, @RequestParam(value = "device_id") String deviceId) {
|
||||
|
||||
if (TextUtils.isEmpty(verifyKey)) {
|
||||
return new ResponseEntity<>("verify key is empty", HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
//
|
||||
// if (TextUtils.isEmpty(verifyKey)) {
|
||||
// return Result.error().message("verify key is empty", HttpStatus.BAD_REQUEST);
|
||||
// }
|
||||
Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
|
||||
if (map != null) {
|
||||
String redisVerifyKey = (String) map.get("verifyKey");
|
||||
if (!Objects.equals(redisVerifyKey, verifyKey)) {
|
||||
return new ResponseEntity<>("verify key is not same", HttpStatus.BAD_REQUEST);
|
||||
return Result.error().message("verify key is not same");
|
||||
}
|
||||
String redisCode = map.get("code").toString();
|
||||
if (!Objects.equals(redisCode, code)) {
|
||||
return new ResponseEntity<>("code is not same", HttpStatus.BAD_REQUEST);
|
||||
return Result.error().message("code is not same");
|
||||
}
|
||||
try {
|
||||
User user = userService.registerByPhone(phone, code, deviceId, new Date());
|
||||
TokenPair tokenPair = jwtUtil.generateTokenPair(user.getUserId(), deviceId);
|
||||
//返回给app保存,access_token用来加入header请求接口,refresh_token用来更换access_token
|
||||
return new ResponseEntity<>(tokenPair, HttpStatus.CREATED);
|
||||
Map<String, Object> tokenMap = new HashMap<>();
|
||||
tokenMap.put("new_user", user.isNewUser());
|
||||
tokenMap.put("user_id", user.getUserId());
|
||||
tokenMap.put("has_password", user.isHasPassword());
|
||||
tokenMap.put("token", tokenPair.toMap());
|
||||
return Result.ok().data(tokenMap);
|
||||
} catch (RuntimeException e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
return Result.error().message(e.getMessage());
|
||||
} finally {
|
||||
redisTemplate.delete(phone);
|
||||
}
|
||||
} else {
|
||||
return new ResponseEntity<>("verify key is expired", HttpStatus.BAD_REQUEST);
|
||||
return Result.error().message("verify key is expired");
|
||||
}
|
||||
}
|
||||
|
||||
@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 Result loginByPhone(
|
||||
@RequestParam String phone, @RequestParam String code,
|
||||
@RequestParam(value = "verify_key") String verifyKey, @RequestParam(value = "device_id") String deviceId) {
|
||||
Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
|
||||
if (map != null) {
|
||||
String redisVerifyKey = (String) map.get("verifyKey");
|
||||
if (!Objects.equals(redisVerifyKey, verifyKey)) {
|
||||
return Result.error().message("verify key is not same");
|
||||
}
|
||||
String redisCode = map.get("code").toString();
|
||||
if (!Objects.equals(redisCode, code)) {
|
||||
return Result.error().message("code is not same");
|
||||
}
|
||||
try {
|
||||
User user = userService.loginByPhone(phone, code);
|
||||
// 生成并返回JWT令牌(实际项目中需要实现JWT逻辑)
|
||||
TokenPair tokenPair = jwtUtil.generateTokenPair(user.getUserId(), deviceId);
|
||||
Map<String, Object> tokenMap = new HashMap<>();
|
||||
tokenMap.put("new_user", user.isNewUser());
|
||||
tokenMap.put("user_id", user.getUserId());
|
||||
tokenMap.put("has_password", user.isHasPassword());
|
||||
tokenMap.put("token", tokenPair.toMap());
|
||||
return Result.ok().data(tokenMap);
|
||||
} catch (RuntimeException e) {
|
||||
return Result.error().message(e.getMessage());
|
||||
}
|
||||
} else {
|
||||
return Result.error().message("verify key is expired");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
@PostMapping("/public/setPasswordByPhone")
|
||||
public Result setPasswordByPhone(
|
||||
HttpServletRequest request, @RequestParam(value = "user_id") String userId,
|
||||
@RequestParam String password, @RequestParam(value = "verify_password") String verifyPassword,
|
||||
@RequestParam(value = "device_id") String deviceId) {
|
||||
String authHeader = request.getHeader("Authorization");
|
||||
if (authHeader != null && authHeader.startsWith("Bearer ")) {
|
||||
String token = authHeader.substring(7); // 提取真正的Token
|
||||
if (StringUtils.equals(password, verifyPassword)) {
|
||||
if (jwtUtil.validateAccessToken(userId, token, deviceId)) {
|
||||
userService.setPasswordByUserId(userId, password);
|
||||
return Result.ok().message("set password success");
|
||||
} else {
|
||||
return Result.error().message("token is not same");
|
||||
}
|
||||
} else {
|
||||
return Result.error().message("password is not same");
|
||||
}
|
||||
} else {
|
||||
return Result.error().message("Authorization header is incorrect");
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/public/changePassword")
|
||||
public Result changePassword(
|
||||
HttpServletRequest request,
|
||||
@RequestParam(value = "user_id") String userId,
|
||||
@RequestParam(value = "old_password") String oldPassword,
|
||||
@RequestParam String password, @RequestParam(value = "verify_password") String verifyPassword,
|
||||
@RequestParam(value = "device_id") String deviceId) {
|
||||
String authHeader = request.getHeader("Authorization");
|
||||
if (authHeader != null && authHeader.startsWith("Bearer ")) {
|
||||
String token = authHeader.substring(7);
|
||||
if (StringUtils.equals(password, verifyPassword)) {
|
||||
if (!oldPassword.equals(password)) {
|
||||
if (jwtUtil.validateAccessToken(userId, token, deviceId)) {
|
||||
return userService.changePassword(userId, oldPassword, password);
|
||||
} else {
|
||||
return Result.error().message("token is not same");
|
||||
}
|
||||
} else {
|
||||
return Result.error().message("The old password and the new password are the same");
|
||||
}
|
||||
} else {
|
||||
return Result.error().message("password is not same");
|
||||
}
|
||||
} else {
|
||||
return Result.error().message("Authorization header is incorrect");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user