最近发现 simpleAuthenticationIfo 中的principal疑问。
场景是这个样子的
//此处使用的是user对象,不是username
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
user,
user.getPassword(),
getName()
);
另外一种是
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
username,
user.getPassword(),
getName()
);
这两种写法都可以,那么问题来的。为什么都可以呢?到底principle到底是怎么使用的呢?
后来发现principle用作用户的唯一标识,他并不参与密码等之类的校对。
这个是别人的一些记录:
第一个参数principal使用一个可以表示该用户对象唯一身份的标识即可,其实不建议直接使用用户对象本身,因为如果开启了授权缓存,shiro在存放授权信息到缓存中时会将principal处理后作为key值。使用对象可能会引发一些不可控现象。