代码之家  ›  专栏  ›  技术社区  ›  TwiN Alireza Fattahi

尽管成功,Spring Security OAuth2登录仍重定向到错误页面

  •  4
  • TwiN Alireza Fattahi  · 技术社区  · 7 年前

    为了不泄露机密信息,提供商将被替换为 $PROVIDER

    授权是有效的,但它不是将我重定向到索引,而是将我重定向到 /error

    复制步骤

    1. 启动应用程序
    2. 转到任何页面,它都会将我重定向到 http://localhost/oauth_login 其中显示链接 login
    3. 单击链接 登录名 (链接到 http://localhost/oauth2/authorization/$PROVIDER )
    4. 重定向到 http://localhost/error

    错误页面显示以下内容(为了便于阅读,我对其进行了格式化):

    {
        "timestamp":"2018-04-05T14:18:47.720+0000", 
        "status":999, 
        "error":"None", 
        "message":"No message available"
    }
    

    这显然与默认设置中显示的参数相同 白标错误页 ,所以实际上,唯一的问题是它是JSON,而不是HTML页面。如果我刷新 http://localhost/error 之后,它向我展示了正常 白标错误页

    如果我想导航到 http://localhost/ 在重定向到错误页面后,我进行了身份验证(用户数据在那里,因此授权成功)。基本上,问题是我被重定向到 http://localhost/error 而不是 http://localhost/

    因为它功能齐全(除了重定向),所以我不会发布整个安全配置,而是将其限制在相关代码中:

    安全配置(SecurityConfiguration)

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .csrf().disable()
                .authorizeRequests().anyRequest()
                    .authenticated()
                .and()
                    .oauth2Login()
                .loginPage("/oauth_login")
                    .defaultSuccessUrl("/")
                    .failureUrl("/oauth_login")
                    .permitAll()
                .and()
                    .logout()
                        .logoutUrl("/logout")
                        .logoutSuccessUrl("/oauth_login").permitAll()
        ;
    }
    

    相关属性

    spring.security.oauth2.client.registration.$PROVIDER.redirectUriTemplate=http://localhost/login/oauth2/code/$PROVIDER
    

    相关信息

    • 一旦我在网站上通过身份验证,我就可以导航、刷新页面、做任何我想做的事(直到我注销)。

    TL;DR公司 授权有效,但已重定向到 /错误 而不是 /

    1 回复  |  直到 7 年前
        1
  •  6
  •   TwiN Alireza Fattahi    7 年前

    我实际上找到了我自己问题的答案,但我决定无论如何都把它贴出来,以防有人遇到同样的问题。

    .defaultSuccessUrl("/")
    

    应该是

    .defaultSuccessUrl("/", true)
    

    如果不设置为 true ,它会自动将用户重定向到上一个请求,在我的情况下,最后一个请求是向url发出的 /error ,这就解释了我的问题。

    通过将其设置为 真的 ,则强制用户重定向到 defaultSuccessUrl

    通过查看日志,您实际上可以看到:

    2018-04-05 11:04:09 DEBUG [-nio-80-exec-10] RequestAwareAuthenticationSuccessHandler : Redirecting to DefaultSavedRequest Url: http://localhost/error
    2018-04-05 11:04:09 DEBUG [-nio-80-exec-10] o.s.security.web.DefaultRedirectStrategy : Redirecting to 'http://localhost/error'