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

相关推荐
百***864614 小时前
Spring Boot应用关闭分析
java·spring boot·后端
tanxiaomi14 小时前
Spring、Spring MVC 和 Spring Boot ,mybatis 相关面试题
java·开发语言·mybatis
弥巷15 小时前
【Android】常见滑动冲突场景及解决方案
android·java
间彧15 小时前
GraalVM 深度解析:下一代 Java 技术平台
java
合作小小程序员小小店15 小时前
网页开发,在线%旧版本旅游管理%系统,基于eclipse,html,css,jquery,servlet,jsp,mysql数据库
java·数据库·servlet·eclipse·jdk·旅游·jsp
20岁30年经验的码农15 小时前
Java Sentinel流量控制与熔断降级框架详解
java·开发语言·sentinel
程序员西西15 小时前
SpringBoot轻松整合Sentinel限流
java·spring boot·后端·计算机·程序员
q***465215 小时前
Spring Boot 实战:轻松实现文件上传与下载功能
java·数据库·spring boot
努力发光的程序员15 小时前
互联网大厂Java面试:从Spring Boot到大数据处理的实战场景问题解析
spring boot·微服务·云原生·java面试·大数据处理·技术解析·互联网求职
Li_76953215 小时前
10分钟快速入手Spring Cloud Config
java·spring·spring cloud