Spring Boot整合Security系列步骤及问题排查(六)—— 自定义图形验证码属性可配置

1.新建图形验证码属性类:

/**
 * 图形验证码属性配置
 *
 * @author zhaohaibin
 */
@Data
public class ImageCodeProperties {

    private int width = 68;
    private int height = 32;
    private int length = 4;
    /**
     * 有效期
     */
    private int expireIn = 60;

}

2.验证码基础类(方便扩展短信验证码):

/**
 * 验证码属性配置基础类
 *
 * @author zhaohaibin
 */
@Data
public class ValidateCodeProperties {

    private ImageCodeProperties image = new ImageCodeProperties();

}

3.更新SecurityProperties:

/**
 * security基础属性配置类
 * @author zhaohaibin
 */
@Data
@ConfigurationProperties(prefix = "demo.security")
public class SecurityProperties {

    private BrowserProperties browser = new BrowserProperties();

    /**
     * 验证码配置
     */
    private ValidateCodeProperties code = new ValidateCodeProperties();

}

4.更新ValidateCodeController:


@Autowired
private SecurityProperties securityProperties;
    
/**
 * 生成图形验证码
 *
 * @param request
 * @return
 */
private ImageCode createImageCode(HttpServletRequest request) {

        // 请求配置覆盖自定义配置覆盖默认配置
        int width = ServletRequestUtils.getIntParameter(request, "width", securityProperties.getCode().getImage().getWidth());
        int height = ServletRequestUtils.getIntParameter(request, "height", securityProperties.getCode().getImage().getHeight());

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics g = image.getGraphics();

        Random random = new Random();

        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        // 字体大小
        g.setFont(new Font("Times New Roman", Font.ITALIC, 24));
        g.setColor(getRandColor(160, 200));

        // 随机线条        
        for (int i = 0; i < 155; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x, y, x + xl, y + yl);
        }

        String sRand = "";

        // 根据配置长度生成随机数
        for (int i = 0; i < securityProperties.getCode().getImage().getLength(); i++) {
            String rand = String.valueOf(random.nextInt(10));
            sRand += rand;
            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            
            // 设置左、上边距
            g.drawString(rand, 13 * i + 6, 26);
        }

        g.dispose();

        return new ImageCode(image, sRand, 60);

    }

4.配置启动,覆盖顺序->请求配置覆盖自定义配置覆盖默认配置:

# security 默认登录页面配置
demo:
  security:
    browser:
      loginPage: "/demoLogin.html"
#      loginType: "REDIRECT"
    code:
      image:
        # 图形验证码长度
        length: 6
        # 图形验证码图形宽
        width: 100
    <div><label>验证码</label><input type="text" name="imageCode" placeholder="请输入验证码"/><img src="code/image?width=200" alt=""></div>

问题排查:
暂无

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值