(5) SpringCloud+Security+Oauth2 -- ResourceServerTokenServices をカスタマイズして oauth2 のパフォーマンスを最適化する

パフォーマンスのボトルネック

前の分析を通じて, トークンを検証するときに oath2 がトークン検証要求をクライアントに送信することがわかっています. このようにして, ResourceServerTokenServices をカスタマイズして、会造成认证中心的负载压力过大,成为造成整个系统瓶颈的关键点
ここに画像の説明を挿入
ここに画像の説明を挿入
トークンストアから認証を直接取得し
、それをセキュリティコンテキストにロードすることができます.

2 つのカスタム ResourceServerTokenServices

@RequiredArgsConstructor
@Component
@Primary
public class LocalResourceTokenServices implements ResourceServerTokenServices {
    
    
    private final TokenStore tokenStore;

    @Override
    public OAuth2Authentication loadAuthentication(String accessToken) throws AuthenticationException, InvalidTokenException {
    
    
        OAuth2Authentication oAuth2Authentication = tokenStore.readAuthentication(accessToken);
        if (Objects.isNull(oAuth2Authentication)){
    
    
            return null;
        }
        oAuth2Authentication.setAuthenticated(true);
        return oAuth2Authentication;
    }

    @Override
    public OAuth2AccessToken readAccessToken(String accessToken) {
    
    
        throw new UnsupportedOperationException("Not supported: read access token");
    }
}

@Primaryトークンの検証中にこの実装を強制的に使用するには、保証を追加する必要があることに注意してください。

おすすめ

転載: blog.csdn.net/Instanceztt/article/details/128202066