关于使用SpringSecurity框架发起JSON请求,但因登陆失效导致响应403的问题。

这里记录一个生产中遇到的一个问题。

现有环境是基于SpringBoot 2.6.8,然后是前后台一体化的项目。

安全框架使用的是内置版本的SpringSecurity。

在实际使用过程中遇到一个问题。

就是当用户登陆失效后,前端操作JSON请求获取列表数据,但因为登陆失效了。导致请求过不去返回结果

同时服务端也没有任何的异常日志。

后来发现是因为Security内置的一个过滤器CsrfFilter。这个过滤器

复制代码
this.accessDeniedHandler.handle(request, response, exception);

有个处理起对这个请求进行了处理,同时转发到了error页面。

解决方案如下:

1.首先需要实现上述过滤器内调用的处理器

java 复制代码
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.util.Assert;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lvshiyu
 * @description: 重写当请求访问被拒绝处理
 * 在基于角色的访问控制(RBAC)或基于权限的访问控制(PBAC)系统中,当用户尝试访问他们没有权限的资源时,
 * Spring Security 会触发 AccessDeniedHandler 来处理这种情况。
 * @date 2024年03月13日 17:55
 */
@Component
@Slf4j
public class CustomAccessDeniedHandler implements AccessDeniedHandler {

    private String errorPage;

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        if (response.isCommitted()) {
            log.trace("Did not write to response since already committed");
            return;
        }
        if (RequestUtil.isAjaxRequest(request)) {
            // 重点是这里,判断如果是ajax请求,则返回对应的登陆失效JSON。
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.getWriter().println(JSON.toJSON(new Result(ResultEnum.TOKEN_EXPIRED)));
            return;
        }
        if (this.errorPage == null) {
            log.debug("Responding with 403 status code");
            response.sendError(HttpStatus.FORBIDDEN.value(), HttpStatus.FORBIDDEN.getReasonPhrase());
            return;
        }
        // Put exception into request scope (perhaps of use to a view)
        request.setAttribute(WebAttributes.ACCESS_DENIED_403, accessDeniedException);
        // Set the 403 status code.
        response.setStatus(HttpStatus.FORBIDDEN.value());
        // forward to error page.
        request.getRequestDispatcher(this.errorPage).forward(request, response);
    }

    public void setErrorPage(String errorPage) {
        Assert.isTrue(errorPage == null || errorPage.startsWith("/"), "errorPage must begin with '/'");
        this.errorPage = errorPage;
    }

}

2.在配置类中引入该处理器,这里涉及到部分的业务代码,所以采用截图的方式,只需要将上面的失效处理器配置到config中即可。

3.在前端JS内对发起请求的地方做统一拦截处理。

javascript 复制代码
$.ajaxSetup({
            complete: function(xhr, status) {
                if (xhr.responseText == 'invalidSession') {
                    if(window.parent){
                        window.parent.location.reload();
                    }else{
                        window.location.reload();
                    }
                }
                //优化统一AJAX拦截
                if (xhr.status == 200 && xhr.responseJSON != null) {
                    let data = xhr.responseJSON;
                    if (data.code == 1000) {
                        console.log("登陆失效")
                        //登陆超时
                        window.location.reload();
                    }
                }
            }
        });

这里就解决了上述所说的问题,如果对您有帮助 请帮忙点个赞。

注意:这里重写了原来的403异常信息,会导致原有出现403的异常都会通过JSON的方式发送所改造的响应字符串。

相关推荐
黎雁·泠崖1 分钟前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
qq_12498707531 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_1 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.1 小时前
Day06——权限认证-项目集成
java
瑶山1 小时前
Spring Cloud微服务搭建四、集成RocketMQ消息队列
java·spring cloud·微服务·rocketmq·dashboard
abluckyboy2 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
2301_818732062 小时前
前端调用控制层接口,进不去,报错415,类型不匹配
java·spring boot·spring·tomcat·intellij-idea
2501_941982052 小时前
深度对比:Java、Go、Python 实现企微外部群推送,哪个效率更高?
java·golang·企业微信
码字的字节2 小时前
Spring Cloud服务注册与发现(一):手把手搭建Eureka Server,详解高可用配置
spring·spring cloud·eureka
大厂资深架构师2 小时前
Spring Cloud Eureka在后端系统中的服务剔除策略
spring·spring cloud·ai·eureka