![](/img/trans.png)
[英]In Spring security 2.0.2 How to redirect to a requested url after login
[英]Spring Security OAuth 2.0 Google: redirect to default URL after unauthorized request instead of requested URL
我使用 Spring Security OAuth 2.0 和 Google 作為身份提供者。 我在正確處理會話超時和重新身份驗證方面遇到問題。
設想:
當前行為:
期望的行為:
我的配置類:
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 鏈接的登錄屏幕。我做錯了什么?
提前致謝。
設置defaultSuccessUrl
將覆蓋您的successHandler()
,因為設置 URL 實際上會添加您想要替換的SavedRequestAwareAuthenticationSuccessHandler
。 順序很重要,因為oauth2Login
配置器中只有一個successHandler
。
相反,您需要將defaultSuccessUrl
傳遞給SimpleUrlAuthenticationSuccessHandler
的構造函數,並且只設置successHandler(successHandler)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.