当前位置:   article > 正文

OAuth2 源码分析(一.核心类)_oauth2源码

oauth2源码

Spring Security源码解析篇介绍了Spring Security的原理,复习下几个概念

  • Principle   GrantedAuthority    Authentication   AbstractAuthenticationToken  UsernamePasswordAuthenticationToken
  • AuthenticationManager  AuthenticationProvider  DaoAuthenticationProvider          ProviderManager   
  • UserDetail  CredentialsContainer  UserDetailsService  UserCache 
  • WebSecurityConfigurerAdapter  WebSecurity   HttpSecuirty  FilterChainProxy

接下来介绍oauth2的相关概念。oauth2是建立在spring security基础上的一套规范,并非框架。

主要角色有

  • 授权服务器   AuthorizationServer    配置client,tokenStore,authenticationManager等
  • 资源服务器   ResourceServer     配置HttpSecurity,哪些uri需要验证;配置ResourceServerSecurityConfigurer,如设置tokenService
  • 客户端   client    包含clientid,secret
  • 用户   user   包含username,password

一般来说,资源服务器和授权服务器都是一个机构提供的,授权服务器给客户端授权,客户端再去请求资源服务器(授权服务器和资源服务器可以是一个应用程序也可以是两个应用程序)。下面例子中的QQ既是资源服务器,也是授权服务器。

用户想登录csdn(客户端),发现还没有注册csdn(客户端)的账号,刚好看见有一个通过QQ登录的方式,遂输入QQ的账户密码,点击授权。此时QQ服务器(授权服务器)对csdn(客户端)和用户密码进行验证,验证成功后给csdn(客户端)发放令牌,csdn凭借令牌去获取用户的QQ头像(资源服务器)等信息。

目录

1. 好基友一辈子    OAuth2Authentication和OAuth2AccessToken

1.1 OAuth2Authentication

1.2 BaseRequest

1.2.1 AuthorizationRequest

1.2.2 TokenRequest

1.2.3 OAuth2Request

1.2.4 OAuth2RequestFactory

1.3 OAuth2AccessToken

2. TokenGranter、TokenStore、TokenExtractor

2.1 TokenGranter

2.1.1 AbstractTokenGranter

2.1.2 CompositeTokenGranter

2.2 TokenStore

2.3 TokenExtractor 

2.4 ResourceServerTokenServices

2.4.1 DefaultTokenServices

2.4.2 RemoteTokenServices

3. ClientDetails   ClientDetailsService

3.1 ClientDetails

3.2 ClientDetailsService

3.3 ClientDetailsServiceBuilder

4. 资源服务器配置  ResourceServerConfigurerAdapter

5. 授权服务器配置    AuthorizationServerConfigurerAdapter

6.TokenEndPoint,AuthorizationEndPoint,CheckTokenEndPoint

6.1 TokenEndPoint

6.2 AuthorizationEndPoint

6.3 CheckTokenEndpoint

7.总结

 


1. 好基友一辈子    OAuth2Authentication和OAuth2AccessToken

1.1 OAuth2Authentication

OAuth2Authentication顾名思义是Authentication的子类,存储用户信息和客户端信息,但多了2个属性

  1. private final OAuth2Request storedRequest;
  2. private final Authentication userAuthentication;

这样OAuth2Authentication可以存储2个Authentication,一个给client(必要),一个给user(只是有些授权方式需要)。除此之外同样有principle,credentials,authorities,details,authenticated等属性。

OAuth2Request 用于存储request中的Authentication信息(grantType,responseType,resouceId,clientId,scope等),这里就引出了OAuth2 中的三大request。

1.2 BaseRequest

BaseRequest是抽象类,有3个属性:clienId、scope和requestParameters。

  1. abstract class BaseRequest implements Serializable {
  2. private String clientId;
  3. private Set<String> scope = new HashSet<String>();
  4. private Map<String, String> requestParameters = Collections
  5. .unmodifiableMap(new HashMap<String, String>());
  6. /** setter,getter */
  7. }

继承类见下图,3个类都在OAuth2包中,这些request都会存在于OAuth2的验证流程中,用于传递clientId,scope,requestParameters等属性,与HttpServletRequest有本质区别

                                        

1.2.1 AuthorizationRequest

向授权服务器AuthorizationEndPoint (/oauth/authorize)请求授权,AuthorizationRequest作为载体存储state,redirect_uri等参数,生命周期很短且不能长时间存储信息,可用OAuth2Request代替存储信息。

  1. public class AuthorizationRequest extends BaseRequest implements Serializable {
  2. // 用户同意授权传递的参数,不可改变
  3. private Map<String, String> approvalParameters = Collections.unmodifiableMap(new HashMap<String, String>());
  4. // 客户端发送出的状态信息,从授权服务器返回的状态应该不变才对
  5. private String state;
  6. // 返回类型集合
  7. private Set<String> responseTypes = new HashSet<String>();
  8. // resource ids 可变
  9. private Set<String> resourceIds = new HashSet<String>();
  10. // 授权的权限
  11. private Collection<? extends GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
  12. // 终端用户是否同意该request发送
  13. private boolean approved = false;
  14. // 重定向uri
  15. private String redirectUri;
  16. // 额外的属性
  17. private Map<String, Serializable> extensions = new HashMap<String, Serializable>();
  18. // 持久化到OAuth2Request
  19. public OAuth2Request createOAuth2Request() {
  20. return new OAuth2Request(getRequestParameters(), getClientId(), getAuthorities(), isApproved(), getScope(), getResourceIds(), getRedirectUri(), getResponseTypes(), getExtensions());
  21. }
  22. // setter,getter
  23. }

