1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.security.oauth2.provider.client;
18
19 import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
20 import org.springframework.security.oauth2.common.OAuth2AccessToken;
21 import org.springframework.security.oauth2.provider.ClientDetailsService;
22 import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
23 import org.springframework.security.oauth2.provider.TokenRequest;
24 import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
25 import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
26
27
28
29
30
31 public class ClientCredentialsTokenGranter extends AbstractTokenGranter {
32
33 private static final String GRANT_TYPE = "client_credentials";
34 private boolean allowRefresh = false;
35
36 public ClientCredentialsTokenGranter(AuthorizationServerTokenServices tokenServices,
37 ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory) {
38 this(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE);
39 }
40
41 protected ClientCredentialsTokenGranter(AuthorizationServerTokenServices tokenServices,
42 ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, String grantType) {
43 super(tokenServices, clientDetailsService, requestFactory, grantType);
44 }
45
46 public void setAllowRefresh(boolean allowRefresh) {
47 this.allowRefresh = allowRefresh;
48 }
49
50 @Override
51 public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
52 OAuth2AccessToken token = super.grant(grantType, tokenRequest);
53 if (token != null) {
54 DefaultOAuth2AccessToken norefresh = new DefaultOAuth2AccessToken(token);
55
56 if (!allowRefresh) {
57 norefresh.setRefreshToken(null);
58 }
59 token = norefresh;
60 }
61 return token;
62 }
63
64 }