diff --git a/src/main/java/com/freedom/admin/config/WebSecurityConfig.java b/src/main/java/com/freedom/admin/config/WebSecurityConfig.java index 7759fdf..6335aea 100644 --- a/src/main/java/com/freedom/admin/config/WebSecurityConfig.java +++ b/src/main/java/com/freedom/admin/config/WebSecurityConfig.java @@ -1,5 +1,7 @@ package com.freedom.admin.config; +import com.alibaba.fastjson.JSON; +import com.freedom.admin.common.Result; import com.freedom.admin.service.impl.UserServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -84,6 +86,14 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { .and() .logout() .logoutUrl("/logout") + .logoutSuccessHandler((request, response, authentication) -> { + // 返回 JSON 而非默认 302 跳转 /login?logout, + // 避免前端 axios 跟随重定向触发跨域拦截 + Result result = Result.ok().message("退出成功"); + response.setCharacterEncoding("utf-8"); + response.setContentType("application/json"); + response.getWriter().write(JSON.toJSONString(result)); + }) .and() .rememberMe() .rememberMeParameter("rememberme")