1.2.2 TokenRequest

向授权服务器TokenEndPoint(/oauth/token)发送请求获得access_token时,tokenRequest作为载体存储请求中grantType等参数。常和tokenGranter.grant(grantType,tokenRequest)结合起来使用。

TokenRequest携带了新属性grantType,和方法createOAuth2Request(用于持久化)

  1. private String grantType;
  2. public OAuth2Request createOAuth2Request(ClientDetails client) {
  3. Map<String, String> requestParameters = getRequestParameters();
  4. HashMap<String, String> modifiable = new HashMap<String, String>(requestParameters);
  5. // Remove password if present to prevent leaks
  6. modifiable.remove("password");
  7. modifiable.remove("client_secret");
  8. // Add grant type so it can be retrieved from OAuth2Request
  9. modifiable.put("grant_type", grantType);
  10. return new OAuth2Request(modifiable, client.getClientId(), client.getAuthorities(), true, this.getScope(),
  11. client.getResourceIds(), null, null, null);
  12. }

1.2.3 OAuth2Request

用来存储TokenRequest或者AuthorizationRequest的信息,只有构造方法和getter方法,不提供setter方法。它作为OAuth2Authentication的一个属性(StoredRequest),存储request中的authentication信息(grantType,approved,responseTypes)。

1.2.4 OAuth2RequestFactory

工厂类生成OAuth2Request、TokenRequest、AuthenticationRequest。

  1. public interface OAuth2RequestFactory {
  2. /**
  3. * 从request请求参数中获取clientId,scope,state
  4. * clientDetailsService loadClientByClientId(clientId) 获取clientDetails resourcesId Authorities
  5. * 根据以上信息生成AuthenticationRequest
  6. */
  7. AuthorizationRequest createAuthorizationRequest(Map<String, String> authorizationParameters);
  8. /**
  9. * AuthorizationRequest request 有生成OAuth2Request的方法
  10. * request.createOAuth2Request()
  11. */
  12. OAuth2Request createOAuth2Request(AuthorizationRequest request);
  13. OAuth2Request createOAuth2Request(ClientDetails client, TokenRequest tokenRequest);
  14. TokenRequest createTokenRequest(Map<String, String> requestParameters, ClientDetails authenticatedClient);
  15. TokenRequest createTokenRequest(AuthorizationRequest authorizationRequest, String grantType);
  16. }

1.3 OAuth2AccessToken

OAuth2AccessToken是一个接口,提供安全令牌token的基本信息,不包含用户信息,仅包含一些静态属性(scope,tokenType,expires_in等)和getter方法,如String getScope,OAuth2RefreshToken getRefreshToken,String getTokenType,String getValue()等。TokenGranter.grant()返回的值即OAuth2AccessToken

OAuth2AccessToken和OAuth2Authentication是一对好基友,谁要先走谁是狗!!!

TokenStore同时存储OAuth2AccessToken和OAuth2Authentication,也可根据OAuth2Authentication中的OAuth2Request信息可获取对应的OAuth2AccessToken。

DefaultTokenServices有如下方法,都可以通过一个获得另一个的值  

OAuth2AccessToken createAccessToken(OAuth2Authentication authentication)
OAuth2Authentication loadAuthentication(String accessTokenValue)

// 当tokenStore是jdbcTokenStore,表示从数据库中根据OAuth2Authentication获取OAuth2AccessToken

OAuth2AccessToken existingAccessToken = tokenStore.getAccessToken(authentication);
 

DefaultOAuth2AccessToken是OAuth2AccessToken的实现类,多了构造方法,setter方法和OAuth2AccessToken  valueOf(Map<String,Object> tokenParams)。经过json转换后就是我们常见的access_token对象,如下所示。

{
  "access_token": "1e95d081-0048-4397-a081-c76f7823fe54",
  "token_type": "bearer",
  "refresh_token": "7f6db28b-50dc-40a2-b381-3e356e30af2b",
  "expires_in": 1799,
  "scope": "read write"
}

OAuth2RefreshToken是接口,只有String getValue()方法。

DefaultOAuth2RefreshToken是OAuth2RefreshToken的实现类。

2. TokenGranter、TokenStore、TokenExtractor

2.1 TokenGranter(/oauth/token)

一般在用户请求TokenEndPoints中的路径/oauth/token时,根据请求参数中的grantType,username,password,client_id,client_secret等,调用TokenGranter给用户分发OAuth2AccessToken。

根据grantType(password,authorization-code)和TokenRequest(requestParameters,clientId,grantType)授予人OAuth2AccessToken令牌。

  1. public interface TokenGranter {
  2. OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest);
  3. }

回忆下TokenRequest包含了基本信息clientId,scope,requestParameters,grantType等。根据tokenRequest获取OAuth2Request,初始化获得OAuth2Authentication,再去数据库里找oauth2accesstoken,如果有则直接返回,如果没有则创建新的oauth2accesstoken,并且和OAuth2Authentication一起存入数据库中。

2.1.1 AbstractTokenGranter

TokenGranter抽象继承类AbstractTokenGranter,实现了grant方法,源码如下。

执行顺序为根据tokenRequest====》clientId ====》clientDetails====》OAuth2Authentication(getOAuth2Authentication(client,tokenRequest))====》OAuth2AccessToken(tokenService.createAccessToken)

  1. public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
  2. if (!this.grantType.equals(grantType)) {
  3. return null;
  4. }
  5. String clientId = tokenRequest.getClientId();
  6. ClientDetails client = clientDetailsService.loadClientByClientId(clientId);
  7. validateGrantType(grantType, client);
  8. logger.debug("Getting access token for: " + clientId);
  9. // getAccessToken 先获得OAuth2Authentication,再创建OAuth2AccessToken
  10. return getAccessToken(client, tokenRequest);
  11. }
  12. protected OAuth2AccessToken getAccessToken(ClientDetails client, TokenRequest tokenRequest) {
  13. return tokenServices.createAccessToken(getOAuth2Authentication(client, tokenRequest));
  14. }
  15. // AbstractTokenGranter的继承类重写了该方法
  16. protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
  17. OAuth2Request storedOAuth2Request = requestFactory.createOAuth2Request(client, tokenRequest);
  18. return new OAuth2Authentication(storedOAuth2Request, null);
  19. }

实现AbstractTokenGranter的类有5种。其中如果用password的方式进行验证,那么TokenGranter类型是ResourceOwnerPasswordTokenGranter,该类中重写了getOAuth2Authentication方法,里面调用了authenticationManager.manage()方法。

用户可自行定义granter类继承AbstractTokenGranter,重写getOAuth2Authentication()方法,并将该granter类添加至CompositeTokenGranter中。

TokenGranter的继承类如下图所示

                

2.1.2 CompositeTokenGranter

有继承类CompositeTokenGranter,包含List<TokenGranter> tokenGranters属性,grant方法是遍历tokenGranters进行逐一grant,只要有一个有返回值就返回。

  1. public class CompositeTokenGranter implements TokenGranter {
  2. private final List<TokenGranter> tokenGranters;
  3. public CompositeTokenGranter(List<TokenGranter> tokenGranters) {
  4. this.tokenGranters = new ArrayList<TokenGranter>(tokenGranters);
  5. }
  6. public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
  7. for (TokenGranter granter : tokenGranters) {
  8. OAuth2AccessToken grant = granter.grant(grantType, tokenRequest);
  9. if (grant!=null) {
  10. return grant;
  11. }
  12. }
  13. return null;
  14. }
  15. public void addTokenGranter(TokenGranter tokenGranter) {
  16. if (tokenGranter == null) {
  17. throw new IllegalArgumentException("Token granter is null");
  18. }
  19. tokenGranters.add(tokenGranter);
  20. }
  21. }

2.2 TokenStore(/oauth/token)

一般在TokenGranter执行grant方法完毕后,将OAuth2AccessToken和OAuth2Authentication存储起来,方便以后根据其中一个查询另外一个(如根据access_token查询获得OAuth2Authentication)。

存储OAuth2AccessTokenOAuth2Authentication(比Authentication多了两个属性storedRequest,userAuthentication),存储方法如下。还有各种read,remove方法

void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication);

实现类有5类,其中JdbcTokenStore是通过连接数据库来存储OAuth2AccessToken的,这也是我们一般存储token的方法。条件是数据库里的表结构必须按照标准建立。(JwtTokenStore不存储token和authentication,直接根据token解析获得authentication)

oauth_access_token表结构如下,可见表里存储了OAuth2AccessToken和OAuth2Authentication两个对象,值得注意的是token_id并不等于OAuth2AccessToken.getValue(),value经过MD5加密后才是token_id。同理authentication_id 和 refresh_token也是经过加密转换存储的。

第一次获得token,直接存入数据库表里。

如果重复post请求/oauth/token,  JdbcTokenStore会先判断表中是否已有该用户的token,如果有先删除,再添加。

           

2.3 TokenExtractor (OAuth2AuthentiactionProcessingFilter)

用户携带token访问资源,过滤器进行到OAuth2AuthentiactionProcessingFilter时,从HttpServletRequest中获取access_token(可以从header或者params中获取),拼接成PreAuthenticatedAuthenticationToken(Authentication子类)

Authentication extract(HttpServletRequest request);

BearerTokenExtractor是它的实现类,实现了从request中获取Authentication的方法。

1.header中  Authentication:Bearer xxxxxxxx--xxx

