在第一节的部分我们也知道登录的流程,但当后台发现redis缓存的登陆实体类过期了,是怎么处理并且怎么给前端发送消息?

其实SpringSecurity有一个config配置SecurityConfig类,继承于WebSecurityConfigurerAdapter

其中重写了configure()方法,用来自定义过滤和拦截处理
在这里插入图片描述
红线框就是处理登录token过期,实质上认证失败处理类,只不过token过期是认证失败的一种情况。
我们看下这个类做了什么?
我们点击紫色的属性
在这里插入图片描述
通过这个注入的实现类看看

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable
{
    private static final long serialVersionUID = -8970718410437077606L;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e)
            throws IOException
    {
        int code = HttpStatus.UNAUTHORIZED;
        String msg = StringUtils.format("请求访问:{},认证失败,无法访问系统资源", request.getRequestURI());
        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(code, msg)));
    }
}

当遇到认证失败情况,如token过期就会到这个方法,只要你把这个类放入到SpingSecurity的configure()方法中进行配置。
其中最后一句就是把返回给前端的AjaxResult封装起来,转换成JSON给前端。

Logo

快速构建 Web 应用程序

更多推荐