【问题标题】:Redirection issue in Oauth2Oauth2 中的重定向问题
【发布时间】:2022-01-18 01:05:55
【问题描述】:

我正在使用 Oauth2 开发简单的微服务,并使用 keycloak 作为授权服务器。 以下是在点击 url 时返回专辑列表的代码: - http://localhost:8087/albums

@Controller
public class AlbumController {
    @Autowired
    OAuth2AuthorizedClientService oauthService;
    
    @Autowired
    RestTemplate restTemplate;
    
    @GetMapping("/albums")
    public String getAlbums(Model model,@AuthenticationPrincipal OidcUser principal,Authentication auth) {
OidcIdToken token=principal.getIdToken();
String tokenValue=token.getTokenValue();
        Authentication authentication=SecurityContextHolder.getContext().getAuthentication();
        OAuth2AuthenticationToken oauthToken=(OAuth2AuthenticationToken) authentication;
        OAuth2AuthorizedClient client=oauthService.loadAuthorizedClient(oauthToken.getAuthorizedClientRegistrationId(), oauthToken.getName());
        String jwtAccessToken=client.getAccessToken().getTokenValue();
        String url="http://localhost:9091/albums";
        HttpHeaders headers=new HttpHeaders();
        headers.add("Authorization", "Bearer "+jwtAccessToken);
        HttpEntity<List<AlbumsRest>> entity=new HttpEntity<List<AlbumsRest>>(headers);
        ResponseEntity<List<AlbumsRest>> responseEntity=restTemplate.exchange(url, HttpMethod.GET, entity, new ParameterizedTypeReference<List<AlbumsRest>>() {
        });
        List<AlbumsRest> albums=responseEntity.getBody();
        model.addAttribute("Albums", albums);
        return "albums";
        
    }

}

以下是我的 application.properties 文件:-

server.port=8087
spring.security.oauth2.client.registration.mywebclient.client-id=tcs
spring.security.oauth2.client.registration.mywebclient.client-secret=2KcMN6xsmJH235k6TlLXUXj3iY3sAl8i
spring.security.oauth2.client.registration.mywebclient.scope=openid,profile,roles
spring.security.oauth2.client.registration.mywebclient.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.mywebclient.redirect-uri=http://localhost:8087/login/oauth2/code/mywebclient

spring.security.oauth2.client.provider.mywebclient.authorization-uri=http://localhost:7070/auth/realms/developer/protocol/openid-connect/auth
spring.security.oauth2.client.provider.mywebclient.token-uri=http://localhost:7070/auth/realms/developer/protocol/openid-connect/token
spring.security.oauth2.client.provider.mywebclient.jwk-set-uri=http://localhost:7070/auth/realms/developer/protocol/openid-connect/certs
spring.security.oauth2.client.provider.mywebclient.user-info-uri=http://localhost:7070/auth/realms/developer/protocol/openid-connect/userinfo
spring.security.oauth2.client.provider.mywebclient.user-name-attribute=Utkarsh0911

每当我点击 url http://localhost:8087/albums 时,它都会执行过多的重定向,最终会出现在显示“此页面无法正常工作”消息的页面上。

【问题讨论】:

    标签: keycloak spring-security-oauth2


    【解决方案1】:

    当重定向 URL 未在应用程序中定义为允许时,通常会发生此循环。请在此处查看: https://dzone.com/articles/spring-boot-how-to-solve-oauth2-err-too-many-redir

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2013-11-19
    • 2014-02-23
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多