2.request parameters中  access_token=xxxx-xxxx-xxxx

  1. protected String extractToken(HttpServletRequest request) {
  2. // 1.直接从header中提取key为Authentication,value是以Bearer 开头的header
  3. // 如Authentication:Bearer f732723d-af7f-41bb-bd06-2636ab2be135
  4. String token = extractHeaderToken(request);
  5. // bearer type allows a request parameter as well
  6. if (token == null) {
  7. logger.debug("Token not found in headers. Trying request parameters.");
  8. // 2.如果header中不包含,则从param中获取"access_token"对应的值
  9. token = request.getParameter(OAuth2AccessToken.ACCESS_TOKEN);
  10. if (token == null) {
  11. logger.debug("Token not found in request parameters. Not an OAuth2 request.");
  12. }
  13. else {
  14. request.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_TYPE, OAuth2AccessToken.BEARER_TYPE);
  15. }
  16. }
  17. return token;
  18. }

2.4 ResourceServerTokenServices

两个方法。用户携access_token访问资源服务器时,资源服务器会将该字符串进行解析,获得OAuth2Authentication和OAuth2AccessToken。

loadAuthentication根据字符串accessToken获得OAuth2Authentication;

readAccessToken根据字符串accessToken获得OAuth2AccessToken。

  1. public interface ResourceServerTokenServices {
  2. /**
  3. * Load the credentials for the specified access token.
  4. *
  5. * @param accessToken The access token value.
  6. * @return The authentication for the access token.
  7. * @throws AuthenticationException If the access token is expired
  8. * @throws InvalidTokenException if the token isn't valid
  9. */
  10. OAuth2Authentication loadAuthentication(String accessToken) throws AuthenticationException, InvalidTokenException;
  11. /**
  12. * Retrieve the full access token details from just the value.
  13. *
  14. * @param accessToken the token value
  15. * @return the full access token with client id etc.
  16. */
  17. OAuth2AccessToken readAccessToken(String accessToken);
  18. }

有两重要继承类,DefaultTokenServices和RemoteTokenServices

2.4.1 DefaultTokenServices

实现了两个接口AuthorizationServerTokenServices和ResourceServerTokenServices。常在granter().grant()方法中调用tokenServices.createAccessToken()方法获得oauth2accesstoken。

其中重要方法createAccessToken(OAuth2Authentication oauth2)源码如下

  1. @Transactional
  2. public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException {
  3. OAuth2AccessToken existingAccessToken = tokenStore.getAccessToken(authentication);
  4. OAuth2RefreshToken refreshToken = null;
  5. // 如果数据库中已存了authentication和accesstoken,则直接提取
  6. if (existingAccessToken != null) {
  7. if (existingAccessToken.isExpired()) {
  8. if (existingAccessToken.getRefreshToken() != null) {
  9. refreshToken = existingAccessToken.getRefreshToken();
  10. // The token store could remove the refresh token when the
  11. // access token is removed, but we want to
  12. // be sure...
  13. tokenStore.removeRefreshToken(refreshToken);
  14. }
  15. tokenStore.removeAccessToken(existingAccessToken);
  16. }
  17. else {
  18. // Re-store the access token in case the authentication has changed
  19. tokenStore.storeAccessToken(existingAccessToken, authentication);
  20. return existingAccessToken;
  21. }
  22. }
  23. // Only create a new refresh token if there wasn't an existing one
  24. // associated with an expired access token.
  25. // Clients might be holding existing refresh tokens, so we re-use it in
  26. // the case that the old access token
  27. // expired.
  28. if (refreshToken == null) {
  29. refreshToken = createRefreshToken(authentication);
  30. }
  31. // But the refresh token itself might need to be re-issued if it has
  32. // expired.
  33. else if (refreshToken instanceof ExpiringOAuth2RefreshToken) {
  34. ExpiringOAuth2RefreshToken expiring = (ExpiringOAuth2RefreshToken) refreshToken;
  35. if (System.currentTimeMillis() > expiring.getExpiration().getTime()) {
  36. refreshToken = createRefreshToken(authentication);
  37. }
  38. }
  39. // 第一次创建access_token,并且存储到数据库中
  40. OAuth2AccessToken accessToken = createAccessToken(authentication, refreshToken);
  41. tokenStore.storeAccessToken(accessToken, authentication);
  42. // In case it was modified
  43. refreshToken = accessToken.getRefreshToken();
  44. if (refreshToken != null) {
  45. tokenStore.storeRefreshToken(refreshToken, authentication);
  46. }
  47. return accessToken;
  48. }

2.4.2 RemoteTokenServices

当授权服务和资源服务不在一个应用程序的时候,资源服务可以把传递来的access_token递交给授权服务的/oauth/check_token进行验证,而资源服务自己无需去连接数据库验证access_token,这时就用到了RemoteTokenServices。

loadAuthentication方法,设置head表头Authorization 存储clientId和clientSecret信息,请求参数包含access_token字符串,向AuthServer的CheckTokenEndpoint (/oauth/check_token)发送请求,返回验证结果map(包含clientId,grantType,scope,username等信息),拼接成OAuth2Authentication。

