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

标签 spring spring-boot spring-security oauth-2.0 spring-security-oauth2

为不泄露 secret 信息,供应商将替换为$PROVIDER .

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

重现步骤

  • 启动应用
  • 转到任何页面,它会将我重定向到 http://localhost/oauth_login显示链接 login .
  • 点击链接login (链接到 http://localhost/oauth2/authorization/$PROVIDER)
  • 重定向到 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/ .

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

    安全配置
    @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;博士 授权有效但重定向到 /error而不是 / .

    最佳答案

    我实际上找到了我自己问题的答案,但我还是决定发布它,以防有人遇到同样的问题。

    .defaultSuccessUrl("/")
    

    本来应该
    .defaultSuccessUrl("/", true)
    

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

    通过将其设置为 true ,你强制用户重定向到任何你的 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'
    

    关于尽管成功,Spring Security OAuth2 登录重定向到错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49675747/

    相关文章:

    grails - 在 Grails 中使用 Spring Security 与 CAS 和 LDAP

    java - cvc-elt.1 : Cannot find the declaration of element 'beans'

    java - 自动生成的 ID,而不是 null 或默认 0

    java - Hibernate 不设置外部约束

    java - 通过守护进程作为独立运行 spring

    java - 无法在itext表中动态显示mysql数据库中的blob图像

    spring - 在spring或spring security中配置cookie头的Domain属性

    java - spring data jpa repository inject在spring boot中使用@Autowired注入(inject)失败

    java - 如何在 Spring Boot 应用程序上注册 javax.jws.Webservice

    java - 如何在Spring中临时授予用户角色?