|
|
@ -7,26 +7,32 @@ import com.yxt.common.base.config.RedisUtil; |
|
|
|
import com.yxt.common.base.config.captcha.CaptchaBaseParam; |
|
|
|
import com.yxt.common.base.config.captcha.CaptchaType; |
|
|
|
import com.yxt.common.base.config.captcha.ImageUtils; |
|
|
|
import com.yxt.common.base.config.captcha.RandomUtils; |
|
|
|
import com.yxt.common.base.config.captcha.factory.CaptchaFactory; |
|
|
|
import com.yxt.common.base.config.captcha.factory.ICaptchaFactory; |
|
|
|
import com.yxt.common.base.config.captcha.factory.impl.AbstractCaptcha; |
|
|
|
import com.yxt.common.base.utils.AjaxResult; |
|
|
|
import com.yxt.common.base.config.component.FileUploadComponent; |
|
|
|
import com.yxt.common.base.utils.Base64; |
|
|
|
import com.yxt.common.base.utils.VerifyCodeUtils; |
|
|
|
import com.yxt.common.base.utils.WordConvertUtils; |
|
|
|
import com.yxt.common.core.result.ResultBean; |
|
|
|
import com.yxt.common.core.vo.CaptchaBaseVO; |
|
|
|
import io.swagger.annotations.Api; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
@Api(tags = "登录验证码") |
|
|
|
@RestController |
|
|
|
@RequestMapping("v1/captcha") |
|
|
@ -40,6 +46,12 @@ public class CaptchaRest implements CaptchaApi { |
|
|
|
@Autowired(required = false) |
|
|
|
private ImageUtils imageUtils; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private FileUploadComponent fileUploadComponent; |
|
|
|
|
|
|
|
@Value("${image.login.path:static/upload/}") |
|
|
|
private String imageLoginPath; |
|
|
|
|
|
|
|
/** |
|
|
|
* 超时时间3min |
|
|
|
*/ |
|
|
@ -85,7 +97,7 @@ public class CaptchaRest implements CaptchaApi { |
|
|
|
captchaBaseParam.setUrlOrPath(imageUtils.getBlockPuzzleBgPath()); |
|
|
|
captcha.setJigsawUrlOrPath(imageUtils.getBlockPuzzleJigsawPath()); |
|
|
|
CaptchaBaseVO dataVO = captcha.create(captchaBaseParam); |
|
|
|
Map<String,Object> ajaxResult = new HashMap<>(); |
|
|
|
Map<String, Object> ajaxResult = new HashMap<>(); |
|
|
|
ajaxResult.put("dataVo", dataVO); |
|
|
|
Map map = JSON.parseObject(JSON.toJSONString(dataVO), Map.class); |
|
|
|
String point = map.get("point").toString(); |
|
|
@ -114,7 +126,7 @@ public class CaptchaRest implements CaptchaApi { |
|
|
|
} catch (IOException e) { |
|
|
|
return ResultBean.fireFail().setMessage(e.getMessage()); |
|
|
|
} |
|
|
|
Map<String,String> ajaxResult = new HashMap<>(); |
|
|
|
Map<String, String> ajaxResult = new HashMap<>(); |
|
|
|
ajaxResult.put("uuid", uuid); |
|
|
|
ajaxResult.put("img", Base64.encode(imgOutputStream.toByteArray())); |
|
|
|
return ResultBean.fireSuccess().setData(ajaxResult); |
|
|
@ -122,20 +134,30 @@ public class CaptchaRest implements CaptchaApi { |
|
|
|
|
|
|
|
@Override |
|
|
|
public ResultBean clickWord(HttpServletResponse httpServletResponse) { |
|
|
|
//=============优化从resources中获取
|
|
|
|
File targetFile = new File(imageLoginPath); |
|
|
|
if (!targetFile.exists()) { |
|
|
|
targetFile.mkdirs(); |
|
|
|
} |
|
|
|
int randomNum = RandomUtils.getRandomInt(1, 12); |
|
|
|
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("images/pic-click/bg" +randomNum+".png"); |
|
|
|
String path = imageLoginPath+"/bg"+randomNum+".png"; |
|
|
|
File file = new File(path); |
|
|
|
WordConvertUtils.inputStreamToFile(inputStream, file); |
|
|
|
//=============优化从resources中获取
|
|
|
|
String uuid = UUID.randomUUID().toString(); |
|
|
|
ICaptchaFactory captchaFactory = new CaptchaFactory(); |
|
|
|
AbstractCaptcha captcha = captchaFactory.getInstance(CaptchaType.CLICK_WORD); |
|
|
|
CaptchaBaseParam captchaBaseParam = new CaptchaBaseParam(); |
|
|
|
captchaBaseParam.setUrlOrPath(imageUtils.getClickWordBgPath()); |
|
|
|
// captchaBaseParam.setUrlOrPath(imageUtils.getClickWordBgPath());
|
|
|
|
captchaBaseParam.setUrlOrPath(path); |
|
|
|
captcha.setFontColorRandom(false); |
|
|
|
CaptchaBaseVO dataVO = captcha.create(captchaBaseParam); |
|
|
|
Map<String,Object> ajaxResult = new HashMap<>(); |
|
|
|
Map<String, Object> ajaxResult = new HashMap<>(); |
|
|
|
ajaxResult.put("dataVO", dataVO); |
|
|
|
Map map = JSON.parseObject(JSON.toJSONString(dataVO), Map.class); |
|
|
|
String wordList = map.get("wordList").toString(); |
|
|
|
String pointList = map.get("pointList").toString(); |
|
|
|
// List<String> lis = Arrays.asList(wordList.split(""));
|
|
|
|
// redisUtil.set(uuid, lis.get(0) + lis.get(1) + lis.get(2));
|
|
|
|
redisUtil.set(uuid, pointList); |
|
|
|
redisUtil.expire(uuid, captchaExpires); |
|
|
|
ajaxResult.put("uuid", uuid); |
|
|
|