重要!!!

AuthServer需要配置checkTokenAcess,否则默认为“denyAll()”,请求访问/oauth/check_token会提示没权限。

  1. @Override
  2. public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
  3. oauthServer.realm(QQ_RESOURCE_ID).allowFormAuthenticationForClients();
  4. // 访问/oauth/check_token 需要client验证
  5. oauthServer.checkTokenAccess("isAuthenticated()");、
  6. // 也可配置访问/oauth/check_token无需验证
  7. // oauthServer.checkTokenAccess("permitAll()");
  8. }

不支持readAccessToken方法。

  1. public class RemoteTokenServices implements ResourceServerTokenServices {
  2. protected final Log logger = LogFactory.getLog(getClass());
  3. private RestOperations restTemplate;
  4. private String checkTokenEndpointUrl;
  5. private String clientId;
  6. private String clientSecret;
  7. private String tokenName = "token";
  8. private AccessTokenConverter tokenConverter = new DefaultAccessTokenConverter();
  9. @Override
  10. public OAuth2Authentication loadAuthentication(String accessToken) throws AuthenticationException, InvalidTokenException {
  11. MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
  12. formData.add(tokenName, accessToken);
  13. HttpHeaders headers = new HttpHeaders();
  14. headers.set("Authorization", getAuthorizationHeader(clientId, clientSecret));
  15. Map<String, Object> map = postForMap(checkTokenEndpointUrl, formData, headers);
  16. if (map.containsKey("error")) {
  17. logger.debug("check_token returned error: " + map.get("error"));
  18. throw new InvalidTokenException(accessToken);
  19. }
  20. Assert.state(map.containsKey("client_id"), "Client id must be present in response from auth server");
  21. return tokenConverter.extractAuthentication(map);
  22. }
  23. @Override
  24. public OAuth2AccessToken readAccessToken(String accessToken) {
  25. throw new UnsupportedOperationException("Not supported: read access token");
  26. }
  27. }

3. ClientDetails   ClientDetailsService

这两个概念简单的有些可怕,完全就是UserDetails和UserDetailsService的翻版。一个是对应user,一个是对应client。

client需要事先注册到授权服务器,这样授权服务器会根据client的授权请求获取clientId,secret等信息,进行验证后返回token。

3.1 ClientDetails

client的信息,存于授权服务器端,这样只需要知道客户端的clientId,就可以获取到客户端能访问哪些资源,是否需要密码,是否限制了scope,拥有的权限等等。

  1. public interface ClientDetails extends Serializable {
  2. String getClientId();
  3. // client能访问的资源id
  4. Set<String> getResourceIds();
  5. // 验证client是否需要密码
  6. boolean isSecretRequired();
  7. String getClientSecret();
  8. // client是否限制了scope
  9. boolean isScoped();
  10. // scope集合
  11. Set<String> getScope();
  12. // 根据哪些grantType验证通过client
  13. Set<String> getAuthorizedGrantTypes();
  14. // 注册成功后跳转的uri
  15. Set<String> getRegisteredRedirectUri();
  16. // client拥有的权限
  17. Collection<GrantedAuthority> getAuthorities();
  18. // client的token时效
  19. Integer getAccessTokenValiditySeconds();
  20. // client的refreshToken时效
  21. Integer getRefreshTokenValiditySeconds();
  22. // true:默认自动授权;false:需要用户确定才能授权
  23. boolean isAutoApprove(String scope);
  24. // 额外的信息
  25. Map<String, Object> getAdditionalInformation();
  26. }

3.2 ClientDetailsService

根据clientId获取clientDetails

  1. public interface ClientDetailsService {
  2. /**
  3. * Load a client by the client id. This method must not return null.
  4. *
  5. * @param clientId The client id.
  6. * @return The client details (never null).
  7. * @throws ClientRegistrationException If the client account is locked, expired, disabled, or invalid for any other reason.
  8. */
  9. ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException;
  10. }

有两个子类 InMemoryClientDetailsService(内存)   和   JdbcClientDetailsService(数据库,OAUTH_CLIENT_DETAILS、OAUTH_CLIENT_TOKEN表等)。说白了就是一个是把ClientDetails存内存里,一个存数据库里(oauth_client_details表)。

一般在AuthServer中配置ClientDetailsServiceConfigurer

1、配置JdbcDetailsService 

  1. @Bean
  2. public ClientDetailsService clientDetails() {
  3. return new JdbcClientDetailsService(dataSource);
  4. }
  5. /**
  6. * 配置客户端 a configurer that defines the client details service.
  7. * Client details can be initialized, or you can just refer to an existing store.
  8. */
  9. @Override
  10. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  11. clients.withClientDetails(clientDetails());
  12. }

