笔者因为项目转型的原因,对Apache Shiro安全框架做了一点研究工作,故想写点东西以便将来查阅。之所以选择Shiro也是看了很多人的推荐,号称功能丰富强大,而且易于使用。实践下来的确如大多数人所说简约优美,小巧精悍。

介绍demo项目前,简单说明一下Shiro框架的特性。

1.  Apache Shiro Features

从上图可以看出Shiro具备应用程序安全框架的四大基石”:身份验证、授权、会话管理和密码。

Authentication有时被称为‘登录’,这是需要明确用户是谁

Authorization访问控制,即确定‘谁’对‘什么’有访问权限。

Session Management管理特定用户的会话,即使在非web或EJB应用程序中也是如此。

Cryptography使用加密算法保持数据安全,但易于使用。

在不同的应用程序环境中,还有更多的特性来支持和增强这些关注点,特别是:

Web SupportShiro的Web支持API帮助轻松地保护web应用程序。

Caching缓存是ApacheShiro的API中的第一等公民,以确保安全操作同时保持快速和高效。

ConcurrencyApacheShiro支持具有并发特性的多线程应用程序。

Testing提供测试支持,以帮助编写单元和集成测试,并确保代码如预期的安全。

Run as允许用户假定另一个用户的身份(如果允许的话)的特性,有时在管理场景中很有用。

Remember Me记住用户在会话中的身份,这样他们就只需要在强制的情况下输入口令登录。

2. High-Level Overview

Shiro的体系结构有三个主要概念:Subject、SecurityManager和Realms。下图展现了它的运行原理,

主题:主题本质上是当前正在执行的用户。虽然“用户”这个词通常意味着一个人,一个主题可以是一个人,但它也可以代表一个第三方服务、守护进程帐户、cron作业或任何类似的东西-基本上是任何当前与软件交互的东西。Subject实例都绑定到(并且需要)一个SecurityManager。当与主题交互时,这些交互转化为与SecurityManager的特定主题交互。

SecurityManagerSecurityManager是Shiro体系结构的核心,它将其内部安全组件协调在一起形成一个对象图。然而,一旦为应用程序配置了SecurityManager及其内部对象图,它通常会被单独使用,应用程序开发人员将几乎所有的时间都花在Subject API上。当与一个主题交互时,实际上是幕后的SecurityManager为任何主题安全操作做了所有繁重的工作。

领域:领域充当Shiro和应用程序安全数据之间的“桥梁”或“连接器”。当涉及到实际与用户帐户等安全相关的数据交互以执行身份验证(登录)和授权(访问控制)时,Shiro从一个或多个为应用程序配置的领域中查找数据。从这个意义上说,领域本质上是一个特定于安全的DAO:它封装数据源的连接细节,并根据需要将相关数据提供给Shiro。配置Shiro时,必须指定至少一个用于身份验证和/或授权的域。SecurityManager可以配置多个Realm,但至少需要一个。Shiro提供了开箱即用的领域,以连接到许多安全数据源(也称为目录),如LDAP、关系数据库(JDBC)、INI和属性文件等文本配置源。

3. Detailed Architecture

4. 过滤器

当 Shiro 被运用到 web 项目时,Shiro 会自动创建一些默认的过滤器对客户端请求进行过滤。以下是 Shiro 内置过滤器:

过滤器简称

对应的 Java 类

anon

org.apache.shiro.web.filter.authc.AnonymousFilter

authc

org.apache.shiro.web.filter.authc.FormAuthenticationFilter

authcBasic

org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter

perms

org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter

port

org.apache.shiro.web.filter.authz.PortFilter

rest

org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter

roles

org.apache.shiro.web.filter.authz.RolesAuthorizationFilter

ssl

org.apache.shiro.web.filter.authz.SslFilter

user

org.apache.shiro.web.filter.authc.UserFilter

logout

org.apache.shiro.web.filter.authc.LogoutFilter

项目中常用的解释一下,

/test/**=anon,  所有url 可以匿名访问

/test/**=authc,  url需要认证才能访问

/test/**=perms[user:add],  url需要认证用户拥有 user:add 权限才能访问

/test/**=roles[admin],  url需要认证用户拥有 admin 角色才能访问

/test/**=user,  url 需要认证或通过记住我认证才能访问

5. DEMO

开发工具为Eclipse+Maven,新建Springboot项目,版本号为1.5.14

模板引擎使用Thymeleaf

为了突出shiro,数据访问层略过,服务层模拟查询数据,Realm里面硬编码权限和角色信息简化代码。

整个系统的核心在于两个class(MyShiroRealm + ShiroConfiguration), 项目结构如下,

5.1 在pom.xml里面添加好shiro-core, shiro-spring

<dependency>

         <groupId>org.apache.shiro</groupId>

         <artifactId>shiro-core</artifactId>

         <version>1.4.0</version>

</dependency>

<!-- shiro权限控制框架 -->

<dependency>

         <groupId>org.apache.shiro</groupId>

         <artifactId>shiro-spring</artifactId>

         <version>1.4.0</version>

</dependency>

5.2 显示层

模板引擎Thymeleaf,故application配置文件如下:

 spring.thymeleaf.cache=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html

5.3 显示层静态页面如下:

403.html

add.html

delete.html

details.html

edit.html

index.html

login.html

logout.html

5.4 几乎所有静态页面就是一个空壳,大体如add.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>Add Page</title>
</head>
<body>
<h1>Add Page</h1>
</body>
</html>

add.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>Login Page</title>
<style type="text/css">
table {
width: 360px;
min-height: 25px;
line-height: 25px;
text-align: center;
border-color: #b6ff00;
border-collapse: collapse; }
</style>
</head>
<body>
<div>
<form action="/home/check" method="post">
<table border="1">
<tr>
<th>User Name</th>
<th><input type="text" name="name" /></th>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td></td>
</tr>
</table>
</form>
</div>
</body>
</html>

login.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>Index Page</title>
<style type="text/css">
p {
font-family: Times, TimesNR, 'New Century Schoolbook', Georgia,
'New York', serif;
font-size: 20px;
}
</style> </head>
<body>
<h1>This is index page.</h1>
<p>
Customer Name: <span th:text="${name}"></span> --- Role: <span
th:text="${role}"></span>
</p>
<table border="1">
<tr>
<th>角色</th>
<th>权限</th>
</tr>
<tr>
<td>admin</td>
<td>增加,删除,编辑,查看</td>
</tr>
<tr>
<td>operator</td>
<td>编辑,查看</td>
</tr>
<tr>
<td>viewer</td>
<td>查看</td>
</tr>
</table>
<ul>
<li><a th:href="@{/customer/index}">Index</a></li>
<li><a th:href="@{/customer/details}">Details</a></li>
<li><a th:href="@{/customer/add}">Add</a></li>
<li><a th:href="@{/customer/edit}">Edit</a></li>
<li><a th:href="@{/customer/delete}">Delete</a></li>
</ul>
</body>
</html>

index.html

5.5 Model

public class Customer implements Serializable {
private static final long serialVersionUID = 7429292944316962328L;
private String name;
private String password;
private String role; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
} public Customer(String name, String password, String role) {
super();
this.name = name;
this.password = password;
this.role = role;
} @Override
public String toString() {
return "Name : --- " + name + ", Password : --- " + password + ", Role : *** " + role;
} }

Customer.java

5.6 Service

 @Service
public class CustomerService {
public Customer findByName(String name) {
// 模拟查询数据库
// tom is admin, alice is operator, lucy is viewer
if (name.equals("alice")) {
return new Customer(name, "123", "operator");
}
return null;
}
}

CustomerService.java

5.7 Controller

@Controller
@RequestMapping("customer")
public class CustomerController {
@RequestMapping("/index")
@RequiresPermissions("customer:index")//权限管理;
public String index(Model model) {
Subject subject = SecurityUtils.getSubject();
Customer customer = (Customer)subject.getPrincipal();
if(customer != null) {
model.addAttribute("name", customer.getName());
model.addAttribute("role", customer.getRole());
}
return "index";
} @RequestMapping("/details")
@RequiresPermissions("customer:details")//权限管理;
public String details() {
return "details";
} @RequestMapping("/add")
@RequiresRoles("admin")
public String add() {
return "add";
} @RequestMapping("/edit")
@RequiresPermissions("customer:edit")//权限管理;
public String edit() {
return "edit";
} @RequestMapping("/delete")
@RequiresPermissions("customer:delete")//权限管理;
public String delete() {
return "delete";
}
}

CustomerController.java

@Controller
@RequestMapping("home")
public class HomeController {
@RequestMapping("/login")
public String login() {
return "login";
} @RequestMapping("/check")
public String check(HttpServletRequest request) throws Exception {
System.out.println("HomeController.check()"); String name = request.getParameter("name");
String password = request.getParameter("password");
UsernamePasswordToken token = new UsernamePasswordToken(name, password);
Subject subject = SecurityUtils.getSubject(); try {
subject.login(token);
} catch (Exception ex) {
System.out.println(ex.getMessage());
System.out.println(ex.getStackTrace());
return "login";
} return "redirect:/customer/index";
}
}

HomeController.java

5.8 最重要的两个类如下:

 package com.example.demo.config;

 import java.util.LinkedHashMap;
import java.util.Map; import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class ShiroConfiguration { @Bean
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
System.out.println("ShiroConfiguration.shirFilter()");
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
// 过滤器.
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
// 配置不会被拦截的链接 顺序判断
filterChainDefinitionMap.put("/static/**", "anon");
filterChainDefinitionMap.put("/home/**", "anon");
filterChainDefinitionMap.put("/test/**", "anon");
filterChainDefinitionMap.put("/customer/**", "authc");
shiroFilterFactoryBean.setLoginUrl("/home/login");
// 登录成功后要跳转的链接
shiroFilterFactoryBean.setSuccessUrl("/customer/index"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
} @Bean
public MyShiroRealm myShiroRealm() {
MyShiroRealm myShiroRealm = new MyShiroRealm();
return myShiroRealm;
} @Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(myShiroRealm());
return securityManager;
} // 开启Shiro AOP注解支持.
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
System.out.println("OPNE AOP......");
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
} @Bean
public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
return new DefaultAdvisorAutoProxyCreator();
} // 管理shiro生命周期
@Bean
public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
}

ShiroConfiguration.java

因为在Shiro中,最终是通过Realm来获取应用程序中的用户、角色及权限信息的。通常情况下,在Realm中会直接从我们的数据源中获取Shiro需要的验证信息。可以说,Realm是专用于安全框架的DAO.

Shiro的认证过程最终会交由Realm执行,这时会调用Realm的getAuthenticationInfo(token)方法。

该方法主要执行以下操作:

1)         根据口令信息检查标识主体(帐户标识信息)

2)         在数据源中查找相应的帐户信息

3)         确保令牌提供的凭据与存储在数据存储中的凭据匹配

4)         如果凭证匹配,则返回一个AuthenticationInfo实例,该实例将帐户数据封装为Shiro理解的格式

5)         如果凭证不匹配,则引发身份验证异常

在应用程序中需要自定义一个Realm类,继承AuthorizingRealm抽象类,覆盖doGetAuthenticationInfo(),重写获取用户信息的方法。

shiro的权限授权是通过继承AuthorizingRealm抽象类,覆盖doGetAuthorizationInfo()。当访问到页面的时候,URL配置了相应的权限或者shiro标签才会执行此方法否则不会执行,所以如果只是简单的身份认证没有权限的控制的话,那么这个方法可以不进行实现,直接返回null即可。

在这个方法中主要是使用类:SimpleAuthorizationInfo进行角色的添加和权限的添加。SecurityManager将权限或角色检查的任务委托给Authorizer,默认为ModularRealmAuthorizer。

应用程序则可以通过角色或者权限进行访问控制。

 package com.example.demo.config;

 import java.util.HashSet;
import java.util.Set; import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired; import com.example.demo.model.Customer;
import com.example.demo.service.CustomerService; public class MyShiroRealm extends AuthorizingRealm { @Autowired
private CustomerService customerService; @Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
System.out.println("MyShiroRealm.doGetAuthenticationInfo()"); // 获取用户的输入的账号.
String name = (String) token.getPrincipal();
System.out.println(token.getCredentials());
Customer c = customerService.findByName(name);
System.out.println("Customer info : " + c);
if (c == null) {
return null;
}
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(c, // 用户名
c.getPassword(), // 密码
getName() // realm name
); return authenticationInfo;
} @Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { System.out.println("权限管理-->MyShiroRealm.doGetAuthorizationInfo()");
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
Customer customer = (Customer) principals.getPrimaryPrincipal();
System.out.println("Customer is : " + customer);
// 权限单个添加;
// 添加一个角色,不是配置意义上的添加,而是证明该用户拥有admin角色
// 模拟查询数据库,得到用户角色为admin或者operator或者viewer
// admin有所有权限,operator有查看和编辑权限,没有添加和删除权限
// viewer只有查看权限
authorizationInfo.addRole("operator");
// 添加权限
Set<String> permissionSet = new HashSet<String>();
permissionSet.add("customer:details");
permissionSet.add("customer:index");
permissionSet.add("customer:edit");
//permissionSet.add("customer:add");
//permissionSet.add("customer:delete"); authorizationInfo.setStringPermissions(permissionSet);
return authorizationInfo;
}
}

MyShiroRealm

6. 参考资料

http://shiro.apache.org/introduction.html

SpringBoot集成Apache Shiro的更多相关文章

  1. springboot 整合apache shiro

    这几天因为项目需要,学习了下shiro,由此留下一些记录,也希望对初学shiro的朋友有帮助. springboot 是这两年新兴起来的一个项目,它的出现是为了减少springmvc开发过程中需要引入 ...

  2. SpringBoot整合Apache Shiro权限验证框架

    比较常见的权限框架有两种,一种是Spring Security,另一种是Apache Shiro,两种框架各有优劣,个人感觉Shiro更容易使用,更加灵活,也更符合RABC规则,而且是java官方更推 ...

  3. SpringBoot整合Apache Shiro

    Subject 用户主体 (把操作交给SecurityManager)SecurityManager 安全管理器 (关联Realm)Realm   Shiro连接数据的桥梁 引入maven依赖 < ...

  4. SpringBoot 集成Apache Kafak 消息队列

    Kafka is a distributed,partitioned,replicated commit logservice.它提供了类似于JMS的特性,但是在实现上完全不同,此外它并不是JMS规范 ...

  5. SpringBoot集成Shiro安全框架

    跟着我的步骤:先运行起来再说 Spring集成Shiro的GitHub:https://github.com/yueshutong/shiro-imooc 一:导包 <!-- Shiro安全框架 ...

  6. Apache Shiro:【1】Shiro基础及Web集成

    Apache Shiro:[1]Shiro基础及Web集成 Apache Shiro是什么 Apache Shiro是一个强大且易于使用的Java安全框架,提供了认证.授权.加密.会话管理,与spri ...

  7. 快速搭建Spring Boot + Apache Shiro 环境

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Apache Shiro 介绍及概念 概念:Apache Shiro是一个强大且易用的Java安全框 ...

  8. Apache Shiro:【2】与SpringBoot集成完成登录验证

    Apache Shiro:[2]与SpringBoot集成完成登录验证 官方Shiro文档:http://shiro.apache.org/documentation.html Shiro自定义Rea ...

  9. Springboot集成权限管理框架apache shiro

    一.名词解释 网上一大堆 二.pom依赖 <dependency> <groupId>org.apache.shiro</groupId> <artifact ...

随机推荐

  1. django 下载文件

    方法一. from django.http import StreamingHttpResponse def big_file_download(request): # do something... ...

  2. 洗礼灵魂,修炼python(64)--爬虫篇—re模块/正则表达式(2)

    前面学习了元家军以及其他的字符匹配方法,那得会用啊对吧?本篇博文就简单的解析怎么运用 正则表达式使用 前面说了正则表达式的知识点,本篇博文就是针对常用的正则表达式进行举例解析.相信你知道要用正则表达式 ...

  3. ASYNC_NETWORK_IO和PREEMPTIVE_OS_WAITFORSINGLEOBJECT等待事件

    背景环境: SQL Server 2005或以上 Select * from 某个表,表的数据量约为30万行,在执行语句时通过观察sys.dm_exec_requests中的wait_type列发现是 ...

  4. UICollectionView 基础

    在iOS开发中经常会用到UICollectionView,和UITableView同样即成UIScrollView 但是操作起来比UITableVIew要麻烦一些 ,有些地方需要注意,一下是UICol ...

  5. Hibernate 5 入门指南-基于映射文件

    由于Hibernate 4版本混乱,Hibernate 3有些过时,Hibernate 5的开发文档尚不完善,所以构建一份简单的Hibernate 5的入门指南 注:案例参考Hibernate 官方参 ...

  6. LeetCode算法题-Excel Sheet Column Title(Java实现)

    这是悦乐书的第180次更新,第182篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第39题(顺位题号是168).给定正整数,返回Excel工作表中显示的相应列标题.例如: ...

  7. Python中进程线程协程小结

    进程与线程的概念 进程 程序仅仅只是一堆代码而已,而进程指的是程序的运行过程.需要强调的是:同一个程序执行两次,那也是两个进程. 进程:资源管理单位(容器). 线程:最小执行单位,管理线程的是进程. ...

  8. Go学习笔记08-包

    Go学习笔记08-包 Go语言 封装 包 封装 CamelCase命名规则 首字母大写:public 首字母小写:private 包 一个目录即一个包 main包为可执行入口,只能有一个main包 为 ...

  9. 《数据库技术基础与应用(第2版)》学习笔记——第7章~

    从这章开始,操作的内容开始增多,概念的东西越来越少,可能跟学校的教学目的有关,但是跟我的学习目的不匹配,就不再继续整理. 总结:这本书适合大学本科生学习和了解数据库的相关知识以及Access和SQL ...

  10. 《Java大学教程》—第23章 Java网络编程

    本章主要关注的是Java的几个应用网络编程的场景,对于网络编程没有太多深入介绍,而Java本来也没有多少针对网络编程的特性.虽然Java有个Applet的概念,但是真用这个的开发的场景其实不多. 23 ...