subject.getPrincipal()强制转换类型错误

    关键是看SimpleAuthenticationInfo的方法有没有配置正确。

     //此处使用的是user对象,不是username
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                user,
                user.getPassword(),
                getName()
        );

这个东西是在realm中的,第一个参数user,这里好多地方传的时候都是user对象,但是都在备注用户名。可是我如果传入username,就会报类型转换问题。

但是在开涛大神的博客中,无状态的shiro里,那边给出的例子是传username。我自己测试的,可以传username,也可以传user对象,仅限他那边一段代码。网上有文章说,这里其实是user和username的集合,后端是分两个字段接收的。由于时间的问题,没有深入里了解这块,传user对象是OK的。

第二个字段是user.getPassword(),注意这里是指从数据库中获取的password。

第三个字段是realm,即当前realm的名称。

看了几篇文章介绍说,这块对比逻辑是先对比username,但是username肯定是相等的,所以真正对比的是password。从这里传入的password(这里是从数据库获取的)和token(filter中登录时生成的)中的password做对比,如果相同就允许登录,不相同就抛出异常。

如果验证成功,最终这里返回的信息authenticationInfo 的值与传入的第一个字段的值相同(我这里传的是user对象)。

另外还有的原因可能是springboot的热部署造成的,去掉试试。

来源于:https://blog.csdn.net/qq_35981283/article/details/78634575

猜你喜欢

转载自blog.csdn.net/weixin_42456466/article/details/80744371