Skip to content

Commit ca8187d

Browse files
committed
shiro教程-第十七章
1 parent 1127928 commit ca8187d

11 files changed

Lines changed: 94 additions & 37 deletions

File tree

shiro-example-chapter17/src/main/java/com/github/zhangkaitao/shiro/chapter17/realm/UserRealm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal
2727
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
2828
authorizationInfo.setRoles(userService.findRoles(username));
2929
authorizationInfo.setStringPermissions(userService.findPermissions(username));
30-
30+
System.out.println(userService.findPermissions(username));
3131
return authorizationInfo;
3232
}
3333

shiro-example-chapter17/src/main/java/com/github/zhangkaitao/shiro/chapter17/service/ResourceService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ public interface ResourceService {
2121
Resource findOne(Long resourceId);
2222
List<Resource> findAll();
2323

24+
/**
25+
* 得到资源对应的权限字符串
26+
* @param resourceIds
27+
* @return
28+
*/
2429
Set<String> findPermissions(Set<Long> resourceIds);
2530

31+
/**
32+
* 根据用户权限得到菜单
33+
* @param permissions
34+
* @return
35+
*/
2636
List<Resource> findMenus(Set<String> permissions);
2737
}

shiro-example-chapter17/src/main/java/com/github/zhangkaitao/shiro/chapter17/service/RoleService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ public interface RoleService {
2020
public Role findOne(Long roleId);
2121
public List<Role> findAll();
2222

23+
/**
24+
* 根据角色编号得到角色标识符列表
25+
* @param roleIds
26+
* @return
27+
*/
2328
Set<String> findRoles(Long... roleIds);
29+
30+
/**
31+
* 根据角色编号得到权限字符串列表
32+
* @param roleIds
33+
* @return
34+
*/
2435
Set<String> findPermissions(Long[] roleIds);
2536
}

