1. Verification code generation class:

import java.util.Random;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

/** * Captcha generator class, can generate numbers, uppercase, lowercase letters and a mixture of captcha types. Supports the number of user-defined verification code characters. Support custom captcha image size; You can customize special characters to be excluded. * Support custom number of interference lines; Supports custom verification code colors */
public class ValidateCode {

    /** * The verification code type is 0 to 9 */
    public static final int TYPE_NUM_ONLY = 0;

    /** * The verification code type is a combination of uppercase and lowercase letters */
    public static final int TYPE_LETTER_ONLY = 1;

    /** * The verification code type is a combination of digits, uppercase letters, and lowercase letters */
    public static final int TYPE_ALL_MIXED = 2;

    /** * The verification code type is a combination of digits and uppercase letters */
    public static final int TYPE_NUM_UPPER = 3;

    /** * The verification code type is a mixture of digits and lowercase letters */
    public static final int TYPE_NUM_LOWER = 4;

    /** * The verification code type is uppercase */
    public static final int TYPE_UPPER_ONLY = 5;

    /** * The verification code type is only lowercase */
    public static final int TYPE_LOWER_ONLY = 6;

    private ValidateCode() {}/** * Generates the verification code string ** @paramType * Verification code type, see static attribute * of this class @paramLength * Indicates the length of the verification code. The value is an integer greater than 0 * @paramExChars * Specifies the special characters to be excluded. This parameter is valid only for the alphanumerical and numeric verification codes @returnVerification code The character string is */
    public static String generateTextCode(int type, int length, String exChars) {

        if (length <= 0)
            return "";

        StringBuffer code = new StringBuffer();
        int i = 0;
        Random r = new Random();

        switch (type) {

        / / only Numbers
        case TYPE_NUM_ONLY:
            while (i < length) {
                int t = r.nextInt(10);
                if (exChars == null || exChars.indexOf(t + "") < 0) {// Exclude special characterscode.append(t); i++; }}break;

        // Only letters (uppercase, lowercase)
        case TYPE_LETTER_ONLY:
            while (i < length) {
                int t = r.nextInt(123);
                if ((t >= 97 || (t >= 65 && t <= 90)) && (exChars == null || exChars.indexOf((char) t) < 0)) {
                    code.append((char) t); i++; }}break;

        // A combination of digits, uppercase letters, and lowercase letters
        case TYPE_ALL_MIXED:
            while (i < length) {
                int t = r.nextInt(123);
                if ((t >= 97 || (t >= 65 && t <= 90) || (t >= 48 && t <= 57))
                        && (exChars == null || exChars.indexOf((char) t) < 0)) {
                    code.append((char) t); i++; }}break;

        // Mix numbers and uppercase letters
        case TYPE_NUM_UPPER:
            while (i < length) {
                int t = r.nextInt(91);
                if ((t >= 65 || (t >= 48 && t <= 57)) && (exChars == null || exChars.indexOf((char) t) < 0)) {
                    code.append((char) t); i++; }}break;

        // A combination of digits and lowercase letters
        case TYPE_NUM_LOWER:
            while (i < length) {
                int t = r.nextInt(123);
                if ((t >= 97 || (t >= 48 && t <= 57)) && (exChars == null || exChars.indexOf((char) t) < 0)) {
                    code.append((char) t); i++; }}break;

        // Uppercase only
        case TYPE_UPPER_ONLY:
            while (i < length) {
                int t = r.nextInt(91);
                if ((t >= 65) && (exChars == null || exChars.indexOf((char) t) < 0)) {
                    code.append((char) t); i++; }}break;

        // Only lowercase letters
        case TYPE_LOWER_ONLY:
            while (i < length) {
                int t = r.nextInt(123);
                if ((t >= 97) && (exChars == null || exChars.indexOf((char) t) < 0)) {
                    code.append((char) t); i++; }}break;

        }

        return code.toString();
    }

    /** * A captcha is generated ** @paramTextCode * Text verification code * @paramWidth * Image width * @paramHeight * Image height * @paramInterLine * The number of interfering lines in a picture * @paramRandomLocation * Whether the height of each character is random * @paramBackColor * Image color, if null, random color * @paramForeColor * Specifies the font color. If the value is null, the random color * is used @paramLineColor * interference lineColor, if null, random color * @returnImage cache object */
    public static BufferedImage generateImageCode(String textCode, int width, int height, int interLine,
            boolean randomLocation, Color backColor, Color foreColor, Color lineColor) {

        BufferedImage bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = bim.getGraphics();

        // Draw the background image
        g.setColor(backColor == null ? getRandomColor() : backColor);
        g.fillRect(0.0, width, height);

        // Draw interference lines
        Random r = new Random();
        if (interLine > 0) {

            int x = 0, y = 0, x1 = width, y1 = 0;
            for (int i = 0; i < interLine; i++) {
                g.setColor(lineColor == null? getRandomColor() : lineColor); y = r.nextInt(height); y1 = r.nextInt(height); g.drawLine(x, y, x1, y1); }}// Write the captcha

        // g.setColor(getRandomColor());
        // g.setColor(isSimpleColor? Color.BLACK:Color.WHITE);

        // The font size is 80% of the height of the image
        int fsize = (int) (height * 0.8);
        int fx = height - fsize;
        int fy = fsize;

        g.setFont(new Font("Default", Font.PLAIN, fsize));

        // Write the captcha character
        for (int i = 0; i < textCode.length(); i++) {
            fy = randomLocation ? (int) ((Math.random() * 0.3 + 0.6) * height) : fy;// Whether each character height is random
            g.setColor(foreColor == null ? getRandomColor() : foreColor);
            g.drawString(textCode.charAt(i) + "", fx, fy);
            fx += fsize * 0.9;
        }

        g.dispose();

        return bim;
    }

    /** * Generate image captcha ** @paramType * Verification code type, see static attribute * of this class @paramLength * Indicates the length of the verification code. The value is an integer greater than 0 * @paramExChars * Special characters to be excluded * @paramWidth * Image width * @paramHeight * Image height * @paramInterLine * The number of interfering lines in a picture * @paramRandomLocation * Whether the height of each character is random * @paramBackColor * Image color, if null, random color * @paramForeColor * Specifies the font color. If the value is null, the random color * is used @paramLineColor * interference lineColor, if null, random color * @returnImage cache object */
    public static BufferedImage generateImageCode(int type, int length, String exChars, int width, int height,
            int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor) {

        String textCode = generateTextCode(type, length, exChars);
        BufferedImage bim = generateImageCode(textCode, width, height, interLine, randomLocation, backColor, foreColor,
                lineColor);

        return bim;
    }

    /** * produces a random color ** @return* /
    private static Color getRandomColor() {
        Random r = new Random();
        Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
        returnc; }}Copy the code

2, the Controller

 /** * Generates a verification code * @param request
     * @param response
     * @throws IOException
     * @ValidateCodeGenerateTextCode (Verification code character type, verification code length, special characters to be excluded) * @ValidateCodeGenerateImageCode (text verification code, image width, image height, number of interference lines, random character height, image color, font color, interference line color) */

    @RequestMapping(value = "validateCode")
    public void validateCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Cache-Control"."no-cache");
        String verifyCode = ValidateCode.generateTextCode(ValidateCode.TYPE_NUM_LOWER, 4.null);
        request.getSession().setAttribute("validateCode", verifyCode);
        response.setContentType("image/jpeg");
        BufferedImage bim = ValidateCode.generateImageCode(verifyCode, 90.30.5.true, Color.WHITE, Color.BLUE, null);
        ImageIO.write(bim, "JPEG", response.getOutputStream());
    }
 /** * Login request * @param* /
    @RequestMapping(value = "login", method = RequestMethod.POST, produces = "text/html; charset=utf-8")
    public String login(HttpServletRequest request, HttpServletResponse response, UserEntity user) {
        // Verify the verification code first
        Session session = SecurityUtils.getSubject().getSession();
        String code = (String) session.getAttribute("validateCode");
        String submitCode = WebUtils.getCleanParam(request, "validateCode");
        if(StringUtils.isEmpty(submitCode) || ! StringUtils.equals(code,submitCode.toLowerCase())) { request.setAttribute("LOGIN_ERROR_CODE", LoginConstant.LOGIN_ERROR_CODE_100000);
            request.setAttribute("LOGIN_ERROR_MESSAGE", LoginConstant.LOGIN_ERROR_MESSAGE_VALIDATECODE);
            return "login";
        }
        // Want to get the securityutils.getSubject () object.. The access address must be within Shiro's blocking address. No and then a null pointer is reported
        Subject sub = SecurityUtils.getSubject();
        // The username and password entered by the user are stored in the UsernamePasswordToken object.. And then compared by Shiro's internal certification,
        // The authentication executor is sent to doGetAuthenticationInfo in ShiroDbRealm
        If the preceding authentication succeeds, the system moves forward. If the authentication fails, an exception is thrown
        UsernamePasswordToken token = new UsernamePasswordToken(user.getAccountName(), user.getPassWord());
        try {
            sub.login(token);
        } catch (LockedAccountException lae) {
            token.clear();
            request.setAttribute("LOGIN_ERROR_CODE", LoginConstant.LOGIN_ERROR_CODE_100002);
            request.setAttribute("LOGIN_ERROR_MESSAGE", LoginConstant.LOGIN_ERROR_MESSAGE_SYSTEMERROR);
            return "login";
        } catch (ExcessiveAttemptsException e) {
            token.clear();
            request.setAttribute("LOGIN_ERROR_CODE", LoginConstant.LOGIN_ERROR_CODE_100003);
            request.setAttribute("LOGIN_ERROR_MESSAGE"."Account number:" + user.getUserName() + LoginConstant.LOGIN_ERROR_MESSAGE_MAXERROR);
            return "login";
        } catch (AuthenticationException e) {
            token.clear();
            request.setAttribute("LOGIN_ERROR_CODE", LoginConstant.LOGIN_ERROR_CODE_100001);
            request.setAttribute("LOGIN_ERROR_MESSAGE", LoginConstant.LOGIN_ERROR_MESSAGE_USERERROR);
            return "login";
        }
        return "redirect:/index.shtml";
    }Copy the code

Note: Some parameters in the login method are defined:

public interface LoginConstant
{
    String LOGIN_ERROR_CODE_100000 = "100000";
    String LOGIN_ERROR_MESSAGE_VALIDATECODE = "Verification code input error, please re-enter!";

    String LOGIN_ERROR_CODE_100001 = "100001";
    String LOGIN_ERROR_MESSAGE_USERERROR = "Wrong account or password, please re-enter!";

    String LOGIN_ERROR_CODE_100002 = "100002";
    String LOGIN_ERROR_MESSAGE_SYSTEMERROR = "User has been locked and cannot log in, please contact the administrator!";

    String LOGIN_ERROR_CODE_100003 = "100003";
    String LOGIN_ERROR_MESSAGE_MAXERROR = "Too many login failures, locked for 10 minutes!";

    String LOGIN_ERROR_CODE_100004 = "100004";
    String LOGIN_ERROR_MESSAGE_FORCELOGOUT = "You have been forcibly logged out by your administrator. Please log in again.";

}Copy the code

3, login JSP (important code) path information:

The < %String path = request.getContextPath();
String basePath = request.getScheme()+": / /"+request.getServerName()+":"+request.getServerPort()+path; % >Copy the code

Js: Used to replace the verification code image

    <script>
        function reloadValidateCode(a){
            $("#validateCodeImg").attr("src"."<%=basePath%>/validateCode.shtml? data=" + new Date() + Math.floor(Math.random()*24));
        }
    </script>Copy the code

Tags in the login form:

<img id="validateCodeImg" src="<%=basePath%>/validateCode.shtml" />&nbsp; &nbsp;<a href="#" onclick="javascript:reloadValidateCode();">Can't see?</a>Copy the code

4. Shiro anonymous access configuration (no captcha image generation without this configuration)

<! -- Custom filterChainDefinitionMap -->
    <bean id="chainDefinitionSectionMetaSource" class="com.collection.shiro.ChainDefinitionSectionMetaSource">
        <property name="filterChainDefinitions">
            <value>// add this line</value>
        </property>
    </bean>Copy the code

Effect: