首先什么是shiro?

shiro是apache下面的一个开源项目,下面是其网站上对其的一段说明:

Apache Shiro is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and session management. With Shiro’s easy-to-understand API, you can quickly and easily secure any application – from the smallest mobile applications to the largest web and enterprise applications.

弱弱的翻译一下:apache shiro是一个强大且易于使用的java安全框架,使用它可以进行

1:认证

2:鉴权

3:加密

4:会话管理

通过shiro简单易懂的api,可以简单快速的为任何应用程序提供安全保护。

什么是认证?

认证就是登陆一个系统之前,认证就是系统通过用户的输入去辨别登陆的用户是谁,认证过程中,用户需要提供一些输入让系统辨别且信任你。

The Shiro framework is designed to make authentication as clean and intuitive as possible while providing a rich set of features. Below is a highlight of the Shiro authentication features.

shiro框架在提供丰富功能的同时提供了直观简单的使用方式,下面是shiro提供的几种认证功能。

  • Subject Based - Almost everything you do in Shiro is based on the currently executing user, called a Subject. And you can easily retrieve the Subject anywhere in your code. This makes it easier for you to understand and work with Shiro in your applications.

Subjecet-在shiro中几乎所有的操作都是基于当前指定的用户,也称为subject,在代码的任何位置都可以轻易的访问到subject,这使在你的项目中使用shiro变得更易于理解

  • Single Method call - The authentication process is a single method call. Needing only one method call keeps the API simple and your application code clean, saving you time and effort.

单个方法调用-认证过程仅仅是一个方法的调用,这让你应用的代码更加简洁,给你节省了时间

  • Rich Exception Hierarchy - Shiro offers a rich exception hierarchy to offered detailed explanations for why a login failed. The hierarchy can help you more easily diagnose code bugs or customer services issues related to authentication. In addition, the richness can help you create more complex authentication functionality if needed.

丰富的异常体系-shiro提供了丰富的异常以便于更加详细的了解登陆失败的原因,这些异常让我们方便定位以及修改bug

  • ‘Remember Me’ built in - Standard in the Shiro API is the ability to remember your users if they return to your application. You can offer a better user experience to your them with minimal development effort

remember me功能内置的支持:shiro中remember me功能的api可以给你应用更好的用户体验。

  • Pluggable data sources - Shiro uses pluggable data access objects (DAOs), called Realms, to connect to security data sources like LDAP and Active Directory. To help you avoid building and maintaining integrations yourself, Shiro provides out-of-the-box realms for popular data sources like LDAP, Active Directory, Kerberos, and JDBC. If needed, you can also create your own realms to support specific functionality not included in the basic realms.

可插拔的数据源-shiro使用被称为Reamls的可插拔的数据连接对象来连接你的数据源,例如:LDAP,避免你自己去构建以及维护这些交互,shiro内置提供了几种常用的数据源接入机制,如果有必要,你可以自己创建特殊的Reaml来提供基础Reamls所不支持的功能。

  • Login with one or more realms - Using Shiro, you can easily authenticate a user against one or more realms and return one unified view of their identity. In addition, you can customize the authentication process with Shiro’s notion of an authentication strategy. The strategies can be setup in configuration files so changes don’t require source code modifications– reducing complexity and maintenance effort.

通过一个或者多个realms进行登录认证-使用shiro可以轻易的认证一个用户并提供一个统一的试图,此外我们还可以定制化认   证的策略,我们可以在配置文件中进行配置而不必修改代码。

什么是鉴权?

鉴权也被成为权限控制,判断是否有权限访问某个资源,shiro对鉴权提供的支持:

  • Checks based on roles or permissions - Since the complexity of authorization differs greatly between applications, Shiro is designed to be flexible, supporting both role-based security and permission-based security based on your projects needs.

提供基于角色和权限的方式进行鉴权

  • Powerful and intuitive permission syntax - As an option, Shiro provides an out-of-the-box permission syntax, called Wildcard Permissions, that help you model the fine grained access policies your application may have. By using Shiro’s Wildcard Permissions you get an easy-to-process and human readable syntax. Moreoever, you don’t have to go through the time-consuming effort and complexity of creating your own method for representing your access policies.

强大且直观的权限规则,shiro提供通配符进行权限的校验,使用通配符规则可读性较高

  • Multiple enforcement options – Authorization checks in Shiro can be done through in-code checks, JDK 1.5 annotations, AOP, and JSP/GSP Taglibs. Shiro’s goal is to give you the choice to use the option you think are best based on your preferences and project needs.

多种鉴权方式可选,可以使用jdk1.5中的注解,aop或者jsp标签

  • Strong caching support - Any of the modern open-source and/or enterprise caching products can be plugged in to Shiro to provide a fast and efficient user-experience. For authorization, caching is crucial for performance in larger environments or with more complex policies using back-end security data sources.

高效的缓存支持

  • Supports any data model - Shiro can support any data model for access control– it doesn’t force a model on you. Your realm implementation ultimately decides how your permissions and roles are grouped together and whether to return a “yes” or a “no” answer to Shiro. This feature allows you to architect your application in the manner you chose and Shiro will bend to support you.

支持任何类型的数据模型

再来看看shiro几个关键的概念:

Subject:Subject我们在上面说过,Subject一般来说代表当前登录的用户,我们可以在自己的代码中很容易的获取到Subject对象

Subject currentUser = SecurityUtils.getSubject();

获得Subject对象之后我们可以通过Subject对象进行授权检查。

AuthenticationToken token = new UsernamePasswordToken(username, password);
Subject.isPermitted()/checkRoles()
Subject.login(token)//登录操作
Subject.logout()//退出登录操作

SecurityManager:Subject代表某一个用户,而SecurityManager就是对这些Subject进行管理的对象,在web项目中使用shiro的时候,我们通常在xml文件中配置好SecurityManager对象,然后就不会跟它打太多的交道,而仅仅是访问Subject的api.

//这里我们使用spring和shiro进行集成
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="shiroDbRealm" /><property name="cacheManager" ref="shiroEhcacheManager" />
</bean>

Realms:shiro中使用Realms这个概念表示与数据进行交互的那一层,封装了数据源连接的细节,我们可以实现不同的realms来连接不同的数据源,通过realms读取用户数据用于认证和鉴权。

下面我们使用shiro和spring集成,对web项目进行控制,shiro与spring的集成灰常的简单。

在已经配置好的spring项目中,我们在xml中加入:

<filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class><init-param><param-name>targetFilterLifecycle</param-name><param-value>true</param-value></init-param>
</filter>
<filter-mapping><filter-name>shiroFilter</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>

这个时候我们可能想DelegatingFilterProxy是个什么东东,难道这个就是shiro的入口么?但是通过查看这个类是位于spring-web这个jar包中的,根本就不属于shiro的一部分,那么也不可能是shiro的入口,我们跟进去发现这个类继承了抽象类GenericFilterBean,而GenericFilterBean实现了Filter接口,应用程序启动的时候应该会调用GenericFilterBean.init()方法:该方法设置了filter的配置,另外调用了方法initFilterBean(),这个方法在子类中进行实现。

DelegatingFilterProxy类中对initFilterBean方法进行了实现:

