为了账号安全,请及时绑定邮箱和手机立即绑定

报错:org.apache.shiro.authc.UnknownAccountException

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.junit.jupiter.api.Test;

public class AuthenticationTest {
   SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();

   @Before
   public void addUser(){
      simpleAccountRealm.addAccount("Mark","123456");
   }

   @Test
   public void testAuthentication(){
      // 1、构建SecurityManager环境
      DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
      defaultSecurityManager.setRealm(simpleAccountRealm);
      // 2、主体提交认证
      SecurityUtils.setSecurityManager(defaultSecurityManager);
      Subject subject = SecurityUtils.getSubject();

      // 3、  SecurityManager认证
      UsernamePasswordToken token = new UsernamePasswordToken("Mark","123456");
      subject.login(token);

      //    4、Authenticator认证
      System.out.println("isAuthenticated:"+subject.isAuthenticated());
      //       5、 Realm验证

      //退出
      subject.logout();
      System.out.println("isAuthenticated:"+subject.isAuthenticated());

   }

}

org.apache.shiro.authc.UnknownAccountException: Realm [org.apache.shiro.realm.SimpleAccountRealm@8b87145] was unable to find account data for the submitted AuthenticationToken [org.apache.shiro.authc.UsernamePasswordToken - Mark, rememberMe=false].


at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:184)

at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:267)

at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)

at org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106)

at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:274)

at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:260)

at com.imooc.test.AuthenticationTest.testAuthentication(AuthenticationTest.java:30)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:594)

at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)

at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:174)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:170)

at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:117)

at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:61)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:130)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:120)

at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:134)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:118)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:117)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:79)

at java.util.ArrayList.forEach(ArrayList.java:1249)

at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:134)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:120)

at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:134)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:118)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:117)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:79)

at java.util.ArrayList.forEach(ArrayList.java:1249)

at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:134)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:120)

at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:134)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:118)

at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:117)

at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:79)

at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)

at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)

at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)

at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)

at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)

at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)



Process finished with exit code -1


正在回答

4 回答

密码错了

1 回复 有任何疑惑可以回复我~
#1

叶0528 提问者

我修改了密码。还报错,报错信息变了。
2019-01-16 回复 有任何疑惑可以回复我~
#2

叶0528 提问者

就是说@Before这个注解没有起到作用
2019-01-16 回复 有任何疑惑可以回复我~

Spring Boot 2.2.9 默认使用的是junit 5,在junit 5中 @Before 被 @BeforeEach代替

0 回复 有任何疑惑可以回复我~

我也是这个问题

0 回复 有任何疑惑可以回复我~

我也是同样的错误


0 回复 有任何疑惑可以回复我~
#1

叶0528 提问者

我今天又试了一下,是可以的。@Before这个注解。pom.xml中junit 4.12
2019-05-27 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

报错:org.apache.shiro.authc.UnknownAccountException

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信