【问题标题】:Spring Security OAuth 2.0 Google: redirect to default URL after unauthorized request instead of requested URLSpring Security OAuth 2.0 Google:在未经授权的请求后重定向到默认 URL 而不是请求的 URL
【发布时间】:2021-11-30 14:35:12
【问题描述】:

我使用 Spring Security OAuth 2.0 和 Google 作为身份提供者。我在正确处理会话超时和重新身份验证方面遇到问题。

场景:

  • 会话超时后对 REST API 的一些请求。
  • 前端处理 HTTP 403 并显示带有 Spring Security 登录端点链接的屏幕。
  • 用户点击此链接。 Spring 问题使用必要的参数(代码、状态等)重定向到 Google 登录页面。用户成功重新验证。

当前行为:

  • 登录后,Google 会重定向到之前请求的 REST API URL。结果,用户在浏览器中看到了一些 JSON。我根本不明白应用程序的哪一部分可以保存它。我禁用了一切。 (参见配置类下面的 cmets。)

期望的行为:

  • 登录后,Google 会重定向到某个启动 UI 屏幕。

我的配置类:

import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler

@Configuration
@EnableWebSecurity
class WebSecurityConfiguration: WebSecurityConfigurerAdapter() {

    @Value("\${app.security.oauth2.defaultSuccessUrl}")
    lateinit var defaultSuccessUrl: String

    @Throws(Exception::class)
    override fun configure(httpSecurity: HttpSecurity) {
        val successHandler = SimpleUrlAuthenticationSuccessHandler()
        successHandler.setUseReferer(false)
        httpSecurity
            .antMatcher("/**")
            .authorizeRequests()
            .antMatchers("/", "/login**", "/js/**", "/error**").permitAll()
            .anyRequest().authenticated()
            .and().oauth2Login()
            .successHandler(successHandler)
            .defaultSuccessUrl(defaultSuccessUrl)
            .and().logout().logoutSuccessUrl("/login").deleteCookies("JSESSIONID").permitAll()
            .and()
            .csrf().disable()
            .exceptionHandling()
            .authenticationEntryPoint(Http403ForbiddenEntryPoint())
    }

}
  • defaultSuccessUrl 设置为所需的 UI 屏幕。
  • 我已经将成功处理程序更改为 SimpleUrlAuthenticationSuccessHandler 而不是 SavedRequestAwareAuthenticationSuccessHandler 以关闭 Spring 保存请求的 URL。
  • successHandler.setUseReferer(false) 在 HTTP 级别禁用 Referer 标头。
  • 我使用Http403ForbiddenEntryPoint() 只是在会话超时时发出HTTP 403。前端处理此问题并显示带有 Spring Security 登录 URL 链接的登录屏幕。

我做错了什么?

提前致谢。

UPD:根据Steve Riesenberg 的回答更新代码:

import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler

@Configuration
@EnableWebSecurity
class WebSecurityConfiguration: WebSecurityConfigurerAdapter() {

    @Value("\${app.security.oauth2.defaultSuccessUrl}")
    lateinit var defaultSuccessUrl: String

    @Throws(Exception::class)
    override fun configure(httpSecurity: HttpSecurity) {
        val successHandler = SimpleUrlAuthenticationSuccessHandler(defaultSuccessUrl)
        successHandler.setUseReferer(false)
        httpSecurity
            .antMatcher("/**")
            .authorizeRequests()
            .antMatchers("/", "/login**", "/js/**", "/error**").permitAll()
            .anyRequest().authenticated()
            .and().oauth2Login()
            .successHandler(successHandler)
            .and().logout().logoutSuccessUrl("/login").deleteCookies("JSESSIONID").permitAll()
            .and()
            .csrf().disable()
            .exceptionHandling()
            .authenticationEntryPoint(Http403ForbiddenEntryPoint())
    }

}

【问题讨论】:

    标签: spring spring-boot kotlin spring-security spring-kotlin


    【解决方案1】:

    设置defaultSuccessUrl 将覆盖您的successHandler(),因为设置URL 实际上会添加您想要替换的SavedRequestAwareAuthenticationSuccessHandler。顺序很重要,因为 oauth2Login 配置器中只有一个 successHandler

    相反,您需要将defaultSuccessUrl 传递给SimpleUrlAuthenticationSuccessHandler 的构造函数,并且只设置successHandler(successHandler)

    【讨论】:

      猜你喜欢
      • 2023-01-11
      • 2020-05-11
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      相关资源
      最近更新 更多