Shiro整合Redis:使用shiro-redis插件踩的坑

  一直想在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插件的整合可以查看:

https://mrbird.cc/Spring-Boot-Shiro%20cache.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章