Grails预认证场景
我正在尝试使用SpringSecurityCore在中配置Grails应用程序 因此,我编写了一个自定义身份验证过滤器:Grails预认证场景,grails,spring-security,pre-authentication,Grails,Spring Security,Pre Authentication,我正在尝试使用SpringSecurityCore在中配置Grails应用程序 因此,我编写了一个自定义身份验证过滤器: class MyAuthenticationFilter extends AbstractPreAuthenticatedProcessingFilter { protected getPreAuthenticatedPrincipal(request) { "my_username" } protected getPreAuthenticatedCreden
class MyAuthenticationFilter extends AbstractPreAuthenticatedProcessingFilter {
protected getPreAuthenticatedPrincipal(request) { "my_username" }
protected getPreAuthenticatedCredentials(request) { "N/A" }
}
将其添加到Spring:
beans = {
myAuthenticationFilter(MyAuthenticationFilter) {
authenticationManager = ref('authenticationManager')
checkForPrincipalChanges = true
}
}
并使用position PRE_AUTH_FILTER在引导中注册它:
class BootStrap {
def init = { servletContext ->
SpringSecurityUtils.clientRegisterFilter('myAuthenticationFilter',
SecurityFilterPosition.PRE_AUTH_FILTER.order)
}
}
Config.groovy只有标准的User、Role和UserRole类名,还有一些静态规则
在筛选器内部,我尝试从其getPreAuthenticatedPrincipal()
方法返回各种内容:用户id、作为字符串的用户id、用户名、用户对象本身……我可以看到每个请求都会调用我的筛选器(这是我想要的,设置了checkForPrincipalChanges=true
)无论我从中返回什么,当前用户仍然是匿名的:springSecurityService.principal
仍然是\ugrails.anonymous.user\uuuu
我需要在设置中更改哪些内容,以便能够使用现有组和角色对我的用户进行身份验证?我不想编写额外的身份验证提供程序,我可以使用Grails的标准
daoAuthenticationProvider
。我需要从我的筛选器返回特定的内容吗?我需要设置一些其他的Spring类吗?我解决了它。我在这里记录它,以防其他人也需要这样做
基本上,缺少的部分是将新的预验证身份验证提供程序配置为唯一的提供程序。无需其他代码,只需在参考资料中定义:
myAuthenticationProvider(PreAuthenticatedAuthenticationProvider) {
preAuthenticatedUserDetailsService = ref('authenticationUserDetailsService')
}
并将其设置为配置中的唯一提供程序:
grails.plugin.pringsecurity.providerNames = ['myAuthenticationProvider']
然后,从身份验证过滤器中,只返回预身份验证用户的用户名,如果没有,则返回null(匿名用户)