- 浏览: 156702 次
- 性别:
- 来自: 大连
文章分类
最新评论
-
eclipseakwolf:
很棒的文章
Shiro User Manual-Authentication -
xugangqiang:
very good
Java Concurrent Programming (1) -
jlcon:
是不是AbstractShiroTest还需要继承EasyMo ...
Shiro User Manual-Testing -
jlcon:
createNiceMock这个EasyMockSupport ...
Shiro User Manual-Testing -
Technoboy:
53873039oycg 写道楼主:问下,你的那个dao接口的 ...
Shiro Example
1. Test Setup
创建的Subject实例,必须要绑定到当前线程上,当执行完毕,需要解除绑定。现在流行的测试框架像JUnit,TestNG都支持"setup"和"teardown"。我们可以利用这个特性模拟Shiro在应用中的完整操作。我们创建了AbstractShiroTest抽象类,可用于单元测试和集成测试:
2. Unit Testing
由于单元测试测试的是业务逻辑,那么就可以使用EasyMock和Mockito对逻辑依赖的Shiro API进行mock。Shiro对此有很好的支持-可以模拟Subject实例,只需要注意,Subject实例要与当前线程绑定。下面这个例子使用了EasyMock:
可以看出,我们没有初始化SecurityManager和Realm实例,只是通过setSubject方法将模拟的Subject实例绑定到当前线程上。这就保证了测试时,没有任何问题。需要注意,setSubject将模拟的Subject对象绑定到当前线程,当再次以不同的Subject参数调用setSubject方法或执行clearSubject()方法时,才解除Subject与线程的绑定。
3. Integration Testing
也可以很容易地在Shiro里做集成测试。SecurityManager实例和其封装的组件,都是轻量级的POJO,占用很少的内存。这意味着,可以在每次测试时,创建和销毁SecurityManager实例。执行集成测试时,可以使用真实的SecurityManager和Subject实例:
可以看出,集成测试和单元测试有不一样的地方:
1. 通过@BeforeClass创建了真实的SecurityManager实例。
2. @Test方法里也创建了真实的Subject,并绑定到线程里。
创建的Subject实例,必须要绑定到当前线程上,当执行完毕,需要解除绑定。现在流行的测试框架像JUnit,TestNG都支持"setup"和"teardown"。我们可以利用这个特性模拟Shiro在应用中的完整操作。我们创建了AbstractShiroTest抽象类,可用于单元测试和集成测试:
import org.apache.shiro.SecurityUtils; import org.apache.shiro.UnavailableSecurityManagerException; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.subject.Subject; import org.apache.shiro.subject.support.SubjectThreadState; import org.apache.shiro.util.LifecycleUtils; import org.apache.shiro.util.ThreadState; import org.junit.AfterClass; /** * Abstract test case enabling Shiro in test environments. */ public abstract class AbstractShiroTest { private static ThreadState subjectThreadState; public AbstractShiroTest() { } /** * Allows subclasses to set the currently executing {@link Subject} instance. * * @param subject the Subject instance */ protected void setSubject(Subject subject) { clearSubject(); subjectThreadState = createThreadState(subject); subjectThreadState.bind(); } protected Subject getSubject() { return SecurityUtils.getSubject(); } protected ThreadState createThreadState(Subject subject) { return new SubjectThreadState(subject); } /** * Clears Shiro's thread state, ensuring the thread remains clean for future test execution. */ protected void clearSubject() { doClearSubject(); } private static void doClearSubject() { if (subjectThreadState != null) { subjectThreadState.clear(); subjectThreadState = null; } } protected static void setSecurityManager(SecurityManager securityManager) { SecurityUtils.setSecurityManager(securityManager); } protected static SecurityManager getSecurityManager() { return SecurityUtils.getSecurityManager(); } @AfterClass public static void tearDownShiro() { doClearSubject(); try { SecurityManager securityManager = getSecurityManager(); LifecycleUtils.destroy(securityManager); } catch (UnavailableSecurityManagerException e) { //we don't care about this when cleaning up the test environment //(for example, maybe the subclass is a unit test and it didn't // need a SecurityManager instance because it was using only // mock Subject instances) } setSecurityManager(null); } }
2. Unit Testing
由于单元测试测试的是业务逻辑,那么就可以使用EasyMock和Mockito对逻辑依赖的Shiro API进行mock。Shiro对此有很好的支持-可以模拟Subject实例,只需要注意,Subject实例要与当前线程绑定。下面这个例子使用了EasyMock:
import org.apache.shiro.subject.Subject; import org.junit.After; import org.junit.Test; import static org.easymock.EasyMock.*; /** * Simple example test class showing how one may perform unit tests for code that requires Shiro APIs. */ public class ExampleShiroUnitTest extends AbstractShiroTest { @Test public void testSimple() { //1. Create a mock authenticated Subject instance for the test to run: Subject subjectUnderTest = createNiceMock(Subject.class); expect(subjectUnderTest.isAuthenticated()).andReturn(true); //2. Bind the subject to the current thread: setSubject(subjectUnderTest); //perform test logic here. Any call to //SecurityUtils.getSubject() directly (or nested in the //call stack) will work properly. } @After public void tearDownSubject() { //3. Unbind the subject from the current thread: clearSubject(); } }
可以看出,我们没有初始化SecurityManager和Realm实例,只是通过setSubject方法将模拟的Subject实例绑定到当前线程上。这就保证了测试时,没有任何问题。需要注意,setSubject将模拟的Subject对象绑定到当前线程,当再次以不同的Subject参数调用setSubject方法或执行clearSubject()方法时,才解除Subject与线程的绑定。
3. Integration Testing
也可以很容易地在Shiro里做集成测试。SecurityManager实例和其封装的组件,都是轻量级的POJO,占用很少的内存。这意味着,可以在每次测试时,创建和销毁SecurityManager实例。执行集成测试时,可以使用真实的SecurityManager和Subject实例:
import org.apache.shiro.config.IniSecurityManagerFactory; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.subject.Subject; import org.apache.shiro.util.Factory; import org.junit.After; import org.junit.BeforeClass; import org.junit.Test; public class ExampleShiroIntegrationTest extends AbstractShiroTest { @BeforeClass public static void beforeClass() { //0. Build and set the SecurityManager used to build Subject instances used in your tests // This typically only needs to be done once per class if your shiro.ini doesn't change, // otherwise, you'll need to do this logic in each test that is different Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:test.shiro.ini"); setSecurityManager(factory.getInstance()); } @Test public void testSimple() { //1. Build the Subject instance for the test to run: Subject subjectUnderTest = new Subject.Builder(getSecurityManager()).buildSubject(); //2. Bind the subject to the current thread: setSubject(subjectUnderTest); //perform test logic here. Any call to //SecurityUtils.getSubject() directly (or nested in the //call stack) will work properly. } @AfterClass public void tearDownSubject() { //3. Unbind the subject from the current thread: clearSubject(); } }
可以看出,集成测试和单元测试有不一样的地方:
1. 通过@BeforeClass创建了真实的SecurityManager实例。
2. @Test方法里也创建了真实的Subject,并绑定到线程里。
评论
2 楼
jlcon
2013-07-22
是不是AbstractShiroTest还需要继承EasyMockSupport?
1 楼
jlcon
2013-07-22
createNiceMock这个EasyMockSupport的方法怎么调用的?
发表评论
-
Shiro Filters
2013-05-06 14:07 30091. Overview 对于web应用 ... -
Shiro Example
2013-04-23 16:11 17321. 说明: maven项目,基于Spring3.1,My ... -
Shiro用户手册-中文版pdf
2013-04-21 19:47 2091Apache Shiro用户手册中文版。 -
Shiro User Manual-Custom Subjects
2013-04-18 11:03 20441. Custom Subject Instances Sh ... -
Shiro User Manual-Architecture
2013-04-16 11:02 12541. Overview Shiro的设计目标是通过直观而简易 ... -
Shiro User Manual-Tutorial
2013-04-16 10:52 22491. Your First Apache Shiro Appl ... -
Shiro User Manual-Introduction
2013-04-16 10:42 11811. What is Apache Shiro? Shiro ... -
Shiro User Manual-Command Line Hasher
2013-04-19 11:49 17871. Overview Shiro1.2及其以 ... -
Shiro User Manual-Configuration
2013-04-16 11:19 18641. Configuration Shiro可以 ... -
Shiro User Manual-Web Support
2013-04-18 10:41 27691. Configuration 将Shiro集成到web应 ... -
Shiro User Manual-Integrating Into Spring
2013-04-18 11:23 20831. Overview Shiro的JavaBeans兼容性 ... -
Shiro User Manual-Caching
2013-04-18 10:52 17991. Caching Shiro团队了解 ... -
Shiro User Manual-Session Management
2013-04-17 22:41 68641. Session Management Shiro提供了 ... -
Shiro User Manual-Realms
2013-04-17 11:54 19211. Realms Realm是可以访问应用系统中数据,例如 ... -
Shiro User Manual-Authorization-Permissions
2013-04-17 09:31 19851. Wildcard Permissions 为了 ... -
Shiro User Manual-Authorization
2013-04-17 09:22 23241. Authorization Authorizatio ... -
Shiro User Manual-Authentication
2013-04-16 11:28 33201.Authentication Authenticatio ...
相关推荐
shiro(shiro-all-1.8.0.jar)
赠送jar包:shiro-core-1.4.0.jar; 赠送原API文档:shiro-core-1.4.0-javadoc.jar; 赠送源代码:shiro-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-core-1.4.0.pom; 包含翻译后的API文档:shiro-core...
标题提到的"shiro-attack-4.7.0-SNAPSHOT-all.zip"很可能是针对Apache Shiro的安全测试工具或者漏洞利用工具包,其主要目的是帮助开发者检测和防范Shiro框架相关的安全问题。 描述中的"序列化验证工具"可能是指该...
赠送jar包:shiro-config-core-1.4.0.jar; 赠送原API文档:shiro-config-core-1.4.0-javadoc.jar; 赠送源代码:shiro-config-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-config-core-1.4.0.pom; ...
解决:升級1.7后附件...shiro-cas-1.7.0.jar shiro-core-1.7.0.jar shiro-ehcache-1.7.0.jar shiro-spring-1.7.0.jar shiro-web-1.7.0.jar CustomShiroFilterFactoryBean.java spring-context-shiro.xml 修改说明.txt
赠送jar包:shiro-ehcache-1.4.0.jar; 赠送原API文档:shiro-ehcache-1.4.0-javadoc.jar; 赠送源代码:shiro-ehcache-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-ehcache-1.4.0.pom; 包含翻译后的API文档...
shiro-all-1.7.1.jar,shiro-aspectj-1.7.1.jar,shiro-cache-1.7.1.jar,shiro-config-core-1.7.1.jar,shiro-config-ogdl-1.7.1.jar,shiro-core-1.7.1.jar,shiro-crypto-cipher-1.7.1.jar,shiro-crypto-core-1.7.1.jar...
赠送jar包:shiro-crypto-core-1.4.0.jar; 赠送原API文档:shiro-crypto-core-1.4.0-javadoc.jar; 赠送源代码:shiro-crypto-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-crypto-core-1.4.0.pom; ...
赠送jar包:shiro-crypto-cipher-1.4.0.jar; 赠送原API文档:shiro-crypto-cipher-1.4.0-javadoc.jar; 赠送源代码:shiro-crypto-cipher-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-crypto-cipher-1.4.0....
赠送jar包:shiro-crypto-core-1.4.0.jar; 赠送原API文档:shiro-crypto-core-1.4.0-javadoc.jar; 赠送源代码:shiro-crypto-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-crypto-core-1.4.0.pom; ...
赠送jar包:shiro-cas-1.2.3.jar; 赠送原API文档:shiro-cas-1.2.3-javadoc.jar; 赠送源代码:shiro-cas-1.2.3-sources.jar; 赠送Maven依赖信息文件:shiro-cas-1.2.3.pom; 包含翻译后的API文档:shiro-cas-...
赠送jar包:shiro-config-core-1.4.0.jar; 赠送原API文档:shiro-config-core-1.4.0-javadoc.jar; 赠送源代码:shiro-config-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-config-core-1.4.0.pom; ...
赠送jar包:shiro-config-ogdl-1.4.0.jar; 赠送原API文档:shiro-config-ogdl-1.4.0-javadoc.jar; 赠送源代码:shiro-config-ogdl-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-config-ogdl-1.4.0.pom; ...
赠送jar包:shiro-core-1.3.2.jar; 赠送原API文档:shiro-core-1.3.2-javadoc.jar; 赠送源代码:shiro-core-1.3.2-sources.jar; 包含翻译后的API文档:shiro-core-1.3.2-javadoc-API文档-中文(简体)版.zip ...
赠送jar包:shiro-cas-1.2.3.jar; 赠送原API文档:shiro-cas-1.2.3-javadoc.jar; 赠送源代码:shiro-cas-1.2.3-sources.jar; 赠送Maven依赖信息文件:shiro-cas-1.2.3.pom; 包含翻译后的API文档:shiro-cas-...
赠送jar包:shiro-cache-1.4.0.jar; 赠送原API文档:shiro-cache-1.4.0-javadoc.jar; 赠送源代码:shiro-cache-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-cache-1.4.0.pom; 包含翻译后的API文档:shiro-...
shiro shiro-core-1.7.1 jar shiro漏洞
赠送jar包:shiro-cache-1.4.0.jar; 赠送原API文档:shiro-cache-1.4.0-javadoc.jar; 赠送源代码:shiro-cache-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-cache-1.4.0.pom; 包含翻译后的API文档:shiro-...
赠送jar包:shiro-event-1.4.0.jar; 赠送原API文档:shiro-event-1.4.0-javadoc.jar; 赠送源代码:shiro-event-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-event-1.4.0.pom; 包含翻译后的API文档:shiro-...
`shiro-1.7.1.zip`是一个包含Shiro框架不同组件的压缩包,主要用于web应用的安全控制。 1. **shiro-core-1.7.1.jar**: 这是Shiro的核心库,包含了基本的身份验证、授权和会话管理功能。它提供了一套API,开发者可以...