如何配置CORS过滤器以使用Spring Security注销获取cookie?

dy1byipe  于 2023-05-17  发布在  Spring
关注(0)|答案(1)|浏览(117)

我正在做一个使用Spring Security的项目。我有一个SecurityFilterChain,我在其中使用.logout()。我的应用程序使用cookie来传输jwt令牌,分别在执行注销后,应用程序必须清除cookie并在数据库中更新它。当我使用Postman时,一切都按计划进行,但是,当我尝试从前端访问此请求时,cookie不会传输。
我认为这个问题与CORS政策有关,但我想不出如何解决它。我将感激你的忠告。
securityFilterChain和corsConfigurationSource:

@Configuration
@EnableWebSecurity
class SecurityConfig(
    val jwtAuthFilter: JwtAuthFilter,
    val authenticationProvider: AuthenticationProvider,
    val logoutHandler: LogoutHandler
) {
    @Bean
    fun corsConfigurationSource(): CorsConfigurationSource {
        val configuration = CorsConfiguration()
        configuration.allowedOrigins = listOf("http://localhost:3000")
        configuration.allowedMethods = listOf("*")
        configuration.allowedHeaders = listOf("*")
        configuration.allowCredentials = true
        val source = UrlBasedCorsConfigurationSource()
        source.registerCorsConfiguration("/**", configuration)
        return source
    }
    @Bean
    fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
        http
            .cors()
            .and()
            .csrf()
            .disable()
            .authorizeHttpRequests()
            .requestMatchers(
                "/api/v1/auth/**",
                "/api/v1/activity/get",
                "/api/v1/timetable/allbydate"
            )
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .authenticationProvider(authenticationProvider)
            .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter::class.java)
            .logout()
            .logoutUrl("/api/v1/auth/logout")
            .addLogoutHandler(logoutHandler)
            .logoutSuccessHandler { request, response, authentication -> SecurityContextHolder.clearContext() }

        return http.build()
    }
}

LogoutHandler:

@Service
class LogoutServiceImpl(
    val tokenRepository: TokenRepository
): LogoutHandler {
    @Value("\${app.security.jwt.access-token.path}")
    private lateinit var accessPath: String
    @Value("\${app.security.jwt.refresh-token.path}")
    private lateinit var refreshPath: String
    override fun logout(
        request: HttpServletRequest?,
        response: HttpServletResponse?,
        authentication: Authentication?
    ) {
        if (request?.cookies == null) return

        val jwtCookies = request
            .cookies
            .filter { cookie -> cookie.name == "jwt-access" || cookie.name == "jwt-refresh" }
        val storedJwt = jwtCookies
            .map { cookie -> tokenRepository.findByToken(cookie.value) }
        for (storedToken in storedJwt) {
            if (storedToken != null) {
                storedToken.expired = true
                storedToken.revoked = true
                tokenRepository.save(storedToken)
            }
        }

        val jwtAccess = Cookie("jwt-access", "")
        jwtAccess.path = accessPath
        jwtAccess.maxAge = 0
        val jwtRefresh = Cookie("jwt-refresh", "")
        jwtRefresh.path = refreshPath
        jwtRefresh.maxAge = 0
        response?.addCookie(jwtAccess)
        response?.addCookie(jwtRefresh)
    }
}

调用logout API的React函数:

async function handleLogout() {
        await axios
            .post(
                "auth/logout", 
                { withCredentials: true }
            )
            .then(response =>
                {
                    setAuth(false)
                    navigate("/")
                }
            )
    }
bmvo0sr5

bmvo0sr51#

这是个愚蠢的错误我忘了在我的axios请求中为“data”添加花括号。
现在它工作得很好:

async function handleLogout() {
        await axios
            .post(
                "auth/logout", 
                {},
                { withCredentials: true }
            )
            .then(response =>
                {
                    setAuth(false)
                    navigate("/")
                }
            )
    }

相关问题