shiro学习07-用户以及登录-subjectCallable类

说实话,这个类我没怎么明白,我写这个都是看的javadoc,是对javadoc的翻译。

这个类将一个Callable关联到subject上,来保证当callable运行时的subject的管理,通过这个callbale,可以保证在运行时的securityUtils.getSubject正确执行,即使callable运行的线程不是产生这个callable的线程。这样就保证了即使是异步的执行,也能正确的获得subject

 

这个类实现了Callable,所以可以被直接用作一个Callable,并且必须实现其call方法。

他的call方法如下:

public V call() throws Exception {

        try {

            threadState.bind();//subejectsecurityManager绑定到当前线程。并保留之前在当前线程上的内容。

            return doCall(this.callable);//执行过滤器的过滤和后面的servlet.

        } finally {

            threadState.restore();//subjectsecurityManager从当前线程上解绑定,然后将之前保存的内容再次绑定。

        }

}

 

protected V doCall(Callable<V> target) throws Exception {

    returntarget.call();//开始执行,

}

执行的内容为abstractShiroFilter中指定的,在doFilter方法中:

            subject.execute(new Callable() {

                public Object call() throws Exception {

                    updateSessionLastAccessTime(request, response);

                    executeChain(request, response, chain);

                    returnnull;

                }

            });

猜你喜欢

转载自suichangkele.iteye.com/blog/2276902