2、配置InMemoryClientDetailsService

  1. @Override
  2. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  3. // @formatter:off
  4. clients.inMemory().withClient("aiqiyi")
  5. .resourceIds(QQ_RESOURCE_ID)
  6. .authorizedGrantTypes("authorization_code", "refresh_token", "implicit")
  7. .authorities("ROLE_CLIENT")
  8. // , "get_fanslist"
  9. .scopes("get_fanslist")
  10. .secret("secret")
  11. .redirectUris("http://localhost:8081/aiqiyi/qq/redirect")
  12. .autoApprove(true)
  13. .autoApprove("get_user_info")
  14. .and()
  15. .withClient("youku")
  16. .resourceIds(QQ_RESOURCE_ID)
  17. .authorizedGrantTypes("authorization_code", "refresh_token", "implicit")
  18. .authorities("ROLE_CLIENT")
  19. .scopes("get_user_info", "get_fanslist")
  20. .secret("secret")
  21. .redirectUris("http://localhost:8082/youku/qq/redirect");
  22. // @formatter:on
  23. }

3.3 ClientDetailsServiceBuilder

创建InMemoryClientDetailsService或者JdbcClientDetailsService,有内部类ClientBuilder。

  1. public class ClientDetailsServiceBuilder<B extends ClientDetailsServiceBuilder<B>> extends
  2. SecurityConfigurerAdapter<ClientDetailsService, B> implements SecurityBuilder<ClientDetailsService> {
  3. private List<ClientBuilder> clientBuilders = new ArrayList<ClientBuilder>();
  4. public InMemoryClientDetailsServiceBuilder inMemory() throws Exception {
  5. return new InMemoryClientDetailsServiceBuilder();
  6. }
  7. public JdbcClientDetailsServiceBuilder jdbc() throws Exception {
  8. return new JdbcClientDetailsServiceBuilder();
  9. }
  10. @SuppressWarnings("rawtypes")
  11. public ClientDetailsServiceBuilder<?> clients(final ClientDetailsService clientDetailsService) throws Exception {
  12. return new ClientDetailsServiceBuilder() {
  13. @Override
  14. public ClientDetailsService build() throws Exception {
  15. return clientDetailsService;
  16. }
  17. };
  18. }
  19. // clients.inMemory().withClient("clientId").scopes().secret()...
  20. public ClientBuilder withClient(String clientId) {
  21. ClientBuilder clientBuilder = new ClientBuilder(clientId);
  22. this.clientBuilders.add(clientBuilder);
  23. return clientBuilder;
  24. }
  25. @Override
  26. public ClientDetailsService build() throws Exception {
  27. for (ClientBuilder clientDetailsBldr : clientBuilders) {
  28. addClient(clientDetailsBldr.clientId, clientDetailsBldr.build());
  29. }
  30. return performBuild();
  31. }
  32. protected void addClient(String clientId, ClientDetails build) {
  33. }
  34. protected ClientDetailsService performBuild() {
  35. throw new UnsupportedOperationException("Cannot build client services (maybe use inMemory() or jdbc()).");
  36. }
  37. public final class ClientBuilder {
  38. // ...
  39. public ClientDetailsServiceBuilder<B> and() {
  40. return ClientDetailsServiceBuilder.this;
  41. }
  42. }
  43. }

4. 资源服务器配置  ResourceServerConfigurerAdapter

配置哪些路径需要认证后才能访问,哪些不需要。自然就联想到了HttpSecurity(配置HttpSecurity就相当于配置了不同uri对应的filters)。

Spring Security中我们是这样配置WebSecurityConfigurerAdapter的。

  1. @Configuration
  2. @EnableWebSecurity
  3. public class SecurityConfig extends WebSecurityConfigurerAdapter
  4. {
  5. @Override
  6. protected void configure(HttpSecurity http) throws Exception {
  7. http
  8. .csrf().disable()
  9. .authorizeRequests()
  10. .anyRequest().authenticated()//所有请求必须登陆后访问
  11. .and().httpBasic()
  12. .and()
  13. .formLogin()
  14. .loginPage("/login")
  15. .defaultSuccessUrl("/index")
  16. .failureUrl("/login?error")
  17. .permitAll()//登录界面,错误界面可以直接访问
  18. .and()
  19. .logout().logoutUrl("/logout").logoutSuccessUrl("/login")
  20. .permitAll().and().rememberMe();//注销请求可直接访问
  21. }
  22. @Override
  23. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  24. auth.inMemoryAuthentication().withUser("user").password("password").roles("USER").and()
  25. .withUser("admin").password("password").roles("USER", "ADMIN");
  26. }
  27. }

作为资源服务器ResourceServerConfigurerAdapter,需要和@EnableResourceServer搭配,然后和上面一样需配置HttpSecurity就好了。还能配置ResourceServerSecurityConfigurer,设置tokenService等

  1. /**
  2. * 配置资源服务器
  3. */
  4. @Configuration
  5. @EnableResourceServer
  6. protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  7. @Autowired
  8. private CustomAuthenticationEntryPoint customAuthenticationEntryPoint;
  9. @Autowired
  10. private CustomLogoutSuccessHandler customLogoutSuccessHandler;
  11. @Override
  12. public void configure(HttpSecurity http) throws Exception {
  13. http
  14. .exceptionHandling()
  15. .authenticationEntryPoint(customAuthenticationEntryPoint)
  16. .and()
  17. .logout()
  18. .logoutUrl("/oauth/logout")
  19. .logoutSuccessHandler(customLogoutSuccessHandler)
  20. .and()
  21. .authorizeRequests()
  22. // hello路径允许直接访问
  23. .antMatchers("/hello/").permitAll()
  24. // secure路径需要验证后才能访问
  25. .antMatchers("/secure/**").authenticated();
  26. }
  27. // 远程连接authServer服务
  28. @Autowired
  29. public RemoteTokenServices remoteTokenServices;
  30. @Override
  31. public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
  32. resources.tokenServices(remoteTokenServices);
  33. }
  34. }

