【问题标题】:org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling codeorg.apache.shiro.UnavailableSecurityManagerException:调用代码无法访问 SecurityManager
【发布时间】:2013-07-26 17:07:47
【问题描述】:

我有一个运行如下代码的 junit 测试用例:

if (SecurityUtil.isAuthenticated()) { 

}

它给出了一个例外:

org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton.  This is an invalid application configuration.

我的测试类配置如下:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ WebContextTestExecutionListener.class,
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class })
@ActiveProfiles("test")
@DirtiesContext
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {
        SpringConfig.class, SpringTestingConfig.class,
        SpringLocalContainerJPAConfig.class, CustomConfiguration.class })
public class MyTestClass { }

请告知如何解决此错误,谢谢。

【问题讨论】:

    标签: spring junit shiro


    【解决方案1】:

    解决如下:

    private ThreadState _threadState;
    protected Subject _mockSubject;
    
            @Before
            public void before() {
                _mockSubject = Mockito.mock(Subject.class);
                _threadState = new SubjectThreadState(_mockSubject);
                _threadState.bind();
            }
    

    【讨论】:

      猜你喜欢
      • 2019-11-02
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多