【问题标题】:How to fix this Oauth 2.0 error redirecting如何修复此 Oauth 2.0 错误重定向
【发布时间】:2019-05-21 14:33:30
【问题描述】:

400。这是一个错误。错误:redirect_uri_mismatch 请求中的重定向 URI,http://testing.com:8008/login,与授权给 OAuth 客户端的不匹配。要更新授权的重定向 URI,请访问:

凭据页面中的授权重定向 URI 是 http://testing.com:8008/success,但它总是重定向到 http://testing.com:8008/login 不知道这里出了什么问题,有人可以帮忙。

下面是我的 application.yml 配置文件。

security:
  oauth2:
    client:
      clientId: 701691307057-184m2p0ce76k.apps.googleusercontent.com
      clientSecret: 7186351
      pre-established-redirect-uri: http://testing.com:8008/success
      accessTokenUri: https://www.googleapis.com/oauth2/v3/token
      userAuthorizationUri: https://accounts.google.com/o/oauth2/auth
      tokenName: oauth_token
      authenticationScheme: query
      clientAuthenticationScheme: form
      scope: profile email
    resource:
      userInfoUri: https://www.googleapis.com/userinfo/v2/me
      preferTokenInfo: false

Web 安全配置类如下。

@EnableOAuth2Sso
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf()
                .disable()
                .antMatcher("/**")
                .authorizeRequests()
                .antMatchers("/", "/home", "/success")
                .permitAll()
                .anyRequest()
                .authenticated();
    }
}

下面的控制器

@Controller
@RequestMapping("/")
public class WelcomeController {

    @RequestMapping(value = {"home"}, method = RequestMethod.GET)
    public String welcomePage(){
        return "welcomePageAfterSignin";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String welcomePage2(){
        return "welcomePage";
    }

    @RequestMapping(value = {"success"}, method = RequestMethod.GET)
    public String AfterSignIn(){
        return "success";
    }

    @RequestMapping(value = "user")
    public Principal user(Principal principal) {
        return principal;
    }
}

【问题讨论】:

  • 如果您被 Spring Security 重定向到 /login on,这意味着 URL /success 受到保护,未经身份验证无法访问。您需要允许无需身份验证即可访问它。
  • 是的,有道理,但使用谷歌登录重定向的全部目的将丢失。因为我想在使用 Oauth 登录后重定向到 /success。如果我允许 /success,它将不会使用 Oauth 进行身份验证。

标签: java oauth-2.0 spring-security-oauth2


【解决方案1】:

其实没什么问题,你被重定向到spring security登录页面。

您缺少的是 spring-security 的凭据

仔细查看日志,您会看到类似Using generated security password: 5608543f-48fd-46ae-94a4-3f7094aa0d53的密码

所以您需要在您的登录页面中输入这些凭据。 用户名默认为:user 每次在日志中生成密码(如):5608543f-48fd-46ae-94a4-3f7094aa0d53

只有这样你才会被重定向到你的谷歌页面或你暴露的api。

添加图片供您参考

【讨论】:

  • 我不想在构建 spring 登录功能中使用,以便我可以为我的网络应用程序使用谷歌登录,我将它配置为这样做。但我收到重定向错误。
  • 那么请展示你的代码,除了这个配置文件,显示我们可以理解,更好。
  • 对不起,我忘了说,也请分享你的jsp文件夹结构,因为它很重要。
【解决方案2】:
  • 确保检查协议“http://”或“https://”,因为谷歌也检查协议。最好在列表中添加这两个 URL。
  • 这似乎很奇怪,也很烦人,没有“一个”解决方案。对我来说,http://localhost:8000 没有成功,但 http://localhost:8000/ 成功了。

【讨论】:

  • 复制自另一个 StackOverflow 答案
猜你喜欢
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多