SpringSecurity中怎么实现登录添加验证码
创始人
2024-12-23 11:42:20
0次

准备验证码

要有验证码,首先得先准备好验证码,本文采用 Java 自画的验证码,代码如下:

/** * 生成验证码的工具类 */public class VerifyCode {private int width = 100;// 生成验证码图片的宽度private int height = 50;// 生成验证码图片的高度private String[] fontNames = { "宋体", "楷体", "隶书", "微软雅黑" };private Color bgColor = new Color(255, 255, 255);// 定义验证码图片的背景颜色为白色private Random random = new Random();private String codes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";private String text;// 记录随机字符串/** * 获取一个随意颜色 *  * @return */private Color randomColor() {int red = random.nextInt(150);int green = random.nextInt(150);int blue = random.nextInt(150);return new Color(red, green, blue);}/** * 获取一个随机字体 *  * @return */private Font randomFont() {String name = fontNames[random.nextInt(fontNames.length)];int style = random.nextInt(4);int size = random.nextInt(5) + 24;return new Font(name, style, size);}/** * 获取一个随机字符 *  * @return */private char randomChar() {return codes.charAt(random.nextInt(codes.length()));}/** * 创建一个空白的BufferedImage对象 *  * @return */private BufferedImage createImage() {BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g2 = (Graphics2D) image.getGraphics();g2.setColor(bgColor);// 设置验证码图片的背景颜色g2.fillRect(0, 0, width, height);return image;}public BufferedImage getImage() {BufferedImage image = createImage();Graphics2D g2 = (Graphics2D) image.getGraphics();StringBuffer sb = new StringBuffer();for (int i = 0; i < 4; i++) {String s = randomChar() + "";sb.append(s);g2.setColor(randomColor());g2.setFont(randomFont());float x = i * width * 1.0f / 4;g2.drawString(s, x, height - 15);}this.text = sb.toString();drawLine(image);return image;}/** * 绘制干扰线 *  * @param image */private void drawLine(BufferedImage image) {Graphics2D g2 = (Graphics2D) image.getGraphics();int num = 5;for (int i = 0; i < num; i++) {int x1 = random.nextInt(width);int y1 = random.nextInt(height);int x2 = random.nextInt(width);int y2 = random.nextInt(height);g2.setColor(randomColor());g2.setStroke(new BasicStroke(1.5f));g2.drawLine(x1, y1, x2, y2);}}public String getText() {return text;}public static void output(BufferedImage image, OutputStream out) throws IOException {ImageIO.write(image, "JPEG", out);}}

这个工具类很常见,网上也有很多,就是画一个简单的验证码,通过流将验证码写到前端页面,提供验证码的 Controller 如下:

@RestControllerpublic class VerifyCodeController {  @GetMapping("/vercode")  public void code(HttpServletRequest req, HttpServletResponse resp) throws IOException {    VerifyCode vc = new VerifyCode();    BufferedImage image = vc.getImage();    String text = vc.getText();    HttpSession session = req.getSession();    session.setAttribute("index_code", text);    VerifyCode.output(image, resp.getOutputStream());  }}

这里创建了一个 VerifyCode 对象,将生成的验证码字符保存到 session 中,然后通过流将图片写到前端,img标签如下:

展示效果如下:

自定义过滤器

在登陆页展示验证码这个就不需要我多说了,接下来我们来看看如何自定义验证码处理器:

@Componentpublic class VerifyCodeFilter extends GenericFilterBean {  private String defaultFilterProcessUrl = "/doLogin";  @Override  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)      throws IOException, ServletException {    HttpServletRequest request = (HttpServletRequest) req;    HttpServletResponse response = (HttpServletResponse) res;    if ("POST".equalsIgnoreCase(request.getMethod()) && defaultFilterProcessUrl.equals(request.getServletPath())) {      // 验证码验证      String requestCaptcha = request.getParameter("code");      String genCaptcha = (String) request.getSession().getAttribute("index_code");      if (StringUtils.isEmpty(requestCaptcha))        throw new AuthenticationServiceException("验证码不能为空!");      if (!genCaptcha.toLowerCase().equals(requestCaptcha.toLowerCase())) {        throw new AuthenticationServiceException("验证码错误!");      }    }    chain.doFilter(request, response);  }}

自定义过滤器继承自 GenericFilterBean ,并实现其中的 doFilter 方法,在 doFilter 方法中,当请求方法是 POST ,并且请求地址是 /doLogin 时,获取参数中的 code 字段值,该字段保存了用户从前端页面传来的验证码,然后获取 session 中保存的验证码,如果用户没有传来验证码,则抛出验证码不能为空异常,如果用户传入了验证码,则判断验证码是否正确,如果不正确则抛出异常,否则执行 chain.doFilter(request, response); 使请求继续向下走。

配置

最后在 Spring Security 的配置中,配置过滤器,如下:

@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter {  @Autowired  VerifyCodeFilter verifyCodeFilter;  ...  ...  @Override  protected void configure(HttpSecurity http) throws Exception {    http.addFilterBefore(verifyCodeFilter, UsernamePasswordAuthenticationFilter.class);    http.authorizeRequests()        .antMatchers("/admin/**").hasRole("admin")        ...        ...        .permitAll()        .and()        .csrf().disable();  }}

这里只贴出了部分核心代码,即 http.addFilterBefore(verifyCodeFilter, UsernamePasswordAuthenticationFilter.class); ,如此之后,整个配置就算完成了。

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...