开发者社区> 问答> 正文

我正在尝试使用存储在数据库中的用户名和密码创建自定义登录页面,但无法解决此错误

我是Spring Security的初学者,代码看起来不错,而且我认为缺少某些内容。

提升应用程序时,它将显示以下错误,但是我无处为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 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 javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class User {
    @Id
    private int id;
    private String name;
    private String password;

    //getters and setters
}

资料库

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 javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Service
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {
    @Autowired
    private UserRepo reop;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user=reop.findByUserName(username);
        if(user==null) {
            throw new UsernameNotFoundException("404 user");

        }
        return new  UserDetailsImpli(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;
    }
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-26 12:24:27 692 0
1 条回答
写回答
取消 提交回答
  • 将存储库中的方法重命名为:

    public interface UserRepo extends JpaRepository<User, Long> {
        User findByName(String UserName);
    }
    

    您无需在方法签名中写入实体名称。之后findBy的字段的名称应遵循,这是name(驼峰)。

    回答来源:Stack Overflow

    2020-03-26 12:24:51
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载