`
suichangkele
  • 浏览: 198201 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

shiro学习02-用户校验及权限验证基础

阅读更多

下面是摘自官方文档中的用户校验和权限检查,我觉得还是很有必要看一看。

一、用户校验

这一节对应于官方文档的地址是:http://shiro.apache.org/java-authentication-guide.html,主要介绍shiro的用户验证的部分。

在这一节中主要的名词有如下:

Subject(类比于用户),Principal(类似于用户名),Credentials(类似密码),Realm(类似于我们的数据库,存储用户的相关信息)

第一步:收集用户的principal和credentials(也就是用户名和密码),代码如下:

 

//Example using most common scenario:
//String username and password.  Acquire in
//system-specific manner (HTTP request, GUI, etc)

UsernamePasswordToken token =
 new UsernamePasswordToken( username, password );

//”Remember Me” built-in, just do this:
token.setRememberMe(true);

这里我们使用了usernamepasswordToken,这个是最长使用的token,通过校验用户名和密码是否匹配来实现对用户的校验。

 

第二步:提交princapal和credentials到校验系统。代码如下:

 

//With most of Shiro, you'll always want to make sure you're working with the currently executing user, referred to as the subject
Subject currentUser = SecurityUtils.getSubject();

//Authenticate the subject by passing
//the user name and password token
//into the login method
currentUser.login(token);

我们通过调用SecurityUtils来获得一个Subject,也就是我们通常说的用户,然后这个用户调用login方法,如果不出任何异常的话,表示校验通过。

第三步、捕获校验时的异常

当用户名和密码不一致时,将会抛出异常(至于到底是那个类的哪个方法抛得异常,先不用管),代码如下:

 

try {
    currentUser.login(token);
} catch ( UnknownAccountException uae ) { ...
} catch ( IncorrectCredentialsException ice ) { ...
} catch ( LockedAccountException lae ) { ...
} catch ( ExcessiveAttemptsException eae ) { ...
} ... catch your own ...
} catch ( AuthenticationException ae ) {
    //unexpected error?
}

然后根据捕获的异常进行判断,比如我们在开发中会跳到不同的页面。

 

Remember Me  的说明:

上面的代码中有个

token.setRememberMe(true);

这个的字面意思是记住我,这个的意思是先不管我是不是正确的用户和密码,你先记住我,他的内部逻辑是发送了一个cookie到浏览器,然后等下一次再访问的时候,就可以通过这个cookie知道我“有可能”是谁了,这里说的是有可能,因为setRememberMe并不一定是通过校验了,可能他的校验失败了,所以只能是通过这个cookie知道可能是谁。

 

 

RememberMe 和 Authenticated

在shiro中有两个方法,一个是isAuthenticated,一个是isRemembered,两者是不同的,前者的逻辑是更加严格的检验,而后者只是说明有可能是谁,而没有证明就是真实的用户。这个我们在淘宝上有相同的例子,当我们重复的使用一个浏览器访问淘宝时,即使我们不登录他的最上面也显示我们的用户名,即使是另一个人在使用同一个浏览器访问淘宝时也是显示的我们的名字,显然另一个人不是你自己,所以尽管通过发送cookie到浏览器记住了可能的用户,但是在进行一些操作,比如查看购物车时,淘宝要求必须登录之后才能操作,也就是必须证明你是谁,也就是authenticating yourself。

 

 二、权限验证

与权限验证相关的术语有:permissions,也就是权限;role,也就是角色;user,用户。

·权限:permission,这个是权限验证模块中粒度最小的名词,所有的权限验证都是建立在permission上的。permission定义了可以做什么事情,比如删除某个记录,更新某个记录,查询某个记录等。

·角色:这个是为了方便用户与权限之间的多对多的联系而采取的办法。一个用户有多个角色,一个角色有多个权限,使用角色来管理角色的好处就是方便权限的管理。

·用户这个概念和之前说的一样。

 

关于权限的验证在编码中的实现有下面的三种:

·programmatically,也就是通过编码实现,比如通过if else进行判断,然后根据逻辑进行操作。

·annotations,使用标签,shiro中提供了很多的标签供使用,但是对标签的使用必须依靠aop,比如使用spring的aop或者是使用aspectj的,我个人不推荐使用这个。

·jsp标签,就像我们使用的标准标签库一样,shiro也提供了一套自己的标签。

1、编码实现

编码实现也有好几种,比如基于角色的,基于权限的,基于字符串的。这里面最不推荐使用的就是基于角色的,因为角色有可能是后期生成的,可能会删除某个角色,需要修改源码,可能会造成很大的麻烦,

下面是官网文档中的基于权限授权的的代码

 

Subject currentUser =
    SecurityUtils.getSubject();

Permission printPermission = 
new PrinterPermission(“laserjet3000n”,“print”);

If (currentUser.isPermitted(printPermission)) {
    //do one thing (show the print button?)‏
} else {
    //don’t show the button?
}

2、基于注解,因为我本人不提倡使用这个,所以没有记录

3、基于jsp标签,下面是官网文档中的一个例子

 

<%@ taglib prefix=“shiro” uri=http://shiro.apache.org/tags %>
<html>
<body>
    <shiro:hasPermission name=“users:manage”>
        <a href=“manageUsers.jsp”>
            Click here to manage users
        </a>
    </shiro:hasPermission>
    <shiro:lacksPermission name=“users:manage”>
        No user management for you!
    </shiro:lacksPermission>
</body>
</html>

shiro的标签不是很多,都在这个页面上:http://shiro.apache.org/jsp-tag-library.html,下面贴出来

 

  • <shiro:guest/> - Displays body content only if the current Subject IS NOT known to the system, either because they have not logged in or they have no corresponding 'RememberMe' identity. It is logically opposite to the 'user' tag.
  • <shiro:user/> - Displays body content only if the current Subject has a known identity, either from a previous login or from 'RememberMe' services. Note that this is semantically different from the 'authenticated' tag, which is more restrictive. It is logically opposite to the 'guest' tag.
  • <shiro:principal/> - Displays the user's principal or a property of the user's principal.
  • <shiro:hasPermission/> - Displays body content only if the current Subject (user) 'has' (implies) the specified permission (i.e the user has the specified ability).
  • <shiro:lacksPermission/> - Displays body content only if the current Subject (user) does NOT have (not imply) the specified permission (i.e. the user lacks the specified ability)
  • <shiro:hasRole/> - Displays body content only if the current user has the specified role.
  • <shiro:lacksRole/> - Displays body content only if the current user does NOT have the specified role (i.e. they explicitly lack the specified role)
  • <shiro:hasAnyRoles/> - Displays body content only if the current user has one of the specified roles from a comma-separated list of role names
  • <shiro:authenticated/> - Displays body content only if the current user has successfully authenticated during their current session. It is more restrictive than the 'user' tag. It is logically opposite to the 'notAuthenticated' tag.
  • <shiro:notAuthenticated/> - Displays body content only if the current user has NOT succesfully authenticated during their current session. It is logically opposite to the 'authenticated' tag.
分享到:
评论

相关推荐

    shiro-root-1.2.3-source-release zipa包 和相关jar包

    Shiro提供角色(Role)和权限(Permission)的概念,可以对用户进行细粒度的权限分配。 3. **会话管理(Session Management)**:Shiro可以跨应用服务器管理会话,支持分布式会话,避免了session劫持和session固定...

    shiro-jwt-oauth权限认证

    通过这两个模块,开发者可以学习如何在实际项目中集成和配置Shiro、JWT以及OAuth2.0,以实现安全且灵活的权限认证。这不仅有助于提高系统的安全性,还能提升用户体验,因为用户只需要登录一次即可访问多个受保护的...

    单点登录sso-shiro-cas-maven

    &gt; casRealm这个类是需要我们自己实现的,主要用于shiro的权限验证,里面的属性说明如下 1. defaultRoles: 默认的角色 2. casServerUrlPrefix: cas地址 3. casService: 系统应用地址 最后我们还需要在/spring-...

    JFinal-Shiro-JDBC-Demo-master.zip_DEMO_jfinal_shiro

    4. **过滤器配置**:Shiro通过Filter链来实现安全控制,如登录拦截、权限校验等。在JFinal中,我们通常会看到对应的ShiroFilter配置,用于定义哪些URL需要经过Shiro的处理。 5. **数据库集成**:JDBC的使用意味着...

    注释最全,一步步详解 shiro-ssm登录认证权限授予验证案例.zip

    - 编写测试用例,模拟不同场景的登录、权限验证,确保 Shiro 整合的正确性。 通过这个案例,你可以全面了解如何在实际项目中应用 Shiro 进行用户认证和授权,为你的系统提供安全基础。案例中包含的详细步骤和代码...

    springboot+shiro+mybatis-plus.7z

    在本案例中,Shiro主要用来实现用户权限的管理,如登录验证、角色分配和权限校验。你可以通过Shiro的过滤器链来定义哪些URL需要用户登录才能访问,哪些操作需要特定的角色权限。 **MyBatis-Plus** MyBatis-Plus是在...

    shiro-core-1.7.0.jar

    Apache Shiro是一个应用广泛的权限管理的用户认证与授权框架。近日,shiro被爆出Apache Shiro 身份验证绕过漏洞 (CVE-2020-11989),攻击者可以使用包含payload的恶意请求绕过Shiro的身份认证,漏洞于1.5.3修复。实际...

    shiro-root-1.4.0-RC2.rar

    Shiro支持角色(Role)和权限(Permission)的概念,可以通过配置控制用户对资源的访问权限。 3. **加密(Cryptography)**:Shiro提供了多种加密工具,如散列函数、对称和非对称加密算法,便于安全地存储密码或...

    springboot-shiro-mybatis-demo.zip

    Shiro与MyBatis的结合,使得我们可以在Shiro的权限校验中调用这些Mapper接口,查询用户的权限信息。 在实际的业务逻辑中,比如用户登录,我们可以通过Shiro的Subject进行认证。Subject代表当前“安全主体”,它可以...

    shiro视频教程-最全,通俗易懂

    通过上述知识点的学习,Java开发者及相关专业和技术爱好者将能够深入理解Shiro框架的核心概念与工作原理,并能够运用到实际项目开发中,实现用户认证、授权、加密等功能,从而提升项目的整体安全性和用户体验。

    shiro-attack-4.5.2-SNAPSHOT-all.7z

    1. **不安全的身份验证机制**:如果配置不当,Shiro可能会允许恶意用户通过未验证或弱验证手段获取系统权限。例如,绕过凭证校验或者利用重放攻击。 2. **授权漏洞**:权限控制可能过于宽松,导致用户可以访问他们...

    shiro-redis-session-master.zip

    Apache Shiro是一个强大的Java安全框架,它提供了身份验证、授权、会话管理和加密等功能,使得在Java应用中处理安全性变得更加简单。在这个名为"shiro-redis-session-master"的项目中,开发者采用Redis作为Shiro的...

    shiro反序列化工具,加强版

    Shiro在处理用户会话或权限验证时,可能会涉及到对象的序列化和反序列化。例如,如果服务器将用户的会话对象序列化并存储在服务器端或者在客户端之间传递,而没有进行安全检查,就可能出现反序列化漏洞。加强版的...

    sping-boot-shiro-jwt-redis.zip

    2. **配置Shiro**:创建Shiro配置类,包括Realm(负责用户身份验证和权限校验)、过滤器链(定义哪些请求需要进行身份验证和授权)以及RedisSessionDAO(将Session数据存储到Redis中)。 3. **JWT实现**:创建JWT...

    shiro常用jar包-1.2.2

    将Shiro集成到项目中,通常需要在配置文件(如`shiro.ini`或Java配置)中指定安全策略,包括 Realm(负责与应用程序数据源交互以获取用户信息和权限信息)的配置,以及其他的设置项。 8. **插件体系** Shiro的...

    SpringBoot与Shiro整合-权限管理的简单权限系统.zip

    在这个项目中,Shiro用于处理用户登录验证,实现基于角色的权限控制,确保只有具有特定权限的用户才能访问相应的资源。 项目的核心流程可能包括以下几个步骤: 1. **用户注册与登录**:用户通过填写用户名和密码...

    shiro登录拦截校验demo

    "shiro登录拦截校验demo"是一个实际应用Shiro框架进行登录验证和权限拦截的示例项目。 在该demo中,我们可以学习到以下几个核心知识点: 1. **Shiro的基本概念**: - **身份验证(Authentication)**:确认用户...

    shiro-1.8.0.zip

    2. **授权**:验证成功后,Shiro检查该主体是否有执行特定操作的权限。 3. **会话管理**:Shiro可以帮助管理用户的会话,包括会话创建、更新、销毁以及超时处理。 4. **加密**:Shiro提供了丰富的加密工具,如密码...

    Shiro根据用户权限显示不同的菜单.Shiro根据权限显示指定菜单

    在这个场景中,我们关注的是如何利用Shiro实现根据用户权限动态显示不同的菜单。 首先,理解Shiro的核心概念是关键。在Shiro中,权限分为角色(Role)和权限(Permission)。角色是一组权限的集合,权限则具体描述...

    shiro学习示例

    - **权限控制**:Shiro的`hasRole()`和`isPermitted()`方法可方便地进行角色和权限的校验。 7. **扩展与社区支持** Shiro社区活跃,有许多插件和扩展,如RememberMe服务、CSRF防护、JWT集成等,可以根据项目需求...

Global site tag (gtag.js) - Google Analytics