我正在尝试使用 Spring Security Core在预身份验证场景中配置 Grails 应用程序。
所以我写了一个自定义的身份验证过滤器:
class MyAuthenticationFilter extends AbstractPreAuthenticatedProcessingFilter {
protected getPreAuthenticatedPrincipal(request) { "my_username" }
protected getPreAuthenticatedCredentials(request) { "N/A" }
}
将其添加到 Spring:
beans = {
myAuthenticationFilter(MyAuthenticationFilter) {
authenticationManager = ref('authenticationManager')
checkForPrincipalChanges = true
}
}
并在 BootStrap 中注册,位置为 PRE_AUTH_FILTER:
class BootStrap {
def init = { servletContext ->
SpringSecurityUtils.clientRegisterFilter('myAuthenticationFilter',
SecurityFilterPosition.PRE_AUTH_FILTER.order)
}
}
Config.groovy 只有标准的 User、Role 和 UserRole 类名,外加一些 staticRules。
在过滤器内部,我尝试从它的方法返回各种东西getPreAuthenticatedPrincipal()
:用户 ID、作为字符串的用户 ID、用户名、用户对象本身……我可以看到每个请求都调用了我的过滤器(这就是我想要 set checkForPrincipalChanges = true
) 无论我从它返回什么,当前用户仍然是匿名的:springSecurityService.principal
仍然是__grails.anonymous.user__
我需要在我的设置中进行哪些更改,以便能够使用他们现有的组和角色对我的用户进行身份验证?我不想编写额外的身份验证提供程序,我对 Grails 的标准很好daoAuthenticationProvider
。我需要从我的过滤器中返回特定的东西吗?我需要设置其他一些 Spring 类吗?