|
|
@@ -31,13 +31,16 @@ import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
-import javax.servlet.ServletOutputStream;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
+import jakarta.servlet.ServletOutputStream;
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
+import jakarta.servlet.http.HttpServletResponse; // 确保是 jakarta
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
@Service
|
|
|
public class UserBPOImpl extends BPOImpl implements UserBPO {
|
|
|
@@ -56,8 +59,8 @@ public class UserBPOImpl extends BPOImpl implements UserBPO {
|
|
|
@Autowired
|
|
|
private CacheManager leafCacheManager;
|
|
|
|
|
|
- @Autowired
|
|
|
- private HttpServletResponse response;
|
|
|
+// @Autowired
|
|
|
+// private HttpServletResponse response;
|
|
|
|
|
|
@Autowired
|
|
|
private SendSmsMapper sendSmsMapper;
|
|
|
@@ -493,40 +496,46 @@ public class UserBPOImpl extends BPOImpl implements UserBPO {
|
|
|
*/
|
|
|
@Override
|
|
|
public Map<String, String> render(){
|
|
|
- response.setDateHeader(HttpHeaders.EXPIRES, 0L);
|
|
|
- response.setHeader(HttpHeaders.CACHE_CONTROL, "no-store, no-cache, must-revalidate");
|
|
|
- response.addHeader(HttpHeaders.CACHE_CONTROL, "post-check=0, pre-check=0");
|
|
|
- response.setHeader(HttpHeaders.PRAGMA, "no-cache");
|
|
|
- response.setContentType("application/json;charset=UTF-8");
|
|
|
-
|
|
|
- CaptchaAutoConfiguration bean = GgfwSpringContextUtil.getBean ("captchaAutoConfiguration",CaptchaAutoConfiguration.class);
|
|
|
- DefaultKaptcha kaptcha = bean.defaultKaptcha();
|
|
|
- String code = kaptcha.createText();
|
|
|
- LOG.info("code="+code);
|
|
|
-
|
|
|
- Map<String, String> ajaxResponse = new HashMap<String, String>();
|
|
|
-
|
|
|
- ajaxResponse.put("appcode", Consts.APPCODE_SUCCESS);
|
|
|
- String reqid = UUID.randomUUID().toString();
|
|
|
- ajaxResponse.put("reqid", reqid);
|
|
|
- try (ServletOutputStream out = response.getOutputStream()) {
|
|
|
- ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
- ImageIO.write(kaptcha.createImage(code), "jpg", os);
|
|
|
- ajaxResponse.put("codeimg", Base64.getEncoder().encodeToString(os.toByteArray()));
|
|
|
- StringBuilder sb = new StringBuilder(JSONObject.toJSONString(ajaxResponse));
|
|
|
- out.write(sb.toString().getBytes("utf-8"));
|
|
|
- //放入缓存
|
|
|
- ValidCodeCacheDTO codeCacheDTO = new ValidCodeCacheDTO();
|
|
|
- codeCacheDTO.setCode(code);
|
|
|
- codeCacheDTO.setStarttime(System.currentTimeMillis());
|
|
|
- Cache cache = leafCacheManager.getCache(IMGCACHENAME);
|
|
|
-
|
|
|
- cache.put(getCodeCacheKey(reqid), codeCacheDTO);
|
|
|
+ ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ if (servletRequestAttributes != null) {
|
|
|
+ HttpServletResponse response = servletRequestAttributes.getResponse();
|
|
|
+ response.setDateHeader(HttpHeaders.EXPIRES, 0L);
|
|
|
+ response.setHeader(HttpHeaders.CACHE_CONTROL, "no-store, no-cache, must-revalidate");
|
|
|
+ response.addHeader(HttpHeaders.CACHE_CONTROL, "post-check=0, pre-check=0");
|
|
|
+ response.setHeader(HttpHeaders.PRAGMA, "no-cache");
|
|
|
+ response.setContentType("application/json;charset=UTF-8");
|
|
|
+
|
|
|
+ CaptchaAutoConfiguration bean = GgfwSpringContextUtil.getBean ("captchaAutoConfiguration",CaptchaAutoConfiguration.class);
|
|
|
+ DefaultKaptcha kaptcha = bean.defaultKaptcha();
|
|
|
+ String code = kaptcha.createText();
|
|
|
+ LOG.info("code="+code);
|
|
|
+
|
|
|
+ Map<String, String> ajaxResponse = new HashMap<String, String>();
|
|
|
+
|
|
|
+ ajaxResponse.put("appcode", Consts.APPCODE_SUCCESS);
|
|
|
+ String reqid = UUID.randomUUID().toString();
|
|
|
+ ajaxResponse.put("reqid", reqid);
|
|
|
+ try (ServletOutputStream out = response.getOutputStream()) {
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(kaptcha.createImage(code), "jpg", os);
|
|
|
+ ajaxResponse.put("codeimg", Base64.getEncoder().encodeToString(os.toByteArray()));
|
|
|
+ StringBuilder sb = new StringBuilder(JSONObject.toJSONString(ajaxResponse));
|
|
|
+ out.write(sb.toString().getBytes("utf-8"));
|
|
|
+ //放入缓存
|
|
|
+ ValidCodeCacheDTO codeCacheDTO = new ValidCodeCacheDTO();
|
|
|
+ codeCacheDTO.setCode(code);
|
|
|
+ codeCacheDTO.setStarttime(System.currentTimeMillis());
|
|
|
+ Cache cache = leafCacheManager.getCache(IMGCACHENAME);
|
|
|
+
|
|
|
+ cache.put(getCodeCacheKey(reqid), codeCacheDTO);
|
|
|
+ return ajaxResponse;
|
|
|
+ } catch (IOException e) {
|
|
|
+ LOG.info("验证码生成失败");
|
|
|
+ }
|
|
|
return ajaxResponse;
|
|
|
- } catch (IOException e) {
|
|
|
- LOG.info("验证码生成失败");
|
|
|
}
|
|
|
- return ajaxResponse;
|
|
|
+
|
|
|
+ return new HashMap<>();
|
|
|
}
|
|
|
private String getCodeCacheKey(String reqid){
|
|
|
return "ValidCode"+reqid;
|