org.shoulder.security.code.img.ImageCodeProcessor Maven / Gradle / Ivy
The newest version!
package org.shoulder.security.code.img;
import org.shoulder.code.exception.ValidateCodeException;
import org.shoulder.code.generator.ValidateCodeGenerator;
import org.shoulder.code.processor.AbstractValidateCodeProcessor;
import org.shoulder.code.propertities.BaseValidateCodeProperties;
import org.shoulder.code.store.ValidateCodeStore;
import org.springframework.web.context.request.ServletWebRequest;
import java.util.Objects;
import javax.imageio.ImageIO;
/**
* 图片验证码处理器
*
* @author lym
*/
public class ImageCodeProcessor extends AbstractValidateCodeProcessor implements ImageValidateCodeType {
public ImageCodeProcessor(BaseValidateCodeProperties baseValidateCodeProperties, ValidateCodeGenerator validateCodeGenerator, ValidateCodeStore validateCodeStore) {
super(baseValidateCodeProperties, validateCodeGenerator, validateCodeStore);
}
/**
* 发送图形验证码,将其写到响应中
*/
@Override
public void send(ServletWebRequest request, ImageCode imageCode) throws ValidateCodeException {
try {
ImageIO.write(imageCode.getImage(), "JPEG", Objects.requireNonNull(request.getResponse()).getOutputStream());
// 这里置为 null,help gc,避免服务器保存验证码时还要存图片信息
imageCode.setImage(null);
} catch (Exception e) {
throw new ValidateCodeException("send validate code fail.", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy