AccessControlFilter

  • 访问控制过滤器,继承PathMatchingFilter过滤器,重写onPreHandle办法.
  • isAccessAllowed 是否容许拜访
  • onAccessDenied 是否回绝拜访

    咱们通过重写上边两个办法来管制过滤逻辑,实现以后的需要,用户不登录点赞提醒请登录

  • 定义一个LoginAuthFilter继承AccessControlFilter
  • 前端js渲染脚本

    // 前端弹窗的js代码private static final String JS =    "<script type='text/javascript'>var wp=window.parent; if(wp!=null){while(wp.parent&&wp.parent!==wp){wp=wp.parent;}wp.location.href='%1$s';}else{window.location.href='%1$s';}</script>";private String loginUrl = "/login";
  • 重写isAccessAllowed办法判断用户是否登录

    @Overrideprotected boolean isAccessAllowed(    ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {  Subject subject = SecurityUtils.getSubject();  if (null != subject) {    if (subject.isRemembered()) {      return true;    }    if (subject.isAuthenticated()) {      return true;    }  }  return false;}
  • 重写onAccessDenied办法,ajax申请返回谬误code,提醒‘请登录’否则跳转到登录页
    查看残缺代码片段移步

    成果如下图


    点击点赞按钮查看成果