一直想在shiro權限這塊加入緩存,使用redis是再合適不過了,恰巧已經有大佬將shiro和redis整合在一起使用了,只需在引入pom文件中引入即可。
<dependency> <groupId>org.crazycake</groupId> <artifactId>shiro-redis</artifactId> <version>3.2.3</version> </dependency>
但是是使用的時候,權限配置這塊,也就是重寫shiro的doGetAuthorizationInfo方法這里,一直進不來,完整的控制台異常信息如下:
org.crazycake.shiro.exception.PrincipalInstanceException: class com.company.project.manage.entity.UserInfo must has getter for field: id We need a field to identify this Cache Object in Redis. So you need to defined an id field which you can get unique id to identify this principal. For example, if you use UserInfo as Principal class, the id field maybe userId, userName, email, etc. For example, getUserId(), getUserName(), getEmail(), etc. Default value is "id", that means your principal object has a method called "getId()"
大概意思就是UserInfo對象中必須要有id屬性,並且要有對應的get方法。在身份驗證的時候就已經將userinfo信息傳遞給了shiro,然后redis做緩存的時候需要key,key的值就與userinfo里面的id值有關。
點開UserInfo對象一看,尷了個尬,主鍵的命名使用的是uid
最后把主鍵換成id,就運行正常了。演示項目在我的github上面,shiro-redis插件的整合可以查看: