|
|
@@ -5,17 +5,29 @@ import com.jzg.aop.PremissionCheck;
|
|
|
import com.jzg.config.JzgTokenService;
|
|
|
import com.jzg.config.JzgUserDetailsService;
|
|
|
import com.jzg.config.JzgUsernamePasswordAuthenticationToken;
|
|
|
+import com.jzg.config.RedissonConfig;
|
|
|
+import com.jzg.constants.RedisKeyConstants;
|
|
|
import com.jzg.entity.SysUser;
|
|
|
import com.jzg.entity.vo.LoginFormVo;
|
|
|
+import com.jzg.exception.SystemException;
|
|
|
import com.jzg.handler.JzgAuthenticationSuccessHandler;
|
|
|
import com.jzg.service.SysUserService;
|
|
|
+import com.wf.captcha.SpecCaptcha;
|
|
|
+import com.wf.captcha.base.Captcha;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
|
import io.swagger.v3.oas.models.annotations.OpenAPI30;
|
|
|
import jakarta.servlet.ServletException;
|
|
|
+import jakarta.servlet.ServletOutputStream;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.tomcat.util.http.fileupload.IOUtils;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
@@ -23,7 +35,13 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
import java.io.IOException;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping("/auth")
|
|
|
@@ -41,6 +59,9 @@ public class AuthController {
|
|
|
@Autowired
|
|
|
private JzgTokenService tokenStore;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ RedissonClient redissonClient;
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(AuthController.class);
|
|
|
/**
|
|
|
* 登录接口
|
|
|
* @param loginFormVo
|
|
|
@@ -58,7 +79,7 @@ public class AuthController {
|
|
|
public void login(@RequestBody LoginFormVo loginFormVo) throws ServletException, IOException {
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
|
|
|
- JzgUsernamePasswordAuthenticationToken authentication = new JzgUsernamePasswordAuthenticationToken(loginFormVo.getUserName(),loginFormVo.getPassWord(),loginFormVo.getSystem());
|
|
|
+ JzgUsernamePasswordAuthenticationToken authentication = new JzgUsernamePasswordAuthenticationToken(loginFormVo);
|
|
|
Authentication authenticate = authenticationManager.authenticate(authentication);
|
|
|
SecurityContextHolder.getContext().setAuthentication(authenticate);
|
|
|
JzgAuthenticationSuccessHandler successHandler = new JzgAuthenticationSuccessHandler(tokenStore);
|
|
|
@@ -66,6 +87,58 @@ public class AuthController {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @GetMapping("captchaById")
|
|
|
+ @Operation(summary = "获取验证码通过用户ID",
|
|
|
+ description = "获取验证码通过用户ID")
|
|
|
+ public void captchaByIdNew(HttpServletResponse response, String captchaId) throws Exception {
|
|
|
+ response.setHeader("Cache-Control", "no-store, no-cache");
|
|
|
+ response.setContentType("image/jpeg");
|
|
|
+ String text;
|
|
|
+ String imageId = null;
|
|
|
+ if(Objects.isNull(redissonClient.getBucket(RedisKeyConstants.VERIFICATION_CODE + captchaId))) {
|
|
|
+ imageId = redissonClient.getBucket(RedisKeyConstants.VERIFICATION_CODE + captchaId).get().toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ Captcha captcha = new SpecCaptcha();
|
|
|
+
|
|
|
+ // 重复验证码问题
|
|
|
+ if (StringUtils.isNotEmpty(imageId)) {
|
|
|
+ text = imageId;
|
|
|
+ } else {
|
|
|
+ // 生成文字验证码
|
|
|
+ text = captcha.text();
|
|
|
+ }
|
|
|
+ // 生成图片验证码
|
|
|
+ BufferedImage image = base64ToBufferedImage(captcha.toBase64());
|
|
|
+ if (StringUtils.isEmpty(captchaId)) {
|
|
|
+ throw new SystemException("captchaId 必传");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存到验证码到 redis
|
|
|
+ redissonClient.getBucket(captchaId).set(RedisKeyConstants.VERIFICATION_CODE + text, 120, TimeUnit.SECONDS);
|
|
|
+ ServletOutputStream out = response.getOutputStream();
|
|
|
+ ImageIO.write(image, "jpg", out);
|
|
|
+ IOUtils.closeQuietly(out);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static BufferedImage base64ToBufferedImage(String base64String) {
|
|
|
+ try {
|
|
|
+ // 去除 Base64 编码字符串中的数据前缀,如 "data:image/png;base64,"
|
|
|
+ String base64Image = base64String.split(",")[1];
|
|
|
+ // 解码 Base64 字符串为字节数组
|
|
|
+ byte[] imageBytes = Base64.getDecoder().decode(base64Image);
|
|
|
+ // 将字节数组转换为 ByteArrayInputStream
|
|
|
+ ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
|
|
|
+ // 使用 ImageIO 读取输入流并创建 BufferedImage
|
|
|
+ BufferedImage image = ImageIO.read(bis);
|
|
|
+ return image;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 获取token数据
|
|
|
* @param token
|