Java 我正在尝试使用存储在数据库中的用户名和密码创建自定义登录页。但我无法解决此错误
我是SpringSecurity的初学者,代码看起来不错,我认为缺少了一些东西 提升应用程序时,它将显示以下错误,但我还没有为appconfig创建beanJava 我正在尝试使用存储在数据库中的用户名和密码创建自定义登录页。但我无法解决此错误,java,spring,hibernate,spring-boot,spring-data,Java,Spring,Hibernate,Spring Boot,Spring Data,我是SpringSecurity的初学者,代码看起来不错,我认为缺少了一些东西 提升应用程序时,它将显示以下错误,但我还没有为appconfig创建bean . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) '
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2020-03-14 12:31:34.584 INFO 5148 --- [ main] com.access6.SecureDemoAppApplication : Starting SecureDemoAppApplication on DESKTOP-G23R8VK with PID 5148 (D:\workspace-spring-tool-suite-4-4.5.1.RELEASE\SecureDemoApp\target\classes started by Admin in D:\workspace-spring-tool-suite-4-4.5.1.RELEASE\SecureDemoApp)
2020-03-14 12:31:34.588 INFO 5148 --- [ main] com.access6.SecureDemoAppApplication : No active profile set, falling back to default profiles: default
2020-03-14 12:31:35.199 INFO 5148 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-03-14 12:31:35.255 INFO 5148 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 49ms. Found 1 JPA repository interfaces.
2020-03-14 12:31:35.717 INFO 5148 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-03-14 12:31:35.724 INFO 5148 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-03-14 12:31:35.724 INFO 5148 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-14 12:31:35.918 INFO 5148 --- [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2020-03-14 12:31:35.922 INFO 5148 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-03-14 12:31:35.922 INFO 5148 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1291 ms
2020-03-14 12:31:36.075 INFO 5148 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-03-14 12:31:36.133 INFO 5148 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.12.Final
2020-03-14 12:31:36.258 INFO 5148 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-03-14 12:31:36.451 INFO 5148 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-03-14 12:31:36.838 INFO 5148 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-03-14 12:31:36.854 INFO 5148 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2020-03-14 12:31:37.401 INFO 5148 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-03-14 12:31:37.408 INFO 5148 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-03-14 12:31:37.467 WARN 5148 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-03-14 12:31:37.710 WARN 5148 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appConfig': Unsatisfied dependency expressed through field 'UserDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsService': Unsatisfied dependency expressed through field 'reop'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepo': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.access6.User com.access6.UserRepo.findByUserName(java.lang.String)! No property userName found for type User!
2020-03-14 12:31:37.710 INFO 5148 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-03-14 12:31:37.713 INFO 5148 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-03-14 12:31:37.718 INFO 5148 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2020-03-14 12:31:37.720 INFO 5148 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-03-14 12:31:37.734 INFO 5148 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-14 12:31:37.743 ERROR 5148 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appConfig': Unsatisfied dependency expressed through field 'UserDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsService': Unsatisfied dependency expressed through field 'reop'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepo': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.access6.User com.access6.UserRepo.findByUserName(java.lang.String)! No property userName found for type User!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
这是我的密码:
配置类
package com.access6;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
@SuppressWarnings("deprecation")
@Configuration
@EnableWebSecurity
public class AppConfig extends WebSecurityConfigurerAdapter {
@Autowired
public UserDetailsService UserDetailsService;
@Bean
public AuthenticationProvider authpro() {
DaoAuthenticationProvider DAuthpro= new DaoAuthenticationProvider();
DAuthpro.setUserDetailsService(UserDetailsService);
DAuthpro.setPasswordEncoder(NoOpPasswordEncoder.getInstance());
return DAuthpro;
}
@Bean
public User u () {
return new User();
}
}
package com.access6;
import java.util.Collection;
import java.util.Collections;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
@Component
@SuppressWarnings("serial")
public class UserDetailsImpli implements UserDetails {
User user;
public UserDetailsImpli(User user) {
super();
this.user = user;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return Collections.singleton(new SimpleGrantedAuthority("USER"));
}
@Override
public String getPassword() {
// TODO Auto-generated method stub
return user.getPassword();
}
@Override
public String getUsername() {
// TODO Auto-generated method stub
return user.getName();
}
@Override
public boolean isAccountNonExpired() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isEnabled() {
// TODO Auto-generated method stub
return true;
}
}
控制器类
package com.access6;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class SecureContrlr {
@RequestMapping("/")
public ModelAndView weclome() {
return new ModelAndView("home");
}
}
模型
存储库
package com.access6;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Repository;
@Repository
@EnableJpaRepositories
public interface UserRepo extends JpaRepository<User, Long> {
User findByUserName(String UserName);
}
实现类
package com.access6;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
@SuppressWarnings("deprecation")
@Configuration
@EnableWebSecurity
public class AppConfig extends WebSecurityConfigurerAdapter {
@Autowired
public UserDetailsService UserDetailsService;
@Bean
public AuthenticationProvider authpro() {
DaoAuthenticationProvider DAuthpro= new DaoAuthenticationProvider();
DAuthpro.setUserDetailsService(UserDetailsService);
DAuthpro.setPasswordEncoder(NoOpPasswordEncoder.getInstance());
return DAuthpro;
}
@Bean
public User u () {
return new User();
}
}
package com.access6;
import java.util.Collection;
import java.util.Collections;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
@Component
@SuppressWarnings("serial")
public class UserDetailsImpli implements UserDetails {
User user;
public UserDetailsImpli(User user) {
super();
this.user = user;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return Collections.singleton(new SimpleGrantedAuthority("USER"));
}
@Override
public String getPassword() {
// TODO Auto-generated method stub
return user.getPassword();
}
@Override
public String getUsername() {
// TODO Auto-generated method stub
return user.getName();
}
@Override
public boolean isAccountNonExpired() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isEnabled() {
// TODO Auto-generated method stub
return true;
}
}
将存储库中的方法重命名为:
public interface UserRepo extends JpaRepository<User, Long> {
User findByName(String UserName);
}
您不需要在方法签名中写入实体名称。通过查找后,字段的名称应紧跟其后,即名称camelCase。您的自定义存储库方法错误。改为:
User findByName(String name);
正如例外情况告诉你的那样
No property userName found for type User!
findBy*仅在其名称与模型中的名称完全相同时才起作用
更新
首先,你可以用谷歌搜索“不满意的支出例外”的含义。现在你可以阅读信息了。这可能有点棘手,因为在本例中,可能存在嵌套执行,在一行中很难读取消息
让我们把它分解一下
这是一个开始,告诉您在创建UserDetailsServebean时出现了一个问题:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appConfig': Unsatisfied dependency expressed through field 'UserDetailsService';
但问题不在于服务,而在于存储库bean:
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsService': Unsatisfied dependency expressed through field 'reop';
在存储库bean中,由于某些原因,方法的创建失败:
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepo': Invocation of init method failed;
就是这样:spring尝试创建一个方法findByUserName,但用户模型没有名为userName的属性:
nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.access6.User com.access6.UserRepo.findByUserName(java.lang.String)! No property userName found for type User!
阅读这样一个例外,你总是可以滚动到最右边并阅读最后一句话。现在你应该知道该怎么做了。哇,只是一个小小的改变就帮我解决了这个错误。谢谢你的时间,这对我有帮助,你也能告诉我你是怎么读到这个错误的吗?我更新了我的答案,因为评论很简短:如果有帮助,请点击灰色旅行,接受答案。谢谢你让我大吃一惊!!,奇妙的解释。谢谢你花时间在这上面。你能通过这个链接吗?对不起,可以早点回答。。。希望有帮助!