ConfigurationPropertiese 无法将SecurityProperties 注入 IOC
来源:4-4 个性化用户认证流程(一)
SpringSecurity
2018-04-30
项目目录如图所示

BrowserProperties 类
package com.fara.security.core.properties
/**
* Created by 黄德辉 on 2018/04/28 16:22
**/
class BrowserProperties {
//设置用户跳转页面的默认值
var loginPage: String = "/fara-signIn.html"
}SecurityProperties 类
package com.fara.security.core.properties
import org.springframework.boot.context.properties.ConfigurationProperties
/**
* Created by 黄德辉 on 2018/04/28 16:21
**/
@ConfigurationProperties(prefix = "fara.security")
class SecurityProperties {
var browser: BrowserProperties = BrowserProperties()
}
SecurityCoreConfig 类
import com.fara.security.core.properties.SecurityProperties import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Configuration /** * Created by 黄德辉 on 2018/04/28 16:22 * 配置的作用 : 让security properties 读取器生效 **/ @Configuration @EnableConfigurationProperties(SecurityProperties::class) class SecurityCoreConfig
yml
spring: session: store-type: none datasource: driver-class-name: com.mysql.jdbc.Driver username: xxxxx password: xxxxxx url: jdbc:mysql://www.xxxxxx.com/fara?characterEncoding=utf-8&useSSL=false server: port: 8060 logging: level: root: warn org.springframework.security: warn org.springframework.web: debug fara: security: browser: loginPage: /demo-signIn.html
异常信息:
2018-04-30 20:51:35.457 DEBUG 21116 --- [ main] o.s.w.c.s.StandardServletEnvironment : Initialized StandardServletEnvironment with PropertySources [StubPropertySource {name='servletConfigInitParams'}, StubPropertySource {name='servletContextInitParams'}, MapPropertySource {name='systemProperties'}, SystemEnvironmentPropertySource {name='systemEnvironment'}]
Root WebApplicationContext: initialization completed in 2062 ms
2018-04-30 20:51:38.490 DEBUG 21116 --- [ main] o.s.w.c.s.StandardServletEnvironment : Replacing PropertySource 'servletContextInitParams' with 'servletContextInitParams'
2018-04-30 20:51:38.790 WARN 21116 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'browserSecurityConfig' defined in file [/home/huangdehui/data3/IdeaProjects/Kotlin/fara-security/fara-security-browser/out/production/classes/com/fara/security/browser/BrowserSecurityConfig.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fara.security.core.properties.SecurityProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
异常指出spring IOC 容器中没有 securityProperties 这个Bean
package com.fara.security.browser
import com.fara.security.core.properties.SecurityProperties //自己的 SecurityProperties
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
@Configuration
class BrowserSecurityConfig @Autowired constructor( private val securityProperties: SecurityProperties)
: WebSecurityConfigurerAdapter() {
@Bean
fun passwordEncoder(): PasswordEncoder {
return BCryptPasswordEncoder()
}
/**
* 用表单去登录
* 这段代码基本上就是spring security 默认的配置代码
*/
override fun configure(http: HttpSecurity?) {
http!!.formLogin() //表单登入
.loginPage("/authentication/require")
.loginProcessingUrl("/authentication/form") ////将请求指定给 UsernamePasswordAuthenticationFilter 处理
//http!!.httpBasic()
.and() //开始配置授权
.authorizeRequests() //对请求做授权
.antMatchers( "/authentication/require",securityProperties.browser.loginPage)
.permitAll() //所有指向fara-signIn.html 请求都允许
.anyRequest() //任何请求
.authenticated() //都需要身份认证
.and()
.csrf().disable()
}
} BrowserSecurityConfig 构造器 中的成员属性无法被注入导致的错误 我尝试过将 SecurityProperties 的@ConfigurationProperties 注解替换成@Component 项目可以正常运行。

yml 与 preix 我已经认真检查过了,但是项目一直爆出 Unsatisfied dependency expressed through constructor parameter 0 指我
class BrowserSecurityController @Autowired constructor(private val securityProperties: SecurityProperties)
无法注入构造器中的 securityProperties 的 Bean 我感觉是我yml 与配置类书写错误,或许是我gradle 依赖有问题,但是却无法发现错误在哪里,麻烦老师帮助看一下。
2回答
-
额...这是什么语法?kotlin么?spring 5刚开始支持,不知道会不会是框架本身的bug。
022018-05-04 -
SpringSecurity
提问者
2018-05-04

图1-1
如图1-1所示SecurityCoreConfig.kt没有包的声明(idea没有报错)所以导致了 SecurityProperties这个对象没有被注入到ioc容器中,导致在BrowserSecurityController.kt 中它无法被注入,缺失了这个Bean如图1-2

图1-2
那么我详细的描述一下当时的原因吧,当SecurityCoreConfig在idea的列表中存在的时候它是这样被显示的图1-3

图1-3
老师的视频中先建了BrowserProperties.kt与SecurityProperties.kt类的时候我模仿着老师的步骤,但是在建立SecurityCoreConfig.kt时由于该文件不存在idea的目录如图1-4所示

图1-4
直接右键去创建SecurityCoreConfig.kt文件就只能创建在properties包中idea在这里有点不是那么方便,我也不知道该怎么做,于是我就直接使用terminal终端idea自带的,用touch SecurityCoreConfig.kt 命令去创建了kt文件,当文件创建完后.java文件如果没有声明包是会包错的,但是kotlin它不会于是我没有声明包就直接点击运行了。由于demo模块下的DemoApplication.kt如图1-5

图1-5
会去扫描com.fara下所有的.class文件,但是我的SecurityCoreConfig.kt没有声明包,所以spring是无法获取这个类的,更不会去讲它注入进ioc容器。所以才有了以上的错误。
00
Spring Security技术栈开发企业级认证与授权
2662 学习 · 1561 问题
相似问题