5. 授权服务器配置    AuthorizationServerConfigurerAdapter

注册client信息,可以同时配置多个不同类型的client。

  1. /**
  2. * 配置认证服务器 @EnableAuthorizationServer自动注册到spring context中
  3. */
  4. @EnableAuthorizationServer
  5. protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter implements EnvironmentAware {
  6. private static final String ENV_OAUTH = "authentication.oauth.";
  7. private static final String PROP_CLIENTID = "clientid";
  8. private static final String PROP_SECRET = "secret";
  9. private static final String PROP_TOKEN_VALIDITY_SECONDS = "tokenValidityInSeconds";
  10. private RelaxedPropertyResolver propertyResolver;
  11. @Autowired
  12. private DataSource dataSource;
  13. @Bean
  14. public TokenStore tokenStore() {
  15. return new JdbcTokenStore(dataSource);
  16. }
  17. @Autowired
  18. @Qualifier("authenticationManagerBean")
  19. private AuthenticationManager authenticationManager;
  20. /**
  21. * 可以设置tokenStore,tokenGranter,authenticationManager,requestFactory等接口使用什么继承类,但一般沿用默认的就好了
  22. * 如果使用的是密码方式授权,则必须设置authenticationManager
  23. */
  24. @Override
  25. public void configure(AuthorizationServerEndpointsConfigurer endpoints)
  26. throws Exception {
  27. endpoints
  28. .tokenStore(tokenStore())
  29. .authenticationManager(authenticationManager);
  30. }
  31. /**
  32. * 注册clients到授权服务器,这里是注册到内存中,且配置了scopes,authorities等信息
  33. */
  34. @Override
  35. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  36. clients
  37. .inMemory()
  38. .withClient(propertyResolver.getProperty(PROP_CLIENTID))
  39. .scopes("read", "write")
  40. .authorities(Authorities.ROLE_ADMIN.name(), Authorities.ROLE_USER.name())
  41. .authorizedGrantTypes("password", "refresh_token")
  42. .secret(propertyResolver.getProperty(PROP_SECRET))
  43. // 给客户端的token时效为1800秒
  44. .accessTokenValiditySeconds(propertyResolver.getProperty(PROP_TOKEN_VALIDITY_SECONDS, Integer.class, 1800));
  45. }
  46. @Override
  47. public void setEnvironment(Environment environment) {
  48. this.propertyResolver = new RelaxedPropertyResolver(environment, ENV_OAUTH);
  49. }
  50. }

6.TokenEndPoint,AuthorizationEndPoint,CheckTokenEndPoint

6.1 TokenEndPoint

客户端post请求"/oauth/token",验证用户信息并获取OAuth2AccessToken,必须先经过client验证。这一步的最终目的是存储OAuth2AccessToken+OAuth2Authentication并返回OAuth2AccessToken。

  1. @RequestMapping(value = "/oauth/token", method=RequestMethod.POST)
  2. public ResponseEntity<OAuth2AccessToken> postAccessToken(Principal principal, @RequestParam
  3. Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
  4. if (!(principal instanceof Authentication)) {
  5. throw new InsufficientAuthenticationException(
  6. "There is no client authentication. Try adding an appropriate authentication filter.");
  7. }
  8. String clientId = getClientId(principal);
  9. ClientDetails authenticatedClient = getClientDetailsService().loadClientByClientId(clientId);
  10. TokenRequest tokenRequest = getOAuth2RequestFactory().createTokenRequest(parameters, authenticatedClient);
  11. ...
  12. // AuthorizationServerEndpointsConfigurer
  13. OAuth2AccessToken token = getTokenGranter().grant(tokenRequest.getGrantType(), tokenRequest);
  14. if (token == null) {
  15. throw new UnsupportedGrantTypeException("Unsupported grant type: " + tokenRequest.getGrantType());
  16. }
  17. return getResponse(token);
  18. }

6.2 AuthorizationEndPoint

这个一般只适用于authorization code模式,客户端请求authorization server中的/oauth/authorize(请求前先得登录oauth server获得authentication),验证client信息后根据redirect_uri请求重定向回client,同时带上code值。client附带code值再次向/oauth/token请求,返回accesstoken。

