:REST spring安全性 - 手动验证新用户并获取访问令牌 [英] :REST spring security - Manually authenticating a new user and getting access token

查看:118
本文介绍了:REST spring安全性 - 手动验证新用户并获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 rest spring security api 在Grails上编写一个REST风格的web服务。所有好...现在我想登录一个用户注册,有一个注册行动,并在注册完成后,我想登录该用户。我发现:

$ p $ springSecurityService.reauthenticate(username)method

但只有登录用户,但不在authentication_token表中创建访问令牌。



是否有其他可能的方式登录并获得该用户的访问令牌?

解决方案

该插件专为前端(纯HTML / JS客户端使用例如AngularJS)与后端(您的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)

重定向url:http://example.org/?access_token=$ { tokenValue}
}
}

然后,前端可以抓取令牌从URL中传递给每个后续API请求。


I am writing a RESTful webservice on grails, using rest spring security api. All good... now I want to login a user on registration, there is a registration action, and up on registration completion, i would like to login that user. I found:

springSecurityService.reauthenticate(username) method 

but that only login the user, but doesnt create access token in authentication_token table.

Is there other possible way to login and get the access token for that user?

解决方案

The plugin is designed for applications where the frontend (a pure HTML/JS client using, for example, AngularJS) is separated from the backend (your Grails app). In such scenario, the backend has to send back the frontend the access token, and the frontend has to store it somehow (usually using local storage or cookies), to pass it as an HTTP on every subsequent request.

You can do something like this in your controller:

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}"
    } 
}

Then, the frontend can grab the token from the URL and pass it on every subsequent API request.

这篇关于:REST spring安全性 - 手动验证新用户并获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