gpt4 book ai didi

java - 创建名称为 'securityConfig' 的 bean 时出错, Autowiring 依赖项注入(inject)失败;异常 java.lang.IllegalArgumentException

转载 作者:行者123 更新时间:2023-11-29 08:26:35 25 4
gpt4 key购买 nike

我正在做图书馆管理系统的 spring boot 安全应用。当我运行我的应用程序时,出现上述错误。有人可以帮帮我吗?提前致谢!

//安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;

@Autowired
private DataSource dataSource;
@Value("${spring.queries.users-query}")
private String usersQuery;
@Value("${spring.queries.roles-query}")
private String rolesQuery;
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.
jdbcAuthentication()
.usersByUsernameQuery(usersQuery)
.authoritiesByUsernameQuery(rolesQuery)
.dataSource(dataSource)
.passwordEncoder(bCryptPasswordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {

http.
authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/registration").permitAll()
.antMatchers("/admin/**").hasAuthority("ADMIN").anyRequest()
.authenticated().and().csrf().disable().formLogin()
.loginPage("/login").failureUrl("/login?error=true")
.defaultSuccessUrl("/admin/home")
.usernameParameter("email")
.passwordParameter("password")
.and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/").and().exceptionHandling()
.accessDeniedPage("/access-denied");
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**");
}
}

//服务实现

@Service
public class StudentServiceImpl implements StudentService {
private StudentRepository stuRepository;
public StudentServiceImpl(StudentRepository stuRepository) {
this.stuRepository=stuRepository;
}
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Override
public Student findStudentByEmail(String email) {
return stuRepository.findByEmail(email);
}
@Override
public void saveStudent(Student stu) {
stu.setPassword(bCryptPasswordEncoder.encode(stu.getPassword()));
stuRepository.save(stu);
}
}

//错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.queries.users-query' in value "${spring.queries.users-query}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:378) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]

//如果我在属性文件中添加查询。我收到这个错误

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'stuserv'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentServiceImpl' defined in file [C:\Users\Root\eclipse-workspace\manlib\target\classes\com\itcinfo\manlib\service\StudentServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentRepository': Post-processing of merged bean definition failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType;
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]

最佳答案

如果您阅读错误消息,您可以看到

Could not resolve placeholder 'spring.queries.users-query' in value "${spring.queries.users-query}"

您在属性文件 spring.queries.users-query 中遗漏了一个属性添加它,错误应该消失

更新:

如果您阅读了新的错误消息:

java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType;

您可以看到一个异常,显示不兼容的 jar 文件

关于java - 创建名称为 'securityConfig' 的 bean 时出错, Autowiring 依赖项注入(inject)失败;异常 java.lang.IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52252698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com