パフォーマンスのボトルネック
前の分析を通じて, トークンを検証するときに 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
トークンの検証中にこの実装を強制的に使用するには、保証を追加する必要があることに注意してください。