CaptchaConfig.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package org.dromara.auth.config;
  2. import cn.hutool.captcha.CaptchaUtil;
  3. import cn.hutool.captcha.CircleCaptcha;
  4. import cn.hutool.captcha.LineCaptcha;
  5. import cn.hutool.captcha.ShearCaptcha;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.context.annotation.Lazy;
  9. import java.awt.*;
  10. /**
  11. * 验证码配置
  12. *
  13. * @author Lion Li
  14. */
  15. @Configuration
  16. public class CaptchaConfig {
  17. private static final int WIDTH = 160;
  18. private static final int HEIGHT = 60;
  19. private static final Color BACKGROUND = Color.LIGHT_GRAY;
  20. private static final Font FONT = new Font("Arial", Font.BOLD, 48);
  21. /**
  22. * 圆圈干扰验证码
  23. */
  24. @Lazy
  25. @Bean
  26. public CircleCaptcha circleCaptcha() {
  27. CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(WIDTH, HEIGHT);
  28. captcha.setBackground(BACKGROUND);
  29. captcha.setFont(FONT);
  30. return captcha;
  31. }
  32. /**
  33. * 线段干扰的验证码
  34. */
  35. @Lazy
  36. @Bean
  37. public LineCaptcha lineCaptcha() {
  38. LineCaptcha captcha = CaptchaUtil.createLineCaptcha(WIDTH, HEIGHT);
  39. captcha.setBackground(BACKGROUND);
  40. captcha.setFont(FONT);
  41. return captcha;
  42. }
  43. /**
  44. * 扭曲干扰验证码
  45. */
  46. @Lazy
  47. @Bean
  48. public ShearCaptcha shearCaptcha() {
  49. ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(WIDTH, HEIGHT);
  50. captcha.setBackground(BACKGROUND);
  51. captcha.setFont(FONT);
  52. return captcha;
  53. }
  54. }