@Overrideprotected void initFilterBean() throws ServletException {synchronized (this.delegateMonitor) {if (this.delegate == null) {// If no target bean name specified, use filter name.if (this.targetBeanName == null) {this.targetBeanName = getFilterName();}// Fetch Spring root application context and initialize the delegate early,// if possible. If the root application context will be started after this// filter proxy, we'll have to resort to lazy initialization.WebApplicationContext wac = findWebApplicationContext();if (wac != null) {this.delegate = initDelegate(wac);}}}}

如果没有设置targetBeanName属性,那么就使用过滤器的名字作为beanName

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><property name="securityManager" ref="securityManager" /><property name="loginUrl" value="/login" /><property name="successUrl" value="/management/index" /><property name="filters"><map><!-- <entry key="authc" value-ref="baseFormAuthenticationFilter"/> --><!-- 是否启用验证码检验 --><entry key="authc" value-ref="captchaFormAuthenticationFilter" /><entry key="user" value-ref="dWZUserFilter" /></map></property><property name="filterChainDefinitions"><value>/Captcha.jpg = anon/styles/** = anon/login/timeout = anon/login = authc/logout = logout/** = user</value></property></bean>

我们在web.xml中配置的过滤器名称是shiroFilter,然后我们在spring的配置文件中以该名字配置一个bean

 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="shiroDbRealm" /><!--  缓存用户的授权信息  --><property name="cacheManager" ref="shiroEhcacheManager" /></bean><bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"><property name="cacheManagerConfigFile" value="classpath:ehcache/ehcache-shiro.xml" /></bean><bean id="shiroDbRealm" class="com.mbb.ShiroDbRealm" depends-on="userDAO, userRoleDAO, moduleDAO, organizationRoleDAO,captchaService"></bean>

ShiroDbRealm是我们自定义实现的realms用于查询用户数据。

public class ShiroDbRealm extends AuthorizingRealm {//实现用户的认证protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {User user = userService.get(authcToken.getUsername());ShiroUser shiroUser = new ShiroUser(user.getId(), user.getUsername(), user);
             return new SimpleAuthenticationInfo(shiroUser, user.getPassword(),ByteSource.Util.bytes(salt), getName());
 }
//实现用户的鉴权
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
Collection<?> collection = principals.fromRealm(getName());
if (CollectionUtils.isEmpty(collection)) {
return null;
}
ShiroUser shiroUser = (ShiroUser) collection.iterator().next();

List<UserRole> userRoles = userRoleService.find(shiroUser.getId());
List<OrganizationRole> organizationRoles = organizationRoleService
.find(shiroUser.getUser().getOrganization().getId());

SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addStringPermissions(makePermissions(userRoles, organizationRoles, shiroUser));

return info;
}
}


												

使用shiro进行权限管理相关推荐

  1. spring boot整合shiro继承redis_spring-boot-plus集成Shiro+JWT权限管理

    SpringBoot+Shiro+JWT权限管理 Shiro Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理. 使用Shiro的易于理解的API,您可以 ...

  2. 《SpringBoot与Shiro整合-权限管理实战---从构建到模拟数据库登入》

    <SpringBoot与Shiro整合-权限管理实战> ---- 从构建到模拟数据库登入 ---- 点击下载源码 ---- 或者查看? 文章目录 <SpringBoot与Shiro整 ...

  3. 如何在 Spring Boot 中用 Shiro 实现权限管理

    本行 Chat 内容包括: 简单介绍 IntelliJ IDEA 的安装与注册 什么是 Spring Boot? 如何通过 IDEA 创建一个简单的 Spring Boot 项目 Apache Shi ...

  4. Shiro之权限管理的概念

    文章目录 前言:什么是shiro 一.什么是权限管理? 举例 二.权限管理的具体分类 1.身份认证 2.授权 总结 前言:什么是shiro Apache Shiro 是一个开源安全框架,提供身份验证. ...

  5. SpringBoot + Redis + Shiro 实现权限管理

    文章主要是针对shiro进行权限配置,只针对角色进行了权限过滤. GitHub:https://github.com/stevencxb/blog 数据库脚本 -- ----------------- ...

  6. Spring Boot与Shiro实现权限管理02

    1.前期设计 1.1 功能模块的设计 用户权限管理系统一般包括以下模块: 1.2 数据库的设计 根据基本的功能可以总结出6张数据库表: sys_permission:权限表 sys_role:角色表 ...

  7. SpringBoot整合Shiro实现权限管理与登陆注册

    前言 Shiro解决了什么问题? 互联网无非就是一些用户C想要使用一些服务S的资源去完成某件事,S的资源不能说给谁用就给谁用,因此产生了权限的概念,即C必须有权限才能操作S的资源.S如何确定C就是C呢 ...

  8. SpringBoot整合Shiro实现权限管理

    超详细的Java知识点路线图 目录 概述 RBAC权限管理 Shiro介绍 Shiro入门 自定义Realm 密码加密加盐 SpringBoot+MyBatis+Shiro整合 RememberMe ...

  9. SpringBoot整合Shiro实现权限管理,经典实战教程

    String username = authenticationToken.getPrincipal().toString(); if(!username.equals("zhang&quo ...

  10. 个人笔记 springboot整合shiro实现权限管理,前端使用vue 10155

    转载自github,地址https://github.com/Heeexy/SpringBoot-Shiro-Vue 目录结构: application.yml spring:datasource:u ...

最新文章

  1. 宏基因组扩增子最新分析流程QIIME2:官方中文帮助文档
  2. Hadoop Shell命令
  3. hpuoj--校赛--与学妹滑雪(最短路+精度判断)
  4. md文件生成Java代码_Beetlsql自定义生成entity,mapper,md代码
  5. linux下tail命令的用法,在Linux命令中tail的用法
  6. Python-初体验
  7. (转)使用Flexible实现手淘H5页面的终端适配
  8. eclipse 史上最舒服(且护眼) 字体+大小+配色 教程(强推!!)
  9. 抖音超火的罗马时钟html代码,最近抖音上挺火的圆形文字时钟
  10. Word在生成PDF后,PDF左侧导航书签没有目录
  11. 考研公式大全-提问版-数学二
  12. Nodelist转化问题。
  13. 美团CAT客户端集成
  14. 加入键盘鼠标控制的代码
  15. stm32miniRTC实时时钟——HAL库
  16. Java封装、继承 、多态
  17. JAVA CRC16校验
  18. SQL 开发应有的一种思维
  19. 在线支付之易宝支付的使用方法
  20. win10计算机快捷方式怎么弄到桌面,win10怎么把此电脑放到桌面_w10如何把此电脑添加到桌面...

热门文章

  1. 基于用户点击偏好和阅读满意度的个性化新闻推荐技术
  2. matlab function调用m文件,matlab下编写和调用函数(在同一个m文件中)
  3. 约四成的平板和智能手机用户看电视时“一心多用”
  4. 计算机桌面没有有了怎么添加,电脑里没有便签小工具怎么办?办公电脑上怎么添加一款方便记事的桌面便签软件...
  5. 开源软件 | 一款深度学习抠图算法,图像精细分割利器
  6. Dalvik虚拟机垃圾收集(GC)过程分析
  7. JavaScript的json和Array及Array数组的使用方法
  8. 微信小程序点击弹出输入框
  9. Git 读书笔记(二)
  10. 「TShark学习」TShark抓包笔记