登录页面通常会有验证码验证信息:
这时候用到
CaptchaUtil 图形验证码工具类:::::
package cn.hutool.captcha;
/**
* 图形验证码工具
*
* @author looly
* @since 3.1.2
*/
public class CaptchaUtil {
/**
* 创建线干扰的验证码,默认5位验证码,150条干扰线
*
* @param width 图片宽
* @param height 图片高
* @return {@link LineCaptcha}
*/
public static LineCaptcha createLineCaptcha(int width, int height) {
return new LineCaptcha(width, height);
}
/**
* 创建线干扰的验证码
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param lineCount 干扰线条数
* @return {@link LineCaptcha}
*/
public static LineCaptcha createLineCaptcha(int width, int height, int codeCount, int lineCount) {
return new LineCaptcha(width, height, codeCount, lineCount);
}
/**
* 创建圆圈干扰的验证码,默认5位验证码,15个干扰圈
*
* @param width 图片宽
* @param height 图片高
* @return {@link CircleCaptcha}
* @since 3.2.3
*/
public static CircleCaptcha createCircleCaptcha(int width, int height) {
return new CircleCaptcha(width, height);
}
/**
* 创建圆圈干扰的验证码
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param circleCount 干扰圆圈条数
* @return {@link CircleCaptcha}
* @since 3.2.3
*/
public static CircleCaptcha createCircleCaptcha(int width, int height, int codeCount, int circleCount) {
return new CircleCaptcha(width, height, codeCount, circleCount);
}
/**
* 创建扭曲干扰的验证码,默认5位验证码
*
* @param width 图片宽
* @param height 图片高
* @return {@link ShearCaptcha}
* @since 3.2.3
*/
public static ShearCaptcha createShearCaptcha(int width, int height) {
return new ShearCaptcha(width, height);
}
/**
* 创建扭曲干扰的验证码,默认5位验证码
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param thickness 干扰线宽度
* @return {@link ShearCaptcha}
* @since 3.3.0
*/
public static ShearCaptcha createShearCaptcha(int width, int height, int codeCount, int thickness) {
return new ShearCaptcha(width, height, codeCount, thickness);
}
}
package cn.yunmark.login.controller;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.date.DateUtil;
import cn.yunmark.aop.logging.annotation.ApiLog;
import cn.yunmark.common.redis.RedisUtils;
import cn.yunmark.common.utils.ResponseUtil;
import cn.yunmark.core.errors.ErrorConstants;
import cn.yunmark.examdetail.service.ExamDetailService;
import cn.yunmark.examdetail.service.dto.ExamDetailDTO;
import cn.yunmark.grade.service.StudentGradeService;
import cn.yunmark.grade.service.dto.StudentGradeDTO;
import cn.yunmark.grade.service.dto.StudentInfoExcelDTO;
import cn.yunmark.login.model.LoginDTO;
import cn.yunmark.login.model.LoginVo;
import cn.yunmark.security.jwt.TokenProvider;
import cn.yunmark.security.util.SecurityUtils;
import cn.yunmark.sys.constant.enums.LogTypeEnum;
import cn.yunmark.sys.model.LoginForm;
import cn.yunmark.sys.service.dto.UserDTO;
import cn.yunmark.sys.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 学生登录Controller
* @author yunmark
* @version @2022/3/25 22:01
*/
@Api(tags ="学生成绩信息")
@Slf4j
@RestController
@RequestMapping(value = "/zsb/student")
public class StudentLoginController {
@Autowired
private StudentGradeService studentGradeService;
@Autowired
private ExamDetailService examDetailService;
/**
* 用户登录
* @param loginForm
* @param session
* @return
*/
@PostMapping("check")
@ApiLog(value = "用户校验", type = LogTypeEnum.LOGIN)
@ApiOperation("登录接口")
public Map login(@RequestBody LoginDTO loginForm,String codeSign) {
Map res = new HashMap<>();
String sfzh = loginForm.getSfzh();
String code = loginForm.getCode();
String ksh = loginForm.getKsh();
RedisUtils utils = RedisUtils.getInstance();
String rediscode = (String) utils.get(codeSign);
if (!code.equals(rediscode) && null != code) {
res.put("code", 500);
res.put("msg", "考生信息有误,请确认后重新输入");
return res;
}
String nf = DateUtil.format(new Date(), "yyyy");
StudentGradeDTO studentGradeDTO = studentGradeService.selectOneStd(ksh, sfzh, nf);
if (studentGradeDTO == null) {
res.put("code", 500);
res.put("msg", "身份信息有误");
return res;
}
//获取有效考试须知
ExamDetailDTO examDetailDTO = examDetailService.getByStatus(nf);
if (examDetailDTO == null) {
examDetailDTO = examDetailService.getDefault();
}
LoginVo loginVo = new LoginVo();
loginVo.setExamDetailDTO(examDetailDTO);
loginVo.setStudentGradeDTO(studentGradeDTO);
res.put("code", 200);
res.put("msg", "校验成功");
res.put("data", loginVo);
return res;
}
/**
* 获取登陆验证码
* @param response
* @param session
* @throws
*/
@ApiOperation ("获取验证码")
@ApiLog("获取验证码")
@GetMapping("getCode")
public void getCode(HttpServletResponse response, HttpSession session){
//HuTool定义图形验证码的长和宽,验证码的位数,干扰线的条数
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,50);
//将验证码放入session
session.setAttribute("code",lineCaptcha.getCode());
try {
ServletOutputStream outputStream = response.getOutputStream();
lineCaptcha.write(outputStream);
outputStream.close();
} catch (IOException e) {
log.error ("{}", e );
}
}
}