1.迁移项目部分代码时,使用caotcha启动报错,组件找不到,如下
17:24:58.549 WARN c.b.m.toolkit.TableInfoHelper Warn: Could not find @TableId in Class: com.biaopu.core.pojo.entity.ProductMenuSell.
17:24:58.549 WARN c.b.m.mapper.AutoSqlInjector class com.biaopu.core.pojo.entity.ProductMenuSell ,Not found @TableId annotation, Cannot use Mybatis-Plus 'xxById' Method.
17:24:58.904 WARN o.s.s.o.p.t.s.JwtAccessTokenConverter Unable to create an RSA verifier from verifierKey (ignoreable if using MAC)
17:24:59.056 WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'validateCodeController': Unsatisfied dependency expressed through field 'producer'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.google.code.kaptcha.Producer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
17:24:59.082 INFO c.a.n.s.c.a.c.NacosValueAnnotationBeanPostProcessor 21 was destroying!
17:24:59.082 INFO c.a.n.s.c.a.c.NacosValueAnnotationBeanPostProcessor class com.alibaba.nacos.spring.context.annotation.config.NacosValueAnnotationBeanPostProcessor was destroying!
17:24:59.082 INFO c.a.n.s.b.f.a.AnnotationNacosInjectedBeanPostProcessor class com.alibaba.nacos.spring.beans.factory.annotation.AnnotationNacosInjectedBeanPostProcessor was destroying!
17:24:59.082 INFO com.zaxxer.hikari.HikariDataSource HikariPool-1 - Shutdown initiated...
17:24:59.092 INFO com.zaxxer.hikari.HikariDataSource HikariPool-1 - Shutdown completed.
17:24:59.093 INFO o.a.catalina.core.StandardService Stopping service [Tomcat]
17:24:59.105 INFO o.s.b.a.l.ConditionEvaluationReportLoggingListener
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
17:24:59.128 ERROR o.s.b.d.LoggingFailureAnalysisReporter
***************************
APPLICATION FAILED TO START
***************************
Description:
Field producer in com.biaopu.base.controller.ValidateCodeController required a bean of type 'com.google.code.kaptcha.Producer' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.google.code.kaptcha.Producer' in your configuration.
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
Process finished with exit code 1
2.查看maven配置没问题
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
3.报错原因:
Consider defining a bean of type 'com.google.code.kaptcha.Producer' in your configuration
,缺少了Kaptcha
的配置Bean
。
经过检查代码,KaptchaConfig类没有复制过来。
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.biaopu.heaven.admin.common.config;
import com.biaopu.heaven.admin.common.constant.SecurityConstants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
/**
* @author lengleng
* @date 2017-12-21 21:12:18
*/
@Configuration
public class KaptchaConfig {
private static final String KAPTCHA_BORDER = "kaptcha.border";
private static final String KAPTCHA_TEXTPRODUCER_FONT_COLOR = "kaptcha.textproducer.font.color";
private static final String KAPTCHA_TEXTPRODUCER_CHAR_SPACE = "kaptcha.textproducer.char.space";
private static final String KAPTCHA_IMAGE_WIDTH = "kaptcha.image.width";
private static final String KAPTCHA_IMAGE_HEIGHT = "kaptcha.image.height";
private static final String KAPTCHA_TEXTPRODUCER_CHAR_LENGTH = "kaptcha.textproducer.char.length";
private static final Object KAPTCHA_IMAGE_FONT_SIZE = "kaptcha.textproducer.font.size";
@Bean
public DefaultKaptcha producer() {
Properties properties = new Properties();
properties.put(KAPTCHA_BORDER, SecurityConstants.DEFAULT_IMAGE_BORDER);
properties.put(KAPTCHA_TEXTPRODUCER_FONT_COLOR, SecurityConstants.DEFAULT_COLOR_FONT);
properties.put(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, SecurityConstants.DEFAULT_CHAR_SPACE);
properties.put(KAPTCHA_IMAGE_WIDTH, SecurityConstants.DEFAULT_IMAGE_WIDTH);
properties.put(KAPTCHA_IMAGE_HEIGHT, SecurityConstants.DEFAULT_IMAGE_HEIGHT);
properties.put(KAPTCHA_IMAGE_FONT_SIZE, SecurityConstants.DEFAULT_IMAGE_FONT_SIZE);
properties.put(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, SecurityConstants.DEFAULT_IMAGE_LENGTH);
Config config = new Config(properties);
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
以上是我项目中的配置,可以选择下面建立初始bean
@Configuration
public class KaptchaConfig {
@Bean
public DefaultKaptcha producer() {
Properties properties = new Properties();
properties.put("xxxx", "xxx");
...
Config config = new Config(properties);
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}