alt+7

OncePerRequestFilter

  public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
if ( request.getAttribute(alreadyFilteredAttributeName) != null ) {
log.trace("Filter '{}' already executed. Proceeding without invoking this filter.", getName());
filterChain.doFilter(request, response);
} else //noinspection deprecation
if (/* added in 1.2: */ !isEnabled(request, response) ||
/* retain backwards compatibility: */ shouldNotFilter(request) ) {
log.debug("Filter '{}' is not enabled for the current request. Proceeding without invoking this filter.",
getName());
filterChain.doFilter(request, response);
} else {
// Do invoke this filter...
log.trace("Filter '{}' not yet executed. Executing now.", getName());
request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE); try {
doFilterInternal(request, response, filterChain); //保证客户端请求后该filter的doFilter只会执行一次。
} finally {
// Once the request has finished, we're done and we don't
// need to mark as 'already filtered' any more.
request.removeAttribute(alreadyFilteredAttributeName);
}
}
}
可以看出doFilter的实质内容是在doFilterInternal方法中完成的。
所以实质上是保证每一个filter的 doFilterInternal只会被执行一次,
例如在配置中 配置路径 /user/** = authc,authc.则只会执行authc中的doFilterInternal一次。
doFilterInternal非常重要,在shiro整个filter体系中的核心方法及实质入口。
另外,shiro是通过在request中设置一个该filter特定的属性值来保证该filter只会执行一次的。
其次可以看到有一个enabled属性,表示是否启用这个filter,默认是true,可以设置成false,
则会跳过这个filter的doFilterInternal方法而去执行filter链中其他filter。(filterChain.doFilter(request, response);放行

AdviceFilter中主要是对doFilterInternal做了更细致的切分

 public void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain)
throws ServletException, IOException { Exception exception = null; try { boolean continueChain = preHandle(request, response);
if (log.isTraceEnabled()) {
log.trace("Invoked preHandle method. Continuing chain?: [" + continueChain + "]");
} if (continueChain) {
executeChain(request, response, chain); //doFilterInternal(request, response, filterChain);
} postHandle(request, response); //进入我们自己的方法逻辑,有点像 interceptor拦截器
if (log.isTraceEnabled()) {
log.trace("Successfully invoked postHandle method");
} } catch (Exception e) {
exception = e;
} finally {
cleanup(request, response, exception);
}
}


PathMatchingFilter主要是对preHandle做进一步细化控制,该filter为抽象类,其他路径直接通过:preHandle中,
若请求的路径非该filter中配置的拦截路径,则直接返回true进行下一个filter。
若包含在此filter路径中,则会在isFilterChainContinued做一些控制,
该方法中会调用onPreHandle方法,
所以子类可以在onPreHandle中编写filter控制流程代码(返回true或false)。
 protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {

         if (this.appliedPaths == null || this.appliedPaths.isEmpty()) {
if (log.isTraceEnabled()) {
log.trace("appliedPaths property is null or empty. This Filter will passthrough immediately.");
}
return true;
} for (String path : this.appliedPaths.keySet()) {
// If the path does match, then pass on to the subclass implementation for specific checks
//(first match 'wins'):
if (pathsMatch(path, request)) {
log.trace("Current requestURI matches pattern '{}'. Determining filter chain execution...", path);
Object config = this.appliedPaths.get(path);
return isFilterChainContinued(request, response, path, config);
}
} //no path matched, allow the request to go through:
return true;
} private boolean isFilterChainContinued(ServletRequest request, ServletResponse response,
String path, Object pathConfig) throws Exception { if (isEnabled(request, response, path, pathConfig)) { //isEnabled check added in 1.2 点击在OncePerRequestFilter里头 默认true
if (log.isTraceEnabled()) {
log.trace("Filter '{}' is enabled for the current request under path '{}' with config [{}]. " +
"Delegating to subclass implementation for 'onPreHandle' check.",
new Object[]{getName(), path, pathConfig});
}
//The filter is enabled for this specific request, so delegate to subclass implementations
//so they can decide if the request should continue through the chain or not:
return onPreHandle(request, response, pathConfig);
} if (log.isTraceEnabled()) {
log.trace("Filter '{}' is disabled for the current request under path '{}' with config [{}]. " +
"The next element in the FilterChain will be called immediately.",
new Object[]{getName(), path, pathConfig});
}
//This filter is disabled for this specific request,
//return 'true' immediately to indicate that the filter will not process the request
//and let the request/response to continue through the filter chain:
return true;
} protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
return true;
}

 AccessControlFilter

AccessControlFilter中的对onPreHandle方法做了进一步细化,isAccessAllowed方法和onAccessDenied方法达到控制效果
。这两个方法都是抽象方法,由子类去实现。到这一层应该明白。isAccessAllowed和onAccessDenied方法会影响到onPreHandle方法,而onPreHandle方法会影响到preHandle方法,
preHandle方法会达到控制filter链是否执行下去的效果。所以如果正在执行的filter中isAccessAllowed和onAccessDenied都返回false,
则整个filter控制链都将结束,不会到达目标方法(客户端请求的接口),而是直接跳转到某个页面(由filter定义的,将会在authc中看到)。

⑥AuthenticationFilter和AuthenticatingFilter认证的filter,在抽象类中AuthenticatingFilter实现了isAccessAllowed方法,该方法是用来判断用户是否已登录,若未登录再判断是否请求的是登录地址,是登录地址则放行,否则返回false终止filter链。

另外可以看到提供了executeLogin方法实现用户登录的,还定义了onLoginSuccess和onLoginFailure方法,在登录成功或者失败时做一些操作。登录将在下面详细说明。

⑦FormAuthenticationFiltershiro提供的登录的filter,如果用户未登录,即AuthenticatingFilter中的isAccessAllowed判断了用户未登录,则会调用onAccessDenied方法做用户登录操作。

若用户请求的不是登录地址,则跳转到登录地址,并且返回false直接终止filter链。

若用户请求的是登录地址,如果是post请求则进行登录操作,由AuthenticatingFilter中提供的executeLogin方法执行。

否则直接通过继续执行filter链,并最终跳转到登录页面(因为用户请求的就是登录地址,若不是登录地址也会重定向到登录地址).

AuthenticatingFilter中的executeLogin

若登录成功返回false(FormAuthenticationFiltershiro的onLoginSuccess默认false),则表示终止filter链,直接重定向到成功页面,甚至不到达目标方法直接返回了。

若登录失败,直接返回true(onLoginFailure返回false),继续执行filter链并最终跳转到登录页面,该方法还会设置一些登录失败提示 shiroLoginFailure,

在目标方法中可以根据这个错误提示制定客户端更加友好的错误提示。

二 自定义filter

一般自定义filter可以继承三种:

①OncePerRequestFilter只需实现doFilterInternal方法即可,在这里面实现filter的功能。切记在该方法中最后调用filterChain.doFilter(request, response),允许filter链继续执行下去。可以在这个自定义filter中覆盖isEnable达到控制该filter是否需要被执行(实质是doFilterInternal方法)以达到动态控制的效果,一般不建议直接继承这个类;

②AdviceFilter  中提供三个方法preHandle postHandle afterCompletion:若需要在目标方法执行前后都做一些判断的话应该继承这个类覆盖preHandle 和postHandle 。

③PathMatchingFilter中preHandle实质会判断onPreHandle来决定是否继续往下执行。所以只需覆盖onPreHandle方法即可。

④AccessControlFilter:最常用的,该filter中onPreHandle调用isAccessAllowed和onAccessDenied决定是否继续执行。一般继承该filter,isAccessAllowed决定是否继续执行。onAccessDenied做后续的操作,如重定向到另外一个地址、添加一些信息到request域等等。

④若要自定义登录filter,一般是由于前端传过来的需求所定义的token与shiro默认提供token的不符,可以继承AuthenticatingFilter ,在这里面实现createToken来创建自定义token。另外需要自定义凭证匹配器credentialsMatcher。重写public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info)即可。realm也需要自定义以返回自定义的token。

三shiro登录

由前面filter链可以看出登录已经很清晰了。shiro提供的FormAuthenticationFilter认证过滤器,继承了AuthenticatingFilter ,若已登录则isAccessAllowed直接通过,否则在 onAccessDenied中判断是否是登录请求,若是请求登录页面,直接通过,若是post提交登录信息  则会进行登录操作。否则直接跳转到登录页面。登录是由shiro的securityManager完成的,securityManager从Realm获取用户的真实身份,从FormAuthenticationFilter的createToken获取用户提交的token,credentialsMatcher完成是否匹配成功操作。

原作者:https://www.cnblogs.com/yoohot/p/6085830.html

最新文章

  1. Struts中文件上传的一些规则...
  2. java中常见的几种Runtimeexception
  3. haskell笔记1
  4. 多线程程序设计学习(3)immutable pattern模式
  5. Bit Map解析
  6. 解决win10客户机本地账户登陆导致远程桌面没法访问问题
  7. SEO定义目的,优化的好处
  8. java基础之类与对象2
  9. final、finally和finalize的区别
  10. python中self和cls的区别
  11. 安装Visual C++ 6.0后报错:应用程序无法正常启动(0xc0000142)
  12. [Python][小知识][NO.4] wxPython 字体选择对话框(O.O 不知道放到那里就放到这个分类的)
  13. Spark应用【根据新df更新旧df】
  14. Python全栈开发记录_第三篇(linux(ubuntu)的操作)
  15. 开启远程Windows系统3389端口
  16. 理解活在Iphone中的那些App (二)
  17. Swift2.0语言教程之Swift2.0语言中的标准函数
  18. 图文详解 Android Binder跨进程通信机制 原理
  19. CSS学习笔记(7)--html页面的CSS、DIV命名规则
  20. 【Docker 命令】- rmi命令

热门文章

  1. loj10099 矿场搭建
  2. Shell表达式,如${file##*/}
  3. JDBC行级锁
  4. C#实现类只实例化一次(被多个类访问调用)
  5. SQL:bat批处理多个.sql文件
  6. Centos7安装配置JDK8
  7. dubbo结果缓存机制
  8. GS70 使用 Linux 下面Oracle数据库时 设定 特定目录存储数据文件
  9. L - Large Division (大数, 同余)
  10. loj #2026. 「JLOI / SHOI2016」成绩比较