org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code

末鹿安然 提交于 2019-12-25 04:07:49

问题


i have a junit test case that runs a code like:

if (SecurityUtil.isAuthenticated()) { 

}

and it gives an exception:

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.

My Test Class configuration is as follows:

@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 { }

please advise how to fix this error, thanks.


回答1:


solved as follows:

private ThreadState _threadState;
protected Subject _mockSubject;

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


来源:https://stackoverflow.com/questions/17881188/org-apache-shiro-unavailablesecuritymanagerexception-no-securitymanager-accessi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!