我的项目将redis会话与springboot会话和spring security 5.1.10]结合使用。我只是迁移了旧的oauth2实现。以前,当我重新启动应用程序时,我仍然具有access_token和refresh_token。通过此实现,用户已登录,但是我松开了AuthorizedClients,因此loadAuthorizedClient函数在重启后返回null。同样在生产中,我们有许多具有相同应用程序的容器。是否有任何springboot stardard方法来实现这一目标?例如注册一些bean之类的东西。
application.yml
... session: store-type: redis redis: namespace: spring:session:${spring.application.name} redis: host: ${redissession.host} password: ${redissession.password} port: ${redissession.port} security: oauth2: client: registration: biocryptology: provider: example client-id: client client-secret: xxx client-authentication-method: basic authorization-grant-type: authorization_code redirect-uri-template: "{baseUrl}/login" scope: - openid provider: example: issuer-uri: https://.... ...
Controller.java
@Autowired private OAuth2AuthorizedClientService clientService; @GetMapping("/user") public String getOidcUserPrincipal() throws InvalidSessionException { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (!(authentication.getPrincipal() instanceof OidcUser)) { throw new InvalidSessionException(); } OidcUser principal = ((OidcUser) authentication.getPrincipal()); LOG.info("oidc: {}", principal.getName()); OAuth2AuthenticationToken oauth2Token = (OAuth2AuthenticationToken) authentication; LOG.info("authentication: {}", oauth2Token); OAuth2AuthorizedClient client = clientService .loadAuthorizedClient(oauth2Token.getAuthorizedClientRegistrationId(), authentication.getName()); LOG.info("client: {}", client); return "logged"; }
目标是跨容器获取access_token和refresh_token,是否有其他没有OAuth2AuthorizedClientService的方式?
编辑:
<!-- Spring -->
<spring-cloud.version>Greenwich.SR5</spring-cloud.version>
我的项目将redis会话与springboot会话和spring security 5.1.10一起使用。我只是迁移了旧的oauth2实现。以前,当我重新启动应用程序时,我仍然具有access_token和...
注册一个bean可以解决这个问题,它可以将其保存在会话中,但是OAuth2AuthorizedClientService
在每种情况下都会崩溃,因此需要一种解决方法,可以直接在会话中搜索,也可以使用OAuth2AuthorizedClientRepository
自动装配: