|
|
|
|
|
package com.nmgs.controller;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.captcha.LineCaptcha;
|
|
|
|
|
|
import cn.hutool.captcha.generator.RandomGenerator;
|
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
|
@RequestMapping(value = "login")
|
|
|
|
|
|
@Api(tags = "登录信息")
|
|
|
|
|
|
public class LoginController {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 登录页面获取验证码
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
@RequestMapping(value = "getCode",
|
|
|
|
|
|
method = {RequestMethod.POST}
|
|
|
|
|
|
)
|
|
|
|
|
|
@ApiOperation(httpMethod = "POST", value = "登录页面获取图片流验证码")
|
|
|
|
|
|
public ResponseEntity<byte[]> getCode() throws IOException {
|
|
|
|
|
|
//配置验证码图片的宽度,以及验证码字符的个数,还有它的模糊程度(也就是干扰线),这儿设置4个字符,1条干扰线
|
|
|
|
|
|
LineCaptcha captcha = new LineCaptcha(130, 40, 4, 1);
|
|
|
|
|
|
//配置只有纯数字 1到9
|
|
|
|
|
|
captcha.setGenerator(new RandomGenerator("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4));
|
|
|
|
|
|
//配置背景颜色
|
|
|
|
|
|
captcha.setBackground(Color.pink);
|
|
|
|
|
|
//配置字体
|
|
|
|
|
|
captcha.setFont(new Font("微软雅黑", Font.BOLD, 18));
|
|
|
|
|
|
//这儿就是生成验证码了
|
|
|
|
|
|
String captchaCode = captcha.getCode();
|
|
|
|
|
|
//其实,到这儿,验证码已配置完成,大家可以打印下 captchaCode 是可以出来4个字符的字符串的。
|
|
|
|
|
|
System.out.println("验证码==========>" + captchaCode);
|
|
|
|
|
|
//下面两行是生成图片的文件流
|
|
|
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
|
|
ImageIO.write(captcha.getImage(), "png", outputStream);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//在这中间还有一串的代码
|
|
|
|
|
|
//那就是还需要生成唯一的uuid,将来需要结合当前生成验证码结合起来
|
|
|
|
|
|
//把它们存到缓存中,这样,前端发送过后就可以拿来做比较
|
|
|
|
|
|
|
|
|
|
|
|
//这儿就是设置返回体以及返回的内容(最后返回的是一个图片的文件流,直接在浏览器访问看到是一个图片)
|
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
|
headers.setContentType(org.springframework.http.MediaType.IMAGE_PNG);
|
|
|
|
|
|
headers.set("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
|
|
|
|
|
|
headers.setPragma("no-cache");
|
|
|
|
|
|
headers.setDate("Expires", 0);
|
|
|
|
|
|
|
|
|
|
|
|
byte[] b = outputStream.toByteArray();
|
|
|
|
|
|
return new ResponseEntity<>(b, headers, HttpStatus.OK);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|