关于使用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的方式发送所改造的响应字符串。

相关推荐
点点滴滴的记录10 分钟前
2017~2018博文汇总目录
java·大数据
夜月行者30 分钟前
如何使用ssm实现民族大学创新学分管理系统分析与设计+vue
java·后端·ssm
艾伦~耶格尔1 小时前
IDEA 配置 Git 详解
java·ide·git·后端·intellij-idea
云兮杜康1 小时前
IDEA中用过git之后文件名颜色全变红
java·git·intellij-idea
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS洗衣店订单管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
杨荧1 小时前
【JAVA开源】基于Vue和SpringBoot的周边产品销售网站
java·开发语言·vue.js·spring boot·spring cloud·开源
橘橙黄又青1 小时前
多线程-初阶(1)
java·开发语言
宇卿.1 小时前
Java中的while和do...while循环
java·开发语言
星浩前进中1 小时前
基于Springboot+VUE的二手奢侈品商城的设计与实现
vue.js·spring boot·后端
shangan_31 小时前
JAVA思维提升案例3
java