我的shiro自白-1

一. Shiro的模块

在这里插入图片描述

二. Subject认证主体
  • Subject认证主题包含两个信息:
  1. Principals:身份,可以是用户名,邮件,手机号码等等,用来标识一个登录主体身份;
  2. Credentials:凭证,常见有密码,数字证书等等;
  • 身份认证流程
    在这里插入图片描述
三. Realm&JDBC Realm
  • Realm:意思是域,Shiro 从 Realm 中获取验证数据;
  • Realm 有很多种类,例如常见的 jdbc realm,jndi realm,text realm。
  • jdbc realm下数据库表名约定为users,表结构及字段名约定如下:
    在这里插入图片描述
四. 权限认证核心要素

权限认证,也就是访问控制,即在应用中控制谁能访问哪些资源。
在权限认证中,最核心的三个要素是:权限,角色和用户;
权限,即操作资源的权利,比如访问某个页面,以及对某个模块的数据的添加,修改,删除,查看的权利;
角色,是权限的集合,一种角色可以包含多种权限;
用户,在 Shiro 中,代表访问系统的用户,即 Subject;

可以参考:http://shiro.apache.org/authentication.html

五. 授权

参考http://shiro.apache.org/authorization.html

  1. 编程式授权
  • 基于角色的访问控制
  • 基于权限的访问控制

注意:在maven普通项目中新建JUnit测试类,需要在src/main/java中右键单击包,然后
New -> JUnit Test Case。然后会在src/test/java下生成相应的包和测试类。

  1. 注解式授权
  • @RequiresAuthentication 要求当前 Subject 已经在当前的 session 中被验证通过才能被访问或调用。
@RequiresAuthentication
public void updateAccount(Account userAccount) {
    //this method will only be invoked by a
    //Subject that is guaranteed authenticated
    ...
}
等价于
public void updateAccount(Account userAccount) {
    if (!SecurityUtils.getSubject().isAuthenticated()) {
        throw new AuthorizationException(...);
    }

    //Subject is guaranteed authenticated here
    ...
}
  • @RequiresGuest 要求当前的 Subject 是一个"guest",也就是说,他们必须是在之前的 session 中没有被验证或被记住才能被访问或调用。
@RequiresGuest
public void signUp(User newUser) {
    //this method will only be invoked by a
    //Subject that is unknown/anonymous
    ...
}
等价于
public void signUp(User newUser) {
    Subject currentUser = SecurityUtils.getSubject();
    PrincipalCollection principals = currentUser.getPrincipals();
    if (principals != null && !principals.isEmpty()) {
        //known identity - not a guest:
        throw new AuthorizationException(...);
    }

    //Subject is guaranteed to be a 'guest' here
    ...
}
  • @RequiresPermissions(“account:create”) 要求当前的 Subject 被允许一个或多个权限,以便执行注解的方法。
@RequiresPermissions("account:create")
public void createAccount(Account account) {
    //this method will only be invoked by a Subject
    //that is permitted to create an account
    ...
}
等价于
public void createAccount(Account account) {
    Subject currentUser = SecurityUtils.getSubject();
    if (!subject.isPermitted("account:create")) {
        throw new AuthorizationException(...);
    }

    //Subject is guaranteed to be permitted here
    ...
}
  • @RequiresRoles(“administrator”) 要求当前的 Subject 拥有所有指定的角色。如果他们没有,则该方法将不会被执行,而且 AuthorizationException 异常将会被抛出。
@RequiresRoles("administrator")
public void deleteUser(User user) {
    //this method will only be invoked by an administrator
    ...
}
等价于
public void deleteUser(User user) {
    Subject currentUser = SecurityUtils.getSubject();
    if (!subject.hasRole("administrator")) {
        throw new AuthorizationException(...);
    }

    //Subject is guaranteed to be an 'administrator' here
    ...
}
  • @RequiresUser RequiresUser 注解需要当前的 Subject 是一个应用程序用户才能被注解的类/实例/方法访问或调用。一个“应用程序用户”被定义为一个拥有已知身份,或在当前 session 中由于通过验证被确认,或者在之前 session 中的’RememberMe’
    服务被记住。
