UnsatisfiedDependencyException:创建名为“securityConfig”的 bean 时出错
Posted
技术标签:
【中文标题】UnsatisfiedDependencyException:创建名为“securityConfig”的 bean 时出错【英文标题】:UnsatisfiedDependencyException: Error creating bean with name 'securityConfig' 【发布时间】:2019-06-15 04:54:06 【问题描述】:我尝试使用本教程通过 jwt 令牌创建身份验证:https://www.callicoder.com/spring-boot-spring-security-jwt-mysql-react-app-part-1/
我无法解决这个问题:
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:157) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at com.easythings.battleship.BattleshipApplicationKt.main(BattleshipApplication.kt:18) [classes/:na]
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:125) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:86) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:415) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:174) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:181) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
... 8 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'customUserDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customUserDetailsService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#58ec8271' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#58ec8271': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @Column(s) not allowed on a @ManyToOne property: com.easythings.battleship.model.Game.board
安全配置:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, jsr250Enabled = true, prePostEnabled = true)
class SecurityConfig : WebSecurityConfigurerAdapter()
@Autowired
internal var customUserDetailsService: CustomUserDetailsService? = null
@Autowired
private val unauthorizedHandler: JwtAuthenticationEntryPoint? = null
@Bean
fun jwtAuthenticationFilter(): JwtAuthenticationFilter
return JwtAuthenticationFilter()
@Throws(Exception::class)
public override fun configure(authenticationManagerBuilder: AuthenticationManagerBuilder?)
authenticationManagerBuilder!!
.userDetailsService<UserDetailsService>(customUserDetailsService)
.passwordEncoder(passwordEncoder())
@Bean(BeanIds.AUTHENTICATION_MANAGER)
@Throws(Exception::class)
override fun authenticationManagerBean(): AuthenticationManager
return super.authenticationManagerBean()
@Bean
fun passwordEncoder(): PasswordEncoder
return BCryptPasswordEncoder()
@Throws(Exception::class)
override fun configure(http: HttpSecurity)
http
.cors()
.and()
.csrf()
.disable()
.exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler)
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/",
"/favicon.ico",
"/**/*.png",
"/**/*.gif",
"/**/*.svg",
"/**/*.jpg",
"/**/*.html",
"/**/*.css",
"/**/*.js")
.permitAll()
.antMatchers("/api/auth/**")
.permitAll()
.antMatchers("/api/user/checkUsernameAvailability", "/api/user/checkEmailAvailability")
.permitAll()
.antMatchers(HttpMethod.GET, "/api/polls/**", "/api/users/**")
.permitAll()
.anyRequest()
.authenticated()
// Add our custom JWT security filter
http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter::class.java)
CustomUserDetailsService:
@Service
class CustomUserDetailsService : UserDetailsService
@Autowired
internal var userRepository: UserRepository? = null
@Transactional
@Throws(UsernameNotFoundException::class)
override fun loadUserByUsername(usernameOrEmail: String): UserDetails
// Let people login with either username or email
val user = userRepository!!.findByUsernameOrEmail(usernameOrEmail, usernameOrEmail)
.orElseThrow
UsernameNotFoundException("User not found with username or email : $usernameOrEmail")
return UserPrincipal.create(user)
// This method is used by JWTAuthenticationFilter
@Transactional
fun loadUserById(id: Long?): UserDetails
val user = userRepository!!.findById(id!!)
.orElseThrow UsernameNotFoundException("User not found with id : $id")
return UserPrincipal.create(user)
我是春天的新人,我不知道出了什么问题。 我的 springBootVersion 是“2.1.2.RELEASE”。我应该改变什么?
【问题讨论】:
【参考方案1】:嵌套异常是 org.hibernate.AnnotationException: @Column(s) not 在 @ManyToOne 属性上允许: com.easythings.battleship.model.Game.board
您似乎在实体类的 manyToOne 字段上添加了@Column
注释。请改用@JoinColumn
。 (看this question和hibernate docs)
【讨论】:
以上是关于UnsatisfiedDependencyException:创建名为“securityConfig”的 bean 时出错的主要内容,如果未能解决你的问题,请参考以下文章