【问题标题】:How to generate an oauth token using spring without the password如何使用没有密码的spring生成oauth令牌
【发布时间】:2015-11-17 22:48:53
【问题描述】:
我正在尝试在我的 Spring Boot 应用程序中为用户创建一个额外的访问令牌。我不知道他们的密码,但用户有权使用该应用程序。据我所知,我需要调用类似
OAuth2AccessToken accessToken = tokenServices.createAccessTokenForUser(authenticationRequest, user);
其中 tokenServices 可能是 DefaultTokenServices 的一个实例。问题是如何获得对已配置令牌服务的引用?我看到它已连接到 AuthorizationServerEndpointsConfigurer 但我无法自动连接它。我正在使用 JWT 进行身份验证,所以真的在寻找一种生成 JWT 令牌的方法。
正在尝试实现Spring OAuth2 - Manually creating an access token in the token store中概述的流程
【问题讨论】:
标签:
spring
spring-security
spring-boot
【解决方案1】:
在我的 spring-security-config.xml 我有这个:
<bean id="tokenServices" class="de.hybris.platform.ycommercewebservices.oauth2.token.provider.HybrisOAuthTokenServices">
<property name="tokenStore" ref="tokenStore" />
<property name="supportRefreshToken" value="true" />
<property name="refreshTokenValiditySeconds" value="2592000" />
<!-- 60*60*24*30 = 30d -->
<property name="accessTokenValiditySeconds" value="43200" />
<!-- 60*60*12 = 12h -->
</bean>
我可以在哪里配置我的自定义 tokenServices
public class HybrisOAuthTokenServices extends DefaultTokenServices{
@Override
public OAuth2AccessToken createAccessToken(final OAuth2Authentication authentication) throws AuthenticationException{
try{
return super.createAccessToken(authentication);
}catch (final ModelSavingException e) {
//in case when other client was faster in saving access token - try to get token again
return super.createAccessToken(authentication);
}catch (final ModelRemovalException e) {
//in case when other client was faster in removing expired token - try to get token again
return super.createAccessToken(authentication);
}
}
}