OAuth2 client fails when redirecting from the authorization step

为君一笑 提交于 2021-01-29 09:28:46

问题


In my current spring-boot, I am trying sign in the user using an external OAuth2 server. The problem right now is that when I execute the application, after the authorization being successful, the user should be redirected back to the application. When this happens, I got an error.

My application.properties file:

spring.security.oauth2.client.registration.mercadolivre.provider=mercadolivre
spring.security.oauth2.client.registration.mercadolivre.client-id=...
spring.security.oauth2.client.registration.mercadolivre.client-secret=...
spring.security.oauth2.client.registration.mercadolivre.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.mercadolivre.redirect-uri=http://localhost:8080/
spring.security.oauth2.client.provider.mercadolivre.authorization-uri=https://auth.mercadolivre.com.br/authorization
spring.security.oauth2.client.provider.mercadolivre.token-uri=https://api.mercadolibre.com/oauth/token

My security configuration class:

@Configuration
public class Security extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .oauth2Login();
    }
}

The error I am facing right now:

What is the problem here?

update

I try add this line to my applicatio.properties file:

spring.security.oauth2.client.registration.mercadolivre.redirect-uri={baseUrl}/login/oauth2/code/{registrationId}

and now I am getting this error:

with the browser developer console open:


回答1:


Change the redirect-uri to something else... It's better if you use a template string like the one the reference uses:

spring.security.oauth2.client.registration.mercadolivre.redirect-uri={baseUrl}/login/oauth2/code/{registrationId}

That's the uri that will process the authorization code the authorization server sends back. If you want the user to be redirected to some page after successful authentication, do something like this in configure():

http.
    ...
    .oauth2Login()
        .defaultSuccessUrl("/");


来源:https://stackoverflow.com/questions/61018997/oauth2-client-fails-when-redirecting-from-the-authorization-step

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!