@RequiresUser
public void updateAccount(Account account) {
    //this method will only be invoked by a 'user'
    //i.e. a Subject with a known identity
    ...
}
等价于
public void updateAccount(Account account) {
    Subject currentUser = SecurityUtils.getSubject();
    PrincipalCollection principals = currentUser.getPrincipals();
    if (principals == null || principals.isEmpty()) {
        //no identity - they're anonymous, not allowed:
        throw new AuthorizationException(...);
    }

    //Subject is guaranteed to have a known identity here
    ...
}
  1. Jsp 标签授权
  • 引入标签前缀
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

同时引入shiro-web.jar(TLD在包中的META-INF/shiro.tld)。

  • Guest 标签:用户没有身份验证时显示相应信息,即游客访问信息;
<shiro:guest>
    Hi there!  Please <a href="login.jsp">Login</a> or <a href="signup.jsp">Signup</a> today!
</shiro:guest>
  • User 标签:用户已经身份验证/记住我登录后显示相应的信息;
<shiro:user>
    Welcome back John!  Not John? Click <a href="login.jsp">here<a> to login.
</shiro:user>
  • Authenticated 标签:用户已经身份验证通过,即 Subject.login 登录成功,不是记住我登录的。
<shiro:authenticated>
    <a href="updateAccount.jsp">Update your contact information</a>.
</shiro:authenticated>
  • notAuthenticated 标签:用户没有身份验证通过,即没有调用 Subject.login 进行登录,包括记住我自动登录的也属于未进行身份验证。
<shiro:notAuthenticated>
    Please <a href="login.jsp">login</a> in order to update your credit card information.
</shiro:notAuthenticated>
  • principal 标签 显示用户身份信息,默认调用 Subject.getPrincipal()获取,即 Primary Principal。
Hello, <shiro:principal/>, how are you today?
等效于
Hello, <%= SecurityUtils.getSubject().getPrincipal().toString() %>, how are you today?
  • hasRole 标签 如果当前 Subject 有角色将显示 body 体内容。
<shiro:hasRole name="administrator">
    <a href="admin.jsp">Administer the system</a>
</shiro:hasRole>
  • lacksRole 标签 如果当前 Subject 没有角色将显示 body 体内容。
<shiro:lacksRole name="administrator">
    Sorry, you are not allowed to administer the system.
</shiro:lacksRole>
  • hasAnyRoles 标签 如果当前 Subject 有任意一个角色(或的关系)将显示 body 体内容。
<shiro:hasAnyRoles name="developer, project manager, administrator">
    You are either a developer, project manager, or administrator.
</shiro:hasAnyRoles>
  • hasPermission 标签 如果当前 Subject 有权限将显示 body 体内容。
<shiro:hasPermission name="user:create">
    <a href="createUser.jsp">Create a new User</a>
</shiro:hasPermission>
  • lacksPermission 标签 如果当前 Subject 没有权限将显示 body 体内容。
<shiro:lacksPermission name="user:delete">
    Sorry, you are not allowed to delete user accounts.
</shiro:lacksPermission>
六. Permissions 对权限深入理解
  • 单个权限 queryPrinter
subject.isPermitted("queryPrinter")
  • 单个资源多个权限 user:query user:add
    多值 user:query,add
printer:query
printer:print
printer:manage
printer:print,query
subject.isPermitted("printer:query")
  • 单个资源所有权限 user:query,add,update,delete user:*
printer:query,print,manage
printer:*
  • 所有资源某个权限 *:view

  • 实例级别的权限控制

    • 单个实例的单个权限 printer:query:lp7200 printer:print:epsoncolor
if ( SecurityUtils.getSubject().isPermitted("printer:query:lp7200") {
    // Return the current jobs on printer lp7200 }
}
  • 所有实例的单个权限 printer:print:*
  • 所有实例的所有权限 printer::
  • 单个实例的所有权限 printer:*:lp7200
  • 单个实例的多个权限 printer:query,print:lp7200

可参考http://shiro.apache.org/permissions.html

七. 授权流程

在这里插入图片描述

参考代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值