Security 使用Grails&;自动登录;阿齐吉
我正在Grails应用程序中使用Acegi插件。用户注册后,将重定向到受保护的操作。因此,他会看到登录表单 但是,我更希望用户在完成注册后立即自动登录。看起来我可以通过重定向到登录表单使用的操作来实现这一点Security 使用Grails&;自动登录;阿齐吉,security,grails,spring-security,grails-plugin,Security,Grails,Spring Security,Grails Plugin,我正在Grails应用程序中使用Acegi插件。用户注册后,将重定向到受保护的操作。因此,他会看到登录表单 但是,我更希望用户在完成注册后立即自动登录。看起来我可以通过重定向到登录表单使用的操作来实现这一点 redirect(uri:"/j_acegi_security_check?j_username=${username}&j_password=${passed}") 但这将向客户端(并返回到服务器)发送一个HTTP请求,其中显示用户的密码。有没有一种方法可以让我以安全的方式自动登
redirect(uri:"/j_acegi_security_check?j_username=${username}&j_password=${passed}")
但这将向客户端(并返回到服务器)发送一个HTTP请求,其中显示用户的密码。有没有一种方法可以让我以安全的方式自动登录
谢谢,
Don我没有用非测试代码尝试过,但这是我创建的在集成测试中登录用户的方法(在测试设置中构建/保存适当的用户/角色后): 我在安全上下文中构造并注入身份验证令牌。为了让用户登录并通过安全保护,您可能还需要做一些事情,但这将是一切的开始
事实上,我需要在一两周内完成您对我当前应用程序的要求,如果您在我之前完全理解,请发回:)。如果您为spring security插件生成控制器类(grails generate registration),您将在RegisterController中看到以下几行代码,它们正是您想要的:
class RegisterController {
def daoAuthenticationProvider
...
def save = {
...
def auth = new AuthToken(person.username, params.passwd)
def authtoken = daoAuthenticationProvider.authenticate(auth)
SecurityContextHolder.context.authentication = authtoken
redirect uri: '/'
}
请确保params.passwd
是纯文本密码(即未哈希),并且它的工作方式类似于一个符咒。这是伯特·贝克维思的答案(不是我的)
(这是伯特留下的评论,但我认为它应该更加突出)
如果您没有密码,可以通过
def user = User.findByUsername(username)
以及在3参数构造函数中设置权限数组。通过创建身份验证
GrantedAuthority[] auths = user.authorities.collect { new GrantedAuthorityImpl(it.authority) }
然后可以省略对authenticate()的调用,并使用:
如果您有密码,则此操作有效。如果没有,可以通过“def user=user.findByUsername(username)”加载用户,并在3参数构造函数中设置权限数组。通过“GrantedAuthority[]auths=user.authorities.collect{new GrantedAuthorityImpl(it.authority)}”创建身份验证。然后您可以省略对authenticate()的调用,并使用“SecurityContextHolder.context.authentication=new UsernamePasswordAuthenticationToken(username,'unknown',auths)”,这真的有效吗?我尝试过,但没有成功,只有在提供了密码的情况下才有效。您实际上希望将user作为第一个参数传递给UsernamePasswordAuthenticationToken构造函数,而不是username,但除此之外,它可以工作:-)AuthToken类的包是什么?
GrantedAuthority[] auths = user.authorities.collect { new GrantedAuthorityImpl(it.authority) }
SecurityContextHolder.context.authentication = new UsernamePasswordAuthenticationToken(username, 'unknown', auths)