【发布时间】:2021-02-20 05:14:06
【问题描述】:
我试图在我的 grails 应用程序中简化安全检查的代码,我发现有一种方法可以提高服务类的安全性。
我找到的一些与此相关的参考资料: https://www.mscharhag.com/grails/spring-security-call-bean-method-in-spel-expression Grails custom security evaluator 还有其他一些...
所以我尝试将所有内容都连接起来,看起来很简单,但是当我将自定义 bean 配置为 resources.groovy 时,我收到了这个错误。
A component required a bean named 'parameterNameDiscoverer' that could not be found.
我的resources.groovy 看起来像这样:
import com.auth0.client.auth.AuthAPI
import grails.plugin.springsecurity.rest.RestAuthenticationProvider
import priz.auth0.Auth0APIService
import priz.auth0.Auth0TokenStorageService
import priz.auth0.Auth0TokenVerificationService
import priz.auth0.Auth0UserResolverService
import priz.security.GrailsBeanResolver
import priz.security.GrailsExpressionHandler
import priz.security.UserPasswordEncoderListener
// Place your Spring DSL code here
beans = {
expressionHandler(GrailsExpressionHandler) {
beanResolver = ref('beanResolver')
parameterNameDiscoverer = ref('parameterNameDiscoverer')
permissionEvaluator = ref('permissionEvaluator')
roleHierarchy = ref('roleHierarchy')
trustResolver = ref('authenticationTrustResolver')
}
beanResolver(GrailsBeanResolver) {
grailsApplication = ref('grailsApplication')
}
userPasswordEncoderListener(UserPasswordEncoderListener)
authApi(AuthAPI) { beanDefinition ->
beanDefinition.constructorArgs = [
'${priz.auth0.api.domain}',
'${priz.auth0.api.clientId}',
'${priz.auth0.api.clientSecret}'
]
}
auth0APIService(Auth0APIService) {
authAPI = ref('authApi')
}
auth0TokenVerificationService(Auth0TokenVerificationService)
auth0UserResolverService(Auth0UserResolverService)
tokenStorageService(Auth0TokenStorageService) {
jwtService = ref('jwtService')
userDetailsService = ref('userDetailsService')
auth0TokenVerificationService = ref('auth0TokenVerificationService')
auth0APIService = ref('auth0APIService')
auth0UserResolverService = ref('auth0UserResolverService')
}
/* restAuthenticationProvider */
restAuthenticationProvider(RestAuthenticationProvider) {
tokenStorageService = ref('tokenStorageService')
useJwt = false
jwtService = ref('jwtService')
}
}
当然,我没有在资源中专门定义parameterNameDiscoverer,但我希望由于我没有自定义任何这些依赖项,因此 Spring Security 插件已经提供了这些依赖项。但是好像找不到。
我错过了什么?我需要在资源中定义整个依赖树吗?
【问题讨论】:
-
“我错过了什么?”- 不清楚。 “我需要在资源中定义整个依赖树吗?” - 不。您特别想将什么注入到
priz.security.GrailsExpressionHandlerbean 的parameterNameDiscoverer属性中? -
我只需要一个带有自定义光束解析器的自定义表达式处理程序。但似乎我必须重新定义所有依赖项。
-
“但似乎我必须重新定义所有依赖项” - 事实并非如此。我们支持你正在做的事情。在您的上下文中是否有名称为
parameterNameDiscoverer的 bean? -
"But it seems like I have to redefine all the dependencies."- 如果这是真的,你会有其他问题,例如对userDetailsServicebean 的引用。 -
@JeffScottBrown 嗯...好点。最简单的检查方法是什么?
标签: grails spring-security service spring-security-rest