这里记录一个生产中遇到的一个问题。
现有环境是基于SpringBoot 2.6.8,然后是前后台一体化的项目。
安全框架使用的是内置版本的SpringSecurity。
在实际使用过程中遇到一个问题。
就是当用户登陆失效后,前端操作JSON请求获取列表数据,但因为登陆失效了。导致请求过不去返回结果
同时服务端也没有任何的异常日志。
后来发现是因为Security内置的一个过滤器CsrfFilter。这个过滤器
this.accessDeniedHandler.handle(request, response, exception);
有个处理起对这个请求进行了处理,同时转发到了error页面。
解决方案如下:
1.首先需要实现上述过滤器内调用的处理器
import cn.com.seecom.vnumber.common.Result;
import cn.com.seecom.vnumber.common.ResultEnum;
import cn.com.seecom.vnumber.utils.RequestUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
import org.springframework