具体流程将在下章中介绍,这里只引出相关概念。

  1. @RequestMapping(value = "/oauth/authorize")
  2. public ModelAndView authorize(Map<String, Object> model, @RequestParam Map<String, String> parameters,
  3. SessionStatus sessionStatus, Principal principal) {
  4. // Pull out the authorization request first, using the OAuth2RequestFactory. All further logic should
  5. // query off of the authorization request instead of referring back to the parameters map. The contents of the
  6. // parameters map will be stored without change in the AuthorizationRequest object once it is created.
  7. AuthorizationRequest authorizationRequest = getOAuth2RequestFactory().createAuthorizationRequest(parameters);
  8. Set<String> responseTypes = authorizationRequest.getResponseTypes();
  9. if (!responseTypes.contains("token") && !responseTypes.contains("code")) {
  10. throw new UnsupportedResponseTypeException("Unsupported response types: " + responseTypes);
  11. }
  12. if (authorizationRequest.getClientId() == null) {
  13. throw new InvalidClientException("A client id must be provided");
  14. }
  15. try {
  16. if (!(principal instanceof Authentication) || !((Authentication) principal).isAuthenticated()) {
  17. throw new InsufficientAuthenticationException(
  18. "User must be authenticated with Spring Security before authorization can be completed.");
  19. }
  20. ClientDetails client = getClientDetailsService().loadClientByClientId(authorizationRequest.getClientId());
  21. // The resolved redirect URI is either the redirect_uri from the parameters or the one from
  22. // clientDetails. Either way we need to store it on the AuthorizationRequest.
  23. String redirectUriParameter = authorizationRequest.getRequestParameters().get(OAuth2Utils.REDIRECT_URI);
  24. String resolvedRedirect = redirectResolver.resolveRedirect(redirectUriParameter, client);
  25. if (!StringUtils.hasText(resolvedRedirect)) {
  26. throw new RedirectMismatchException(
  27. "A redirectUri must be either supplied or preconfigured in the ClientDetails");
  28. }
  29. authorizationRequest.setRedirectUri(resolvedRedirect);
  30. // We intentionally only validate the parameters requested by the client (ignoring any data that may have
  31. // been added to the request by the manager).
  32. oauth2RequestValidator.validateScope(authorizationRequest, client);
  33. // Some systems may allow for approval decisions to be remembered or approved by default. Check for
  34. // such logic here, and set the approved flag on the authorization request accordingly.
  35. authorizationRequest = userApprovalHandler.checkForPreApproval(authorizationRequest,
  36. (Authentication) principal);
  37. // TODO: is this call necessary?
  38. boolean approved = userApprovalHandler.isApproved(authorizationRequest, (Authentication) principal);
  39. authorizationRequest.setApproved(approved);
  40. // Validation is all done, so we can check for auto approval...
  41. if (authorizationRequest.isApproved()) {
  42. if (responseTypes.contains("token")) {
  43. return getImplicitGrantResponse(authorizationRequest);
  44. }
  45. if (responseTypes.contains("code")) {
  46. // 生成code值并返回
  47. return new ModelAndView(getAuthorizationCodeResponse(authorizationRequest,
  48. (Authentication) principal));
  49. }
  50. }
  51. // Place auth request into the model so that it is stored in the session
  52. // for approveOrDeny to use. That way we make sure that auth request comes from the session,
  53. // so any auth request parameters passed to approveOrDeny will be ignored and retrieved from the session.
  54. model.put("authorizationRequest", authorizationRequest);
  55. return getUserApprovalPageResponse(model, authorizationRequest, (Authentication) principal);
  56. }
  57. catch (RuntimeException e) {
  58. sessionStatus.setComplete();
  59. throw e;
  60. }
  61. }

6.3 CheckTokenEndpoint

当采用RemoteTokenServices时,resouceServer无法自行验证access_token字符串是否正确,遂递交给另一个应用程序中的authserver里CheckTokenEndpoint(/oauth/check_token)进行检验,检验结果返回给resourceServer。

  1. @RequestMapping(value = "/oauth/check_token")
  2. @ResponseBody
  3. public Map<String, ?> checkToken(@RequestParam("token") String value) {
  4. OAuth2AccessToken token = resourceServerTokenServices.readAccessToken(value);
  5. if (token == null) {
  6. throw new InvalidTokenException("Token was not recognised");
  7. }
  8. if (token.isExpired()) {
  9. throw new InvalidTokenException("Token has expired");
  10. }
  11. OAuth2Authentication authentication = resourceServerTokenServices.loadAuthentication(token.getValue());
  12. Map<String, ?> response = accessTokenConverter.convertAccessToken(token, authentication);
  13. return response;
  14. }

7.总结

复习下这章讲到的知识点。

  • 四大角色:ResouceServer   AuthorizationServer    client     user
  • OAuth2AccessToken  OAuth2Authentiaction
  • OAuth2Request    TokenRequest   AuthorizationRequest
  • TokenGranter   TokenStore   TokenExtractor   DefaultTokenServices    RemoteTokenServices
  • ResourceServerConfigurerAdapter      AuthorizationServerConfigurerAdapter
  • TokenEndPoint(/oauth/token)    AuthorizationEndPoint(/oauth/authorize)   CheckTokenEndpoint(/oauth/check_token)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/84015
推荐阅读
相关标签
  

闽ICP备14008679号