七、Shiro授權-使用Shiro過濾器實現授權頁面攔截
1. 在ShiroConfig中添加過濾器
2. 添加設置未授權頁面
3. 測試
八、Shiro授權-編寫資源授權邏輯
九、Shiro授權-關聯數據庫動態授權
1. 修改數據表
2. 一系列小修改
3. 修改UserRealm中的doGetAuthorizationInfo方法
4. 登錄不同權限用戶進行測試
十、ThymeLeaf和shiro標籤整合使用
1. 導入thymeleaf對shiro的擴展座標
2. 配置ShiroDialect
3. 在頁面上使用shiro標籤
4. 運行測試
七、Shiro授權-使用Shiro過濾器實現授權頁面攔截
1. 在ShiroConfig中添加過濾器
//授權過濾器:授權攔截後,shiro會自動跳轉到未授權頁面
filterMap.put("/add", "perms[user:add]");
filterMap.put("/*", "authc");
1
2
3
Tips:注意要寫在/*之前,否則不會攔截
2. 添加設置未授權頁面
(1)ShiroConfig中
//修改自動跳轉的未授權頁面
shiroFilterFactoryBean.setUnauthorizedUrl("/unAuth");
1
2
(2)UserController中
@RequestMapping("/unAuth")
public String unAuth() {
return "unAuth";
}
1
2
3
4
(3)添加unAuth.html
3. 測試
登錄認證之後,訪問/add頁面會提示未授權,而/update可以正常訪問。
八、Shiro授權-編寫資源授權邏輯
剛纔打印的log日誌中可以看到,只要訪問了需要授權訪問的資源,就會執行UserRealm中的doGetAuthenticationInfo()方法,在該方法中給資源進行授權。
/**
* 執行授權邏輯
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
System.out.println("執行授權邏輯");
//給資源進行授權
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
//添加資源的授權字符串
info.addStringPermission("user:add");
return info;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
測試查看效果:日誌中可以看到執行了該授權邏輯,現在可以訪問/add了
九、Shiro授權-關聯數據庫動態授權
1. 修改數據表
給user表添加perms字段,插入兩個測試用戶
2. 一系列小修改
(1)User.java:添加perms屬性和getter/setter
(2)UserMapper.java:
public User findById(Integer id);
1
(3)UserMapper.xml
<select id="findById" parameterType="int" resultType="User" >
select id,username,password,perms from user where id = #{value}
</select>
1
2
3
(4)UserService.java
public User findById(Integer id);
1
(5)UserServiceImpl.java
@Override
public User findById(Integer id) {
return userMapper.findById(id);
}
1
2
3
4
(6)給/update添加資源攔截
filterMap.put("/update", "perms[user:update]");
1
3. 修改UserRealm中的doGetAuthorizationInfo方法
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
System.out.println("執行授權邏輯");
//給資源進行授權
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
/*//添加資源的授權字符串
info.addStringPermission("user:add");*/
//獲取當前用戶
Subject subject=SecurityUtils.getSubject();
User user=(User)subject.getPrincipal();
//到數據庫查詢當前登錄用戶的授權字符串
User dbUser=userService.findById(user.getId());//通過當前登錄用戶id查找的數據庫用戶
info.addStringPermission(dbUser.getPerms());
return info;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
將doGetAuthenticationInfo()方法的返回修改爲
return new SimpleAuthenticationInfo(user,user.getPassword(),"");
因爲User user=(User)subject.getPrincipal(); 所取得的當前登錄用戶就是從這裏來的
4. 登錄不同權限用戶進行測試
各自有了各自的權限。
十、ThymeLeaf和shiro標籤整合使用
1. 導入thymeleaf對shiro的擴展座標
<!-- 導入thymeleaf對shiro的擴展座標 -->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>
1
2
3
4
5
6
2. 配置ShiroDialect
@Bean
public ShiroDialect getShiroDialect(){
return new ShiroDialect();
}
1
2
3
4
3. 在頁面上使用shiro標籤
<div shiro:hasPermission="user:add">
進入用戶新增頁面:<a href="add">用戶新增</a>
</div>
<div shiro:hasPermission="user:update">
進入用戶更新頁面:<a href="update">用戶更新</a>
</div>
1
2
3
4
5
6
4. 運行測試
不同權限用戶登錄,只顯示了他有權限看到的內容。