shiro-example-chapter17/src/main/java/com/github/zhangkaitao/shiro/chapter17/web/controller/UserController.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public class UserController {
3030
@Autowired
3131
private RoleService roleService;
3232

33-
@RequiresPermissions("role:view")
33+
@RequiresPermissions("user:view")
3434
@RequestMapping(method = RequestMethod.GET)
3535
public String list(Model model) {
3636
model.addAttribute("userList", userService.findAll());
3737
return "user/list";
3838
}
3939

40-
@RequiresPermissions("role:create")
40+
@RequiresPermissions("user:create")
4141
@RequestMapping(value = "/create", method = RequestMethod.GET)
4242
public String showCreateForm(Model model) {
4343
setCommonData(model);
@@ -46,15 +46,15 @@ public String showCreateForm(Model model) {
4646
return "user/edit";
4747
}
4848

49-
@RequiresPermissions("role:create")
49+
@RequiresPermissions("user:create")
5050
@RequestMapping(value = "/create", method = RequestMethod.POST)
5151
public String create(User user, RedirectAttributes redirectAttributes) {
5252
userService.createUser(user);
5353
redirectAttributes.addFlashAttribute("msg", "新增成功");
5454
return "redirect:/user";
5555
}
5656

57-
@RequiresPermissions("role:update")
57+
@RequiresPermissions("user:update")
5858
@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
5959
public String showUpdateForm(@PathVariable("id") Long id, Model model) {
6060
setCommonData(model);
@@ -63,15 +63,15 @@ public String showUpdateForm(@PathVariable("id") Long id, Model model) {
6363
return "user/edit";
6464
}
6565

66-
@RequiresPermissions("role:update")
66+
@RequiresPermissions("user:update")
6767
@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
6868
public String update(User user, RedirectAttributes redirectAttributes) {
6969
userService.updateUser(user);
7070
redirectAttributes.addFlashAttribute("msg", "修改成功");
7171
return "redirect:/user";
7272
}
7373

74-
@RequiresPermissions("role:delete")
74+
@RequiresPermissions("user:delete")
7575
@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
7676
public String showDeleteForm(@PathVariable("id") Long id, Model model) {
7777
setCommonData(model);
@@ -80,7 +80,7 @@ public String showDeleteForm(@PathVariable("id") Long id, Model model) {
8080
return "user/edit";
8181
}
8282

83-
@RequiresPermissions("role:delete")
83+
@RequiresPermissions("user:delete")
8484
@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
8585
public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
8686
userService.deleteUser(id);
@@ -89,15 +89,15 @@ public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAtt
8989
}
9090

9191

92-
@RequiresPermissions("role:update")
92+
@RequiresPermissions("user:update")
9393
@RequestMapping(value = "/{id}/changePassword", method = RequestMethod.GET)
9494
public String showChangePasswordForm(@PathVariable("id") Long id, Model model) {
9595
model.addAttribute("user", userService.findOne(id));
9696
model.addAttribute("op", "修改密码");
9797
return "user/changePassword";
9898
}
9999

100-
@RequiresPermissions("role:update")
100+
@RequiresPermissions("user:update")
101101
@RequestMapping(value = "/{id}/changePassword", method = RequestMethod.POST)
102102
public String changePassword(@PathVariable("id") Long id, String newPassword, RedirectAttributes redirectAttributes) {
103103
userService.changePassword(id, newPassword);

shiro-example-chapter17/src/main/java/com/github/zhangkaitao/shiro/chapter17/web/exception/DefaultExceptionHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ public ModelAndView processUnauthenticatedException(NativeWebRequest request, Un
2828
mv.setViewName("unauthorized");
2929
return mv;
3030
}
31-
3231
}

shiro-example-chapter17/src/main/resources/spring-config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<context:component-scan base-package="com.github.zhangkaitao.shiro.chapter17">
1717
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
1818
</context:component-scan>
19+
1920
<!-- 开启AOP监听 只对当前配置文件有效 -->
2021
<aop:aspectj-autoproxy expose-proxy="true"/>
2122

shiro-example-chapter17/src/main/webapp/WEB-INF/jsp/index.jsp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
22
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3+
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
34
<html>
45
<head>
56
<title>Shiro综合案例</title>
@@ -9,7 +10,7 @@
910

1011
<iframe name="content" class="ui-layout-center"
1112
src="${pageContext.request.contextPath}/welcome" frameborder="0" scrolling="auto"></iframe>
12-
<div class="ui-layout-north">欢迎学习Shiro综合案例</div>
13+
<div class="ui-layout-north">欢迎[<shiro:principal/>]学习Shiro综合案例,<a href="${pageContext.request.contextPath}/logout">退出</a></div>
1314
<div class="ui-layout-south">
1415
获取源码:<a href="https://github.com/zhangkaitao/shiro-example" target="_blank">https://github.com/zhangkaitao/shiro-example</a>
1516
</div>

shiro-example-chapter17/src/main/webapp/WEB-INF/jsp/organization/maintain.jsp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
22
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
33
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
4+
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
45
<html>
56
<head>
67
<title></title>
@@ -18,15 +19,25 @@
1819
<form:label path="name">名称:</form:label>
1920
<form:input path="name"/>
2021
</div>
22+
<shiro:hasPermission name="organization:update">
23+
<form:button id="updateBtn">修改</form:button>
24+
</shiro:hasPermission>
2125

22-
<form:button id="updateBtn">修改</form:button>
23-
<c:if test="${not organization.rootNode}">
24-
<form:button id="deleteBtn">删除</form:button>
25-
</c:if>
26-
<form:button id="appendChildBtn">添加子节点</form:button>
27-
<c:if test="${not organization.rootNode}">
28-
<form:button id="moveBtn">移动节点</form:button>
29-
</c:if>
26+
<shiro:hasPermission name="organization:delete">
27+
<c:if test="${not organization.rootNode}">
28+
<form:button id="deleteBtn">删除</form:button>
29+
</c:if>
30+
</shiro:hasPermission>
31+
32+
<shiro:hasPermission name="organization:create">
33+
<form:button id="appendChildBtn">添加子节点</form:button>
34+
</shiro:hasPermission>
35+
36+
<shiro:hasPermission name="organization:update">
37+
<c:if test="${not organization.rootNode}">
38+
<form:button id="moveBtn">移动节点</form:button>
39+
</c:if>
40+
</shiro:hasPermission>
3041
</form:form>
3142

3243
<script src="${pageContext.request.contextPath}/static/js/jquery-1.11.0.min.js"></script>

shiro-example-chapter17/src/main/webapp/WEB-INF/jsp/resource/list.jsp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
22
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3+
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
34
<html>
45
<head>
56
<title></title>
@@ -38,14 +39,20 @@
3839
<td>${resource.url}</td>
3940
<td>${resource.permission}</td>
4041
<td>
41-
<c:if test="${resource.type ne 'button'}">
42-
<a href="${pageContext.request.contextPath}/resource/${resource.id}/appendChild">添加子节点</a>
43-
|
44-
</c:if>
45-
<a href="${pageContext.request.contextPath}/resource/${resource.id}/update">修改</a>
42+
<shiro:hasPermission name="resource:create">
43+
<c:if test="${resource.type ne 'button'}">
44+
<a href="${pageContext.request.contextPath}/resource/${resource.id}/appendChild">添加子节点</a>
45+
</c:if>
46+
</shiro:hasPermission>
47+
48+
<shiro:hasPermission name="resource:update">
49+
<a href="${pageContext.request.contextPath}/resource/${resource.id}/update">修改</a>
50+
</shiro:hasPermission>
4651
<c:if test="${not resource.rootNode}">
47-
|
48-
<a class="deleteBtn" href="#" data-id="${resource.id}">删除</a>
52+
53+
<shiro:hasPermission name="resource:delete">
54+
<a class="deleteBtn" href="#" data-id="${resource.id}">删除</a>
55+
</shiro:hasPermission>
4956
</c:if>
5057
</td>
5158
</tr>

shiro-example-chapter17/src/main/webapp/WEB-INF/jsp/role/list.jsp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
22
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
33
<%@taglib prefix="zhangfn" uri="http://github.com/zhangkaitao/tags/zhang-functions" %>
4+
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
45
<html>
56
<head>
67
<title></title>
@@ -12,7 +13,9 @@
1213
<div class="message">${msg}</div>
1314
</c:if>
1415

15-
<a href="${pageContext.request.contextPath}/role/create">角色新增</a><br/>
16+
<shiro:hasPermission name="role:create">
17+
<a href="${pageContext.request.contextPath}/role/create">角色新增</a><br/>
18+
</shiro:hasPermission>
1619
<table class="table">
1720
<thead>
1821
<tr>
@@ -29,9 +32,13 @@
2932
<td>${role.description}</td>
3033
<td>${zhangfn:resourceNames(role.resourceIds)}</td>
3134
<td>
32-
<a href="${pageContext.request.contextPath}/role/${role.id}/update">修改</a>
33-
|
34-
<a href="${pageContext.request.contextPath}/role/${role.id}/delete">删除</a>
35+
<shiro:hasPermission name="role:update">
36+
<a href="${pageContext.request.contextPath}/role/${role.id}/update">修改</a>
37+
</shiro:hasPermission>
38+
39+
<shiro:hasPermission name="role:delete">
40+
<a href="${pageContext.request.contextPath}/role/${role.id}/delete">删除</a>
41+
</shiro:hasPermission>
3542
</td>
3643
</tr>
3744
</c:forEach>

0 commit comments

Comments
 (0)