上一章使用了Shiro的角色管理,现在加入粒度更小的权限管理,即根据用户角色分配的权限来判断用户能否访问页面
准备实体类和修改数据源
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Getter @Setter public class Role implements Serializable { private String name; private Set<Permission> permissions; public Role(String name) { this.name = name; this.permissions = new HashSet<>(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Getter @Setter @AllArgsConstructor public class Permission { private String name; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class UserService { private static final Map<String, User> userMap = new HashMap<>(); static { userMap.put("user", new User("user", "123456")); userMap.put("admin", new User("admin", "123456")); userMap.put("super", new User("super", "123456")); Role userRole = new Role("user"); Role adminRole = new Role("admin"); Role superRole = new Role("super"); Permission getPermission = new Permission("get"); Permission addPermission = new Permission("add"); adminRole.getPermissions().add(getPermission); superRole.getPermissions().add(getPermission); superRole.getPermissions().add(addPermission); userMap.get("user").getRoles().add(userRole); userMap.get("admin").getRoles().add(userRole); userMap.get("admin").getRoles().add(adminRole); userMap.get("super").getRoles().add(userRole); userMap.get("super").getRoles().add(adminRole); userMap.get("super").getRoles().add(superRole); } public static User getUserByName(String name) { return userMap.get(name); } }
添加、修改测试页面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!—adduser.html--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加用户</title> </head> <body> 添加用户界面 <br/> <a href="/index">返回首页</a> </body> </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!—admin.html--> <!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>管理界面</title> </head> <body> <p th:text="${user.username}+' 管理员您好'"></p> <br> 这里是用户列表 <br/> <a href="/adduser">添加用户</a> <a href="/index">返回首页</a> </body> </html>
配置Controller
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@RequestMapping(value = {"/adduser"}, method = RequestMethod.GET) public String addUser() { return "adduser"; }
在Realm.doGetAuthorizationInfo() 为用户添加拥有的权限
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); User user = (User) principalCollection.getPrimaryPrincipal(); for (Role role : user.getRoles()) { info.addRole(role.getName()); for (Permission permission : role.getPermissions()) { info.addStringPermission(permission.getName()); } } return info; }
配置Shiro拦截器
//权限拦截 filterChainDefinitionMap.put("/user", "authc,roles[user]"); filterChainDefinitionMap.put("/admin", "authc,roles[user,admin],perms[get]"); filterChainDefinitionMap.put("/adduser", "authc,perms[add,get]");
启动服务,在登录之后admin用户能查看用户列表,不能添加用户,super用户不受限制!
到这里,Shiro和SpringBoot的集成就告一段落了。实际使用中肯定会有更多的东西可以挖掘