package com.dy.pipIrrApp.captcha; import com.dy.common.multiDataSource.DataSourceContext; import com.dy.common.webUtil.BaseResponse; import com.dy.common.webUtil.BaseResponseUtils; import com.dy.pipIrrApp.captcha.dto.CaptchaDomain; import com.dy.pipIrrGlobal.pojoBa.BaCaptcha; import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.imageio.ImageIO; import java.io.IOException; import java.util.Calendar; /** * @author ZhuBaoMin * @date 2024-09-11 13:45 * @LastEditTime 2024-09-11 13:45 * @Description */ @Slf4j @RestController @RequestMapping(path="captcha") public class CaptchaCtrl { @Autowired private CaptchaSV captchaSV; /** * 获取验证码 * @param type,图片类型:char-文本,math-一位数算式,math2-两位数算式 * @return */ @GetMapping("/get") @ResponseBody public BaseResponse getCaptcha(HttpServletRequest request, @RequestParam(value = "type", required = false, defaultValue = "char") String type) { // 配置数据源 HttpServletRequest httpRequest = (HttpServletRequest) request; String wxDataSourceName = httpRequest.getHeader("tag"); if(wxDataSourceName != null && wxDataSourceName.trim().length() > 0){ log.info("微信开发,设置数据源名称为:" + wxDataSourceName); //把组织单位标签作为数据源名称 DataSourceContext.set(wxDataSourceName); } else { log.info("用户未选择数据源"); } // 生成验证码实体 CaptchaDomain captchaDomain = captchaSV.createGoogleCaptcha(type); if (null != captchaDomain) { // 验证码保存至redis // redisUtils.set(captchaDomain.getToken(), captchaDomain.getCode(), 300L); // 验证码保存至session //HttpSession session = (HttpSession) request.getSession(); //session.setAttribute(captchaDomain.getToken(), captchaDomain.getCode()); /** * 验证保存到数据库 * 获取当前时间戳并延后3分钟 */ Long timestamp = System.currentTimeMillis(); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timestamp); calendar.add(Calendar.SECOND, 180); BaCaptcha baCaptcha = new BaCaptcha(); baCaptcha.setToken(captchaDomain.getToken()); baCaptcha.setCode(captchaDomain.getCode()); baCaptcha.setExpiration(calendar.getTimeInMillis()); Long rec = captchaSV.addCaptcha(baCaptcha); System.out.println("token: " + captchaDomain.getToken() + "; code: " + captchaDomain.getCode()); // 无用信息设空 captchaDomain.setText(null); captchaDomain.setCode(null); // 返回前端信息 return BaseResponseUtils.buildSuccess(captchaDomain); } else { return null; } } /** * 直接输出图片 * @param type,图片类型:char-文本,math-一位数算式,math2-两位数算式 */ @GetMapping("/get/image") public void getCaptchaImage(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "type", required = false, defaultValue = "char") String type) { CaptchaDomain captchaDomain = null; // 生成谷歌验证码实体 captchaDomain = captchaSV.createGoogleCaptcha(type); // 验证码保存至redis // redisUtils.set(captchaDomain.getToken(), captchaDomain.getCode(), 300L); // 验证码保存至session //HttpSession session = (HttpSession) request.getSession(); //session.setAttribute(captchaDomain.getToken(), captchaDomain.getCode()); /** * 验证保存到数据库 * 获取当前时间戳并延后3分钟 */ Long timestamp = System.currentTimeMillis(); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timestamp); calendar.add(Calendar.SECOND, 180); BaCaptcha baCaptcha = new BaCaptcha(); baCaptcha.setToken(captchaDomain.getToken()); baCaptcha.setCode(captchaDomain.getCode()); baCaptcha.setExpiration(calendar.getTimeInMillis()); Long rec = captchaSV.addCaptcha(baCaptcha); System.out.println("token: " + captchaDomain.getToken() + "; code: " + captchaDomain.getCode()); // 以文件流的形式,输出验证码图片 ServletOutputStream out = null; try { response.setContentType("image/jpeg"); out = response.getOutputStream(); ImageIO.write(captchaDomain.getImage(), "jpg", out); out.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } }