Spring OAuth2 从访问令牌字符串中提取委托人
Spring OAuth2 extract Principal from access token string
我在控制器中收到了访问令牌,我需要从字符串访问令牌中提取主体。不在方法参数中使用身份验证,因为此对象将是不同的用户。令牌的简单解码应该有所帮助。任何人都知道如何从访问令牌字符串中做到这一点?
例子
@RequestMapping(value = "create", method = RequestMethod.POST)
public ResponseEntity create(Authentication authentication,@RequestParam("access_token") String accessToken) {
//extract Principal from accessToken variable
}
我不知道您为什么要在请求中发送另一个用户的令牌,我发现这很危险,因为访问令牌包含敏感信息 ( credentials )。我建议您通过创建诸如操作或标识令牌之类的东西来更改识别第二个用户的方式(您定义的架构将包含用户的 ID 和您要发送的信息)。
如果你有另一个你没有提到的哲学并且假设访问令牌是 Jwt,你必须首先验证它,使用算法和用于散列的私钥 it.if 它是一个有效的令牌,您可以访问其内容。
@RequestMapping(value = "create", method = RequestMethod.POST)
public ResponseEntity create(Authentication authentication,@RequestParam("access_token") JwtAuthenticationToken accessToken) {
// validate your accessToken
// to access the token details
accessToken.getTokenAttributes().get(A_KEY_IN_YOUR_TOKEN)
}
检查这个class
一段时间后,我设法从访问令牌字符串中获取主体。
@Autowired
private TokenStore tokenStore;
@RequestMapping(value = "create", method = RequestMethod.POST)
public ResponseEntity create(Authentication authentication,@RequestParam("access_token") String accessToken) {
tokenStore.readAuthentication(accessToken).getPrincipal();
}
我在控制器中收到了访问令牌,我需要从字符串访问令牌中提取主体。不在方法参数中使用身份验证,因为此对象将是不同的用户。令牌的简单解码应该有所帮助。任何人都知道如何从访问令牌字符串中做到这一点? 例子
@RequestMapping(value = "create", method = RequestMethod.POST)
public ResponseEntity create(Authentication authentication,@RequestParam("access_token") String accessToken) {
//extract Principal from accessToken variable
}
我不知道您为什么要在请求中发送另一个用户的令牌,我发现这很危险,因为访问令牌包含敏感信息 ( credentials )。我建议您通过创建诸如操作或标识令牌之类的东西来更改识别第二个用户的方式(您定义的架构将包含用户的 ID 和您要发送的信息)。
如果你有另一个你没有提到的哲学并且假设访问令牌是 Jwt,你必须首先验证它,使用算法和用于散列的私钥 it.if 它是一个有效的令牌,您可以访问其内容。
@RequestMapping(value = "create", method = RequestMethod.POST)
public ResponseEntity create(Authentication authentication,@RequestParam("access_token") JwtAuthenticationToken accessToken) {
// validate your accessToken
// to access the token details
accessToken.getTokenAttributes().get(A_KEY_IN_YOUR_TOKEN)
}
检查这个class
一段时间后,我设法从访问令牌字符串中获取主体。
@Autowired
private TokenStore tokenStore;
@RequestMapping(value = "create", method = RequestMethod.POST)
public ResponseEntity create(Authentication authentication,@RequestParam("access_token") String accessToken) {
tokenStore.readAuthentication(accessToken).getPrincipal();
}