90 lines
2.5 KiB
Java
90 lines
2.5 KiB
Java
|
package com.yunan.controller;
|
||
|
|
||
|
import com.yunan.constant.ResponseCode;
|
||
|
import com.yunan.dto.ApiResponse;
|
||
|
import com.yunan.dto.LoginDTO;
|
||
|
import com.yunan.dto.RegisterDTO;
|
||
|
import com.yunan.entity.Mail;
|
||
|
import com.yunan.pojo.User;
|
||
|
import com.yunan.service.AuthService;
|
||
|
import com.yunan.service.EmailService;
|
||
|
import com.yunan.util.VerificationCodeUtils;
|
||
|
import io.swagger.annotations.Api;
|
||
|
import io.swagger.annotations.ApiOperation;
|
||
|
import io.swagger.v3.oas.annotations.parameters.RequestBody;
|
||
|
import lombok.extern.slf4j.Slf4j;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.http.ResponseEntity;
|
||
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
||
|
import javax.annotation.Resource;
|
||
|
import javax.mail.MessagingException;
|
||
|
import java.util.Map;
|
||
|
import java.util.concurrent.ConcurrentHashMap;
|
||
|
|
||
|
|
||
|
@RestController //注解标识这是一个控制器类
|
||
|
@RequestMapping("/auth")
|
||
|
@Slf4j
|
||
|
@Api(tags = "身份验证接口")
|
||
|
public class AuthController {
|
||
|
@Resource
|
||
|
private AuthService authService;
|
||
|
|
||
|
@Autowired
|
||
|
private EmailService emailService;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 用户登入
|
||
|
* @param loginDTO
|
||
|
*/
|
||
|
@PostMapping("/login")
|
||
|
@ApiOperation("用户登入")
|
||
|
public void login(@RequestBody LoginDTO loginDTO) {
|
||
|
// authService.login(loginDTO.getUsername(), loginDTO.getPassword());
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 用户注册
|
||
|
* @param registerDTO
|
||
|
*/
|
||
|
@PostMapping("/register")
|
||
|
@ApiOperation("用户注册")
|
||
|
public ResponseEntity<User> register(@RequestBody RegisterDTO registerDTO) {
|
||
|
// return authService.register(registerDTO);
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
// 存储已发送的验证码
|
||
|
private final Map<String, String> emailCodeMap = new ConcurrentHashMap<>(16);
|
||
|
|
||
|
/**
|
||
|
* 发送验证码
|
||
|
* @param mail
|
||
|
* @return {@link ApiResponse }<{@link String }>
|
||
|
* @throws MessagingException
|
||
|
*/
|
||
|
@PostMapping("/sendEmail")
|
||
|
@ApiOperation("邮箱验证码")
|
||
|
public ApiResponse<String> sendEmail(@RequestBody Mail mail) throws MessagingException {
|
||
|
// 生成验证码
|
||
|
String code = VerificationCodeUtils.generateCode(6);
|
||
|
|
||
|
// 发送邮件
|
||
|
String subject = "注册验证码";
|
||
|
String content = "尊敬的用户,您的验证码为:" + code;
|
||
|
emailService.sendMail(mail.email, subject, content);
|
||
|
|
||
|
// 保存验证码
|
||
|
emailCodeMap.put(mail.email, code);
|
||
|
|
||
|
return new ApiResponse<>(ResponseCode.SUCCESS,"验证码已发送");
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
}
|