Maison > Java > javaDidacticiel > Comment utiliser kaptcha pour implémenter le code de vérification dans SpringBoot

Comment utiliser kaptcha pour implémenter le code de vérification dans SpringBoot

王林
Libérer: 2023-05-11 22:55:14
avant
1714 Les gens l'ont consulté

Introduction liée à 1.kaptcha

Kaptcha est un projet open source de code de vérification basé sur SimpleCaptcha.

2. Solution d'intégration

①Configurer les dépendances

1

2

3

4

5

6

<!-- 验证码-->

<dependency>

 <groupId>com.github.penggle</groupId>

 <artifactId>kaptcha</artifactId>

 <version>2.3.2</version>

</dependency>

Copier après la connexion

②Configurer les paramètres liés au code de vérification Kaptcha dans pom Chargement de la configuration

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@Configuration

public class kaptchaConfig {

  @Bean(name="captchaProducer")

  public DefaultKaptcha getKaptchaBean(){

    DefaultKaptcha defaultKaptcha=new DefaultKaptcha();

    Properties properties=new Properties();

    properties.setProperty("kaptcha.border""yes");

    properties.setProperty("kaptcha.border.color""105,179,90");

    properties.setProperty("kaptcha.textproducer.font.color""blue");

    properties.setProperty("kaptcha.image.width""125");

    properties.setProperty("kaptcha.image.height""45");

    properties.setProperty("kaptcha.session.key""code");

    properties.setProperty("kaptcha.textproducer.char.length""4");

    properties.setProperty("kaptcha.textproducer.font.names""宋体,楷体,微软雅黑");

    Config config=new Config(properties);

    defaultKaptcha.setConfig(config);

    return defaultKaptcha;

  }

}

Copier après la connexion

Les deux méthodes de configuration sont disponibles dans springboot ;

③KaptchaController

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">

    <property name="config">

      <bean class="com.google.code.kaptcha.util.Config">

        <constructor-arg type="java.util.Properties">

          <props>

            <prop key = "kaptcha.border ">yes</prop>

            <prop key="kaptcha.border.color">105,179,90</prop>

            <prop key="kaptcha.textproducer.font.color">blue</prop>

            <prop key="kaptcha.image.width">100</prop>

            <prop key="kaptcha.image.height">50</prop>

            <prop key="kaptcha.textproducer.font.size">27</prop>

            <prop key="kaptcha.session.key">code</prop>

            <prop key="kaptcha.textproducer.char.length">4</prop>

            <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>

            <prop key="kaptcha.textproducer.char.string">23456789ABCEFGHJKMNOPQRSTUVWXYZ</prop>

            <prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.WaterRipple</prop>

            <prop key="kaptcha.noise.color">black</prop>

            <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>

            <!--<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.DefaultNoise</prop>-->

            <prop key="kaptcha.background.clear.from">185,56,213</prop>

            <prop key="kaptcha.background.clear.to">white</prop>

            <prop key="kaptcha.textproducer.char.space">3</prop>

          </props>

        </constructor-arg>

      </bean>

    </property>

  </bean>

</beans>

Copier après la connexion
.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal