我正在使用 Spring+Oauth2 来保护 Web 服务,并且我添加了自定义授权类型 (custom-grant):
<bean id="myTokenGranter" class="com.example.oauth2.MyTokenGranter" />
<oauth:authorization-server client-details-service-ref="client-details-service" token-services-ref="tokenServices">
<oauth:refresh-token/>
<oauth:password/>
<oauth:custom-grant token-granter-ref="myTokenGranter" />
</oauth:authorization-server>
Spring 调用实现就好了。但是我不知道我应该如何在这里实际生成一个 token 。我看到他们使用了一个名为“RandomValueStringGenerator”的类,但我不确定是否没有更好的方法,而且我不知道如何生成一个“好”的 token ,因为它应该有多长,或者 spring 是否检查 token 的唯一性实际上等。有没有办法在这里调用 Spring 自己的生成器部分?
现在这是我的 tokengranter 类:
public class MyTokenGranter implements TokenGranter {
private RandomValueStringGenerator generator = new RandomValueStringGenerator();
@Override
public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
//...logic added here later
return new DefaultOAuth2AccessToken(generator.generate());
}
我找不到这方面的一个很好的例子,在 spring ouath2 源中只有一个实现了一半的测试 tokengranter。
最佳答案
好吧,这实际上可以通过 org.springframework.security.oauth2.provider.token.AbstractTokenGranter
来完成,方法是复制它或尝试传递适当的构造函数。我只是把它贴给有同样问题的人。您还可以扩展 AbstractTokenGranter
但我未能传递正确的构造函数
这是我的实现:
public class MyTokenGranter implements TokenGranter {
@Autowired
private AuthorizationServerTokenServices tokenService;
@Autowired
private ClientDetailsService clientDetailsService;
@Autowired
private DefaultOAuth2RequestFactory defaultOauth2RequestFactory;
private String grantType;
@Override
public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
if (!this.grantType.equals(grantType)) {
return null;
}
String clientId = tokenRequest.getClientId();
ClientDetails client = clientDetailsService.loadClientByClientId(clientId);
validateGrantType(grantType, client);
return getAccessToken(client, tokenRequest);
}
protected OAuth2AccessToken getAccessToken(ClientDetails client, TokenRequest tokenRequest) {
return tokenService.createAccessToken(getOAuth2Authentication(client, tokenRequest));
}
protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
OAuth2Request storedOAuth2Request = defaultOauth2RequestFactory.createOAuth2Request(client, tokenRequest);
return new OAuth2Authentication(storedOAuth2Request, null);
}
protected void validateGrantType(String grantType, ClientDetails clientDetails) {
Collection<String> authorizedGrantTypes = clientDetails.getAuthorizedGrantTypes();
if (authorizedGrantTypes != null && !authorizedGrantTypes.isEmpty()
&& !authorizedGrantTypes.contains(grantType)) {
throw new InvalidClientException("Unauthorized grant type: " + grantType);
}
}
public String getGrantType() {
return grantType;
}
public void setGrantType(String grantType) {
this.grantType = grantType;
}
}
XML配置:
<bean id="myTokenGranter" class="com.example.MyTokenGranter">
<property name="grantType" value="custom-grant" />
</bean>
<oauth:authorization-server client-details-service-ref="clientDetailsService" token-services-ref="tokenServices">
<oauth:refresh-token/>
<oauth:password/>
<oauth:custom-grant token-granter-ref="myTokenGranter" />
</oauth:authorization-server>
关于java - Spring Security 和 OAuth2 生成具有自定义授权类型的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29034214/