suf*_*aib 6 rest grails spring-security
我正在使用rest spring security api编写一个关于grails的RESTful web服务.一切都很好...现在我想在注册时登录用户,有注册操作,注册完成后,我想登录该用户.我发现:
springSecurityService.reauthenticate(username) method
Run Code Online (Sandbox Code Playgroud)
但是只登录用户,但不在authentication_token表中创建访问令牌.
是否有其他可能的方法来登录并获取该用户的访问令牌?
小智 10
该插件专为前端(使用AngularJS的纯HTML/JS客户端)与后端(Grails应用程序)分离的应用程序而设计.在这种情况下,后端必须将访问令牌发送回前端,并且前端必须以某种方式(通常使用本地存储或cookie)存储它,以便在每个后续请求中将其作为HTTP传递.
您可以在控制器中执行以下操作:
class RegisterController {
def springSecurityService
def tokenGenerator
def tokenStorageService
def register() {
//do stuff
springSecurityService.reauthenticate(username)
String tokenValue = tokenGenerator.generateToken()
tokenStorageService.storeToken(tokenValue, springSecurityService.principal)
redirect url: "http://example.org/?access_token=${tokenValue}"
}
}
Run Code Online (Sandbox Code Playgroud)
然后,前端可以从URL中获取令牌,并在每个后续API请求中传递它.
| 归档时间: |
|
| 查看次数: |
1293 次 |
| 最近记录: |