1. ShiroConfig中添加授權過濾器
filterMap.put("/add", "perms[user:add]");
filterMap.put("/update", "perms[user:update]");
// 設置未授權頁面
filterFactoryBean.setUnauthorizedUrl("/unAuth");
這個時候我們登錄點擊add或者update 就會跳轉到未授權頁面
2. 給用戶授權
我們只需要一個 SimpleAhtuorizationInfo
並給他設置授權權限並返回即可
這樣add頁面就可以訪問, update依舊沒有權限
3. 關聯數據庫
數據庫中寫一個簡單的權限
然後我們直接去獲取用戶的權限信息就可以實現權限攔截
數據庫中有兩個賬號, admin這個賬號只可以訪問 add, 而 root 只可以訪問 update
4. shiro和thymeleaf整合使用
1. 引入thymeleaf對shiro拓展的依賴
<!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro -->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>
2. 在ShiroConfig中配置 ShiroDialect
用於 shiro和thymeleaf配合使用
/**
* 配置 shiroDialect 用於thymeleaf和
* @return
*/
@Bean
public ShiroDialect getShiroDialect() {
return new ShiroDialect();
}
3. 在thymeleaf中使用shiro標籤
首頁分別用div套上加上shiro:hasPermission
即可實現,當我們用admin登錄只有添加顯示, root則只有update, 否則啥也沒有