① 默认Token 有效期说明

  1. // 默认刷新token 的有效期
  2. private int refreshTokenValiditySeconds = 60 * 60 * 24 * 30; // default 30 days.
  3. // 默认token 的有效期
  4. private int accessTokenValiditySeconds = 60 * 60 * 12; // default 12 hours.

② 修改 Token 有效期

:::info 每个终端配置为空,取默认的时间设置 刷新token 30天,令牌12小时。
前端使用的客户端是pig,设置令牌失效等。 ::: pig token有效期及个性化 - 图1

个性化Token 目的

  • 默认通过调用 /oauth/token 返回的报文格式包含以下参数
  1. {
  2. "access_token": "e6669cdf-b6cd-43fe-af5c-f91a65041382",
  3. "token_type": "bearer",
  4. "refresh_token": "da91294d-446c-4a89-bdcf-88aee15a75e8",
  5. "expires_in": 43199,
  6. "scope": "server"
  7. }

并没包含用户的业务信息比如用户信息、租户信息等。

  • 扩展生成包含业务信息(如下),避免系统多次调用,直接可以通过认证接口获取到用户信息等,大大提高系统性能
  1. {
  2. "access_token":"a6f3b6d6-93e6-4eb8-a97d-3ae72240a7b0",
  3. "token_type":"bearer",
  4. "refresh_token":"710ab162-a482-41cd-8bad-26456af38e4f",
  5. "expires_in":42396,
  6. "scope":"server",
  7. "tenant_id":1,
  8. "license":"made by pigx",
  9. "dept_id":1,
  10. "user_id":1,
  11. "username":"admin"
  12. }

密码模式生成Token 源码解析

pig token有效期及个性化 - 图2

  1. 主页参考红框部分
  • ResourceOwnerPasswordTokenGranter (密码模式)根据用户的请求信息,进行认证得到当前用户上下文信息

    1. protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
    2. Map<String, String> parameters = new LinkedHashMap<String, String>(tokenRequest.getRequestParameters());
    3. String username = parameters.get("username");
    4. String password = parameters.get("password");
    5. // Protect from downstream leaks of password
    6. parameters.remove("password");
    7. Authentication userAuth = new UsernamePasswordAuthenticationToken(username, password);
    8. ((AbstractAuthenticationToken) userAuth).setDetails(parameters);
    9. userAuth = authenticationManager.authenticate(userAuth);
    10. OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);
    11. return new OAuth2Authentication(storedOAuth2Request, userAuth);
    12. }
  • 然后调用AbstractTokenGranter.getAccessToken() 获取OAuth2AccessToken

    1. protected OAuth2AccessToken getAccessToken(ClientDetails client, TokenRequest tokenRequest) {
    2. return tokenServices.createAccessToken(getOAuth2Authentication(client, tokenRequest));
    3. }
  • 默认使用DefaultTokenServices来获取token

    1. public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException {
    2. ... 一系列判断 ,合法性、是否过期等判断
    3. OAuth2AccessToken accessToken = createAccessToken(authentication, refreshToken);
    4. tokenStore.storeAccessToken(accessToken, authentication);
    5. // In case it was modified
    6. refreshToken = accessToken.getRefreshToken();
    7. if (refreshToken != null) {
    8. tokenStore.storeRefreshToken(refreshToken, authentication);
    9. }
    10. return accessToken;
    11. }
  • createAccessToken 核心逻辑 ```java // 默认刷新token 的有效期 private int refreshTokenValiditySeconds = 60 60 24 30; // default 30 days. // 默认token 的有效期 private int accessTokenValiditySeconds = 60 60 * 12; // default 12 hours.

private OAuth2AccessToken createAccessToken(OAuth2Authentication authentication, OAuth2RefreshToken refreshToken) { DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(uuid); token.setExpiration(Date) token.setRefreshToken(refreshToken); token.setScope(authentication.getOAuth2Request().getScope()); return accessTokenEnhancer != null ? accessTokenEnhancer.enhance(token, authentication) : token; }

  1. - 如上代码,在拼装好token对象后会调用认证服务器配置TokenEnhancer( 增强器) 来对默认的token进行增强。
  2. - TokenEnhancer.enhance 通过上下文中的用户信息来个性化Token
  3. ```java
  4. public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
  5. final Map<String, Object> additionalInfo = new HashMap<>(8);
  6. PigxUser pigxUser = (PigxUser) authentication.getUserAuthentication().getPrincipal();
  7. additionalInfo.put("user_id", pigxUser.getId());
  8. additionalInfo.put("username", pigxUser.getUsername());
  9. additionalInfo.put("dept_id", pigxUser.getDeptId());
  10. additionalInfo.put("tenant_id", pigxUser.getTenantId());
  11. additionalInfo.put("license", SecurityConstants.PIGX_LICENSE);
  12. ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
  13. return accessToken;
  14. }

❤ 问题咨询

手势点击蓝字求关注简约风动态引导关注__2022-09-07+23_18_38.gif