Commit 6b477c9b by xiaolang

图形验证码

parent 7ab2c292
...@@ -102,6 +102,12 @@ ...@@ -102,6 +102,12 @@
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
</dependency> </dependency>
<!-- 验证码 -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.asset.controller;
import com.asset.common.Common;
import com.asset.common.ResultUtil;
import com.asset.domain.Result;
import com.asset.domain.system.Captcha;
import com.asset.enums.CodeEnum;
import com.asset.service.system.CaptchaService;
import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* @author xiaol
* @date 2020/3/23
*/
@RestController
@RequestMapping(value = "/captcha")
public class CaptchaController {
@Autowired
CaptchaService captchaService;
@RequestMapping("/captcha")
public Result captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
String verCode = specCaptcha.text().toLowerCase();
String key = UUID.randomUUID().toString();
// 存入表并设置过期时间为2分钟
Captcha cap = new Captcha();
cap.setCaptcha_key(key);
cap.setCaptcha_value(specCaptcha.toBase64());
String ex = Common.getDateToString(Common.getTime() + 120);
cap.setExpire(ex);
return ResultUtil.success(CodeEnum.SUCCESS,cap);
}
}
package com.asset.domain.system;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("ex_captcha")
@ToString
public class Captcha {
private Integer id;
private String captcha_key;
private String captcha_value;
private String expire;
}
package com.asset.mapper.system;
import com.asset.domain.system.Captcha;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface CaptchaMapper extends BaseMapper<Captcha> {
}
package com.asset.service.impl.system;
import com.asset.domain.system.Captcha;
import com.asset.mapper.system.CaptchaMapper;
import com.asset.service.system.CaptchaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/*
* @author xiaol
* @date 2020/03/23
*/
@Service
@Transactional
public class CaptchaServiceImpl extends ServiceImpl<CaptchaMapper, Captcha> implements CaptchaService {
}
package com.asset.service.system;
import com.asset.domain.system.Captcha;
import com.baomidou.mybatisplus.extension.service.IService;
/*
* @author xiaol
* @date 2020/03/23
*/
public interface CaptchaService extends IService<Captcha> {
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.asset.mapper.system.CaptchaMapper">
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment