Spring Security OAuth 2 Redisを使用してtokenキー値の詳細を格納


1.Spring Security OAuth 2はtoken値を格納する方式が複数あり、すべての実装方式はTokenStoreインタフェースを実現している
  • InMemoryTokenStore:tokenは、自機のメモリに
  • 格納
  • JdbcTokenStore:tokenデータベースに格納
  • JwtTokenStore:tokenはメディアに保存されません
  • RedisTokenStore:token Redisデータベースに格納される
  • 2.RedisTokenStore実装クラスがredisに格納しているkeyを見て、ソースコードを貼り付けます.
        private static final String ACCESS = "access:";
        private static final String AUTH_TO_ACCESS = "auth_to_access:";
        private static final String AUTH = "auth:";
        private static final String REFRESH_AUTH = "refresh_auth:";
        private static final String ACCESS_TO_REFRESH = "access_to_refresh:";
        private static final String REFRESH = "refresh:";
        private static final String REFRESH_TO_ACCESS = "refresh_to_access:";
        private static final String CLIENT_ID_TO_ACCESS = "client_id_to_access:";
        private static final String UNAME_TO_ACCESS = "uname_to_access:";
    

    この例ではpassword、refresh_を使用します.tokenモードでは、Redisキャッシュに9つのキー値ペアが格納され、そのうち5つがaccess_token関連、4個とrefresh_token相関;
  • access_token関連アクセス:(OAuth 2 AccessToken)、auth:(OAuth 2 Authentication)、auth_to_access:(OAuth2AccessToken)、client_id_to_access:(OAuth2AccessToken)、uname_to_access:(OAuth2AccessToken)
  • refresh_token関連refresh:(OAuth 2 RefreshToken)、refresh_auth:(OAuth2Authentication)、access_to_refresh(refresh_token):、refresh_to_access:(refresh_token)

  • 3.RedisTokenStoreのソースコード(ソースコードは貼らない)を表示することで、keyごとに格納されているデータを理解する
  • access:に格納キーはaccess:be 171 b 573 f 5 a 496 ca 601 b 32 b 1360 fe 84、値はOAuth 2 AccessTokenオブジェクトのシーケンス化後の値
  • である.
  • キーはaccess:+access_token
  • の値の例は、
    {
            "access_token": "12833d6c89fb4ea58cbe7b6ada5de7b5",
            "token_type": "bearer",
            "refresh_token": "357304ee0a404700b3e65d547713011b",
            "expires_in": 898,
            "scope": "test"
        }
    
  • です.
  • auth_to_アクセス:に格納されているキーはauth_to_access:a 994 f 2 a 9 a 61186 f 32870 e 32 d 72 a 38 d 21、値はOAuth 2 AccessTokenシーケンス化後の値
  • である.
  • キーはauth_to_access:+ username、client_id、scopeの3つのMD 5暗号化後の値
  • の値の例は、
    {
            "access_token": "12833d6c89fb4ea58cbe7b6ada5de7b5",
            "token_type": "bearer",
            "refresh_token": "357304ee0a404700b3e65d547713011b",
            "expires_in": 898,
            "scope": "test"
        }
    
  • です.
  • auth:に格納キーはauth:be 171 b 573 f 5 a 496 ca 601 b 32 b 1360 fe 84、値はOAuth 2 Authenticationオブジェクトのシーケンス化後の値
  • である
  • キーはauth:+access_token値
  • の値の例は、
  • です.
    {
        "authorities": [
            {
                "authority": "ROLE"
            }
        ],
        "details": {
            "remoteAddress": "0:0:0:0:0:0:0:1",
            "sessionId": null,
            "tokenValue": "dfec9f18e161408dbf66b85b94401d7f",
            "tokenType": "Bearer",
            "decodedDetails": null
        },
        "authenticated": true,
        "userAuthentication": {
            "authorities": [
                {
                    "authority": "ROLE"
                }
            ],
            "details": {
                "grant_type": "password",
                "username": "user",
                "scope": "test"
            },
            "authenticated": true,
            "principal": {
                "password": null,
                "username": "user",
                "authorities": [
                    {
                        "authority": "ROLE"
                    }
                ],
                "accountNonExpired": true,
                "accountNonLocked": true,
                "credentialsNonExpired": true,
                "enabled": true
            },
            "credentials": null,
            "name": "user"
        },
        "credentials": "",
        "principal": {
            "password": null,
            "username": "user",
            "authorities": [
                {
                    "authority": "ROLE"
                }
            ],
            "accountNonExpired": true,
            "accountNonLocked": true,
            "credentialsNonExpired": true,
            "enabled": true
        },
        "oauth2Request": {
            "clientId": "client_password",
            "scope": [
                "test"
            ],
            "requestParameters": {
                "grant_type": "password",
                "scope": "test",
                "username": "user"
            },
            "resourceIds": [
                "resource_password_id"
            ],
            "authorities": [],
            "approved": true,
            "refresh": false,
            "redirectUri": null,
            "responseTypes": [],
            "extensions": {},
            "grantType": "password",
            "refreshTokenRequest": null
        },
        "clientOnly": false,
        "name": "user"
    }
    
  • refresh_auth:に格納されているのはrefresh_auth:d 0017 ce 6 db 6441 d 1 b 87 a 0 a 2804 d 1434 b、値はOAuth 2 Authenticationシーケンス化後の値
  • である.
  • キーYes:refresh_auth:+refresh_token値
  • の値の例は、
  • です.
    {
        "authorities": [
            {
                "authority": "ROLE"
            }
        ],
        "details": {
            "remoteAddress": "0:0:0:0:0:0:0:1",
            "sessionId": null,
            "tokenValue": "dfec9f18e161408dbf66b85b94401d7f",
            "tokenType": "Bearer",
            "decodedDetails": null
        },
        "authenticated": true,
        "userAuthentication": {
            "authorities": [
                {
                    "authority": "ROLE"
                }
            ],
            "details": {
                "grant_type": "password",
                "username": "user",
                "scope": "test"
            },
            "authenticated": true,
            "principal": {
                "password": null,
                "username": "user",
                "authorities": [
                    {
                        "authority": "ROLE"
                    }
                ],
                "accountNonExpired": true,
                "accountNonLocked": true,
                "credentialsNonExpired": true,
                "enabled": true
            },
            "credentials": null,
            "name": "user"
        },
        "credentials": "",
        "principal": {
            "password": null,
            "username": "user",
            "authorities": [
                {
                    "authority": "ROLE"
                }
            ],
            "accountNonExpired": true,
            "accountNonLocked": true,
            "credentialsNonExpired": true,
            "enabled": true
        },
        "oauth2Request": {
            "clientId": "client_password",
            "scope": [
                "test"
            ],
            "requestParameters": {
                "grant_type": "password",
                "scope": "test",
                "username": "user"
            },
            "resourceIds": [
                "resource_password_id"
            ],
            "authorities": [],
            "approved": true,
            "refresh": false,
            "redirectUri": null,
            "responseTypes": [],
            "extensions": {},
            "grantType": "password",
            "refreshTokenRequest": null
        },
        "clientOnly": false,
        "name": "user"
    }
    
  • access_to_refresh:に格納されているのはaccessです.to_refresh:c 90 cab 28971948 d 2 a 85 ca 2 ae 814641 ed、値はrefresh_token値
  • キーはaccessです.to_refresh:+refresh_token値
  • 値はrefresh_token値
  • refresh:にはrefresh:d 0017 ce 6 db 6441 d 1 b 87 a 0 a 2804 d 1434 bが格納され、値はOAuth 2 RefreshTokenオブジェクトがシーケンス化された値
  • である.
  • キーはrefresh:+refresh_token値
  • の値の例は、
  • です.
     {
            "access_token": "dfec9f18e161408dbf66b85b94401d7f",
            "token_type": "bearer",
            "refresh_token": "8bcd9cfb04a3445e8933c788b2673a89",
            "expires_in": 898,
            "scope": "test"
        }
    
  • refresh_to_アクセス:に格納されている値はrefresh_です.to_access:d 0017 ce 6 db 6441 d 1 b 87 a 0 a 2804 d 1434 b、値はrefresh_token値
  • キーはrefresh_to_access:+refresh_token値
  • の値の例は、
  • です.
    be171b573f5a496ca601b32b1360fe84
    
  • client_id_to_アクセス:に格納されている値はclient_id_to_access:client_password、値はOAuth 2 AccessTokenシーケンス化後の値
  • キーはclient_id_to_access:+clientId
  • の値の例は、
  • です.
    {
            "access_token": "dfec9f18e161408dbf66b85b94401d7f",
            "token_type": "bearer",
            "refresh_token": "8bcd9cfb04a3445e8933c788b2673a89",
            "expires_in": 898,
            "scope": "test"
        }
    
  • uname_to_アクセス:に格納されているキーはunameです.to_access:client_password:user,値はOAuth 2 AccessTokenオブジェクトのシーケンス化後の値
  • である.
  • キーYes:uname_to_アクセス:+clientid+ユーザー名
  • の値の例は、
  • です.
    {
            "access_token": "dfec9f18e161408dbf66b85b94401d7f",
            "token_type": "bearer",
            "refresh_token": "8bcd9cfb04a3445e8933c788b2673a89",
            "expires_in": 898,
            "scope": "test"
        }
    

    GitHubソース:https://github.com/mingyang66/spring-parent/edit/master/spring-security-oauth2-server-redis-service/README.md