如何在百里香中显示图案图像和随机字符串

时间:2020-10-20 06:12:50

标签: spring-boot spring-mvc thymeleaf captcha

借助于SpringBoot模块,我在控制器中为随机字符串和图像输出流编写了验证码实现。

我已经提到了一些链接enter link description here,以使用这些技术来实现。我尝试通过在类顶部创建一个RequestMapping("/captcha")而不扩展HttpServlet和我在图像源中调用的/ captcha请求,但也找不到方法。

还有其他方法吗?

这是我的源代码:

package com.wbl.ntcolympiad.web;

import com.wbl.ntcolympiad.util.CaptchaUtil;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.servlet.http.*;

import java.io.*;

public class CaptchaController extends HttpServlet {

    public static final String FILE_TYPE = "jpeg";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Max-Age", 0);

        String captchaStr = "";

        //captchaStr=Util.generateCaptchaTextMethod();

        captchaStr = CaptchaUtil.generateCaptchaTextMethod2(6);

        try {

            int width = 100;
            int height = 40;

            Color bg = new Color(0, 255, 255);
            Color fg = new Color(0, 100, 0);

            Font font = new Font("Arial", Font.BOLD, 20);
            BufferedImage cpimg = new BufferedImage(width, height, BufferedImage.OPAQUE);
            Graphics g = cpimg.createGraphics();

            g.setFont(font);
            g.setColor(bg);
            g.fillRect(0, 0, width, height);
            g.setColor(fg);
            g.drawString(captchaStr, 10, 25);

            HttpSession session = request.getSession(true);
            session.setAttribute("CAPTCHA", captchaStr);

            OutputStream outputStream = response.getOutputStream();

            ImageIO.write(cpimg, FILE_TYPE, outputStream);
            outputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

}

这是我的注册页面,我需要在胸腺叶中显示验证码图像和随机字符串

<form method="POST" th:action="@{/register}" th:object="${userAccountDTO}">
                        <div class="row">
                            <div class="form-group col-md-12 mb-4">
                                <input type="text" th:field="*{username}" class="form-control input-lg"
                                       id="username" aria-describedby="nameHelp"
                                       placeholder="Mobile Number"
                                       title="Enter 10 digit mobile number starts with 7 or 8 or 9"
                                       pattern="[7-9]{1}[0-9]{9}" required>

                                <p class="text-right" style="color: red; font-size: 13px"
                                   th:each="error: ${#fields.errors('username')}"
                                   th:text="${error}">Validation error</p>

                            </div>

                            <div class="form-group col-md-12 mb-4">
                                <input type="text" th:field="*{fullName}" class="form-control input-lg"
                                       id="name" aria-describedby="nameHelp"
                                       placeholder="Student Name" required>
                            </div>

                            <div class="form-group col-md-12 ">
                                <input type="password" th:field="*{password}" class="form-control input-lg"
                                       id="password"
                                       placeholder="Password" required>
                            </div>
                            <div class="form-group col-md-12 ">
                                <input type="password" th:field="*{confirmPassword}" class="form-control input-lg"
                                       id="cpassword"
                                       placeholder="Confirm Password" required>

                                <p class="text-right" style="color: red; font-size: 13px"
                                   th:each="error: ${#fields.errors('confirmPassword')}"
                                   th:text="${error}">Validation error</p>
                            </div>
                            <div>
                                <img id="captcha_id" name="imgCaptcha" th:src="@{captcha.jpg}">
                            </div>
                            <div>

                            </div>
                            <div class="col-md-12">
                                <div class="d-inline-block mr-3">
                                    <label class="control control-checkbox">
                                        <input type="checkbox" required/>
                                        <div class="control-indicator"></div>
                                        I Agree the terms and conditions
                                    </label>

                                </div>
                                <button type="submit" class="btn btn-lg btn-primary btn-block mb-4">Sign Up</button>
                                <p>Already have an account?
                                    <a class="text-blue" href="/login">Sign in</a>
                                </p>
                            </div>
                        </div>
                    </form>

我对此主题有任何想法或指示。

0 个答案:

没有答案