spring-boot - 在 Spring Boot 中创建自定义安全配置时出现错误
问题描述
ConfigServletWebServerApplicationContext:上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“securityConfig”的bean时出错:通过字段“readerRepository”表示不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“readerRepository”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException: Not a managed type: class java.io.Reader
package com.example.readinglist;
import java.io.Reader;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Repository;
@Repository
public interface ReaderRepository extends JpaRepository<Reader, String> {
UserDetails findOne(String username);
}
阅读清单应用:
package com.example.readinglist;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ReadinglistApplication {
public static void main(String[] args) {
SpringApplication.run(ReadinglistApplication.class, args);
}
}
安全配置文件:
package com.example.readinglist;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private ReaderRepository readerRepository;
@Override
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.antMatchers("/")
.access("hasRole('Reader')")
.antMatchers("/**").permitAll().and().formLogin().loginPage("/login")
.failureUrl("/login?error=true");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(new UserDetailsService(){
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return readerRepository.findOne(username);
}
});
}
}
解决方案
您导入了错误的 Reader 实体 --> 您导入了“import java.io.Reader;” 您需要导入您的自定义 Reader 实体类。
推荐阅读
- javascript - 让我的班级在做 Array.from 时返回数组
- c++ - 为什么文件扩展名对编译有影响?
- f# - 在 F# 中,如何在字符串上使用 Array.Pick?
- java - 使用@Inject 进行依赖注入
- java - Setting.Secure ANDROID_ID 在使用不同的 APK 时返回不同的值
- python - python我如何在for循环中使用我的字符串
- docker - Docker compose,apache2:无法可靠地确定服务器的完全限定域名,
- python - 为什么在 django 中使用 asyncio 会有时间延迟
- python - Django 管理面板:如果在下拉列表中选择了特定值,则显示/隐藏字段
- reactjs - 在反应中我只想选择一个用户我的代码正在选择每个用户