声明本文只适合初学者,本人也是刚接触而已,经过一段时间的研究小有收获,特来分享下希望和大家互相交流学习。

首先配置我们的web.xml代码如下:
  1. <filter>
  2. <filter-name>shiroFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>shiroFilter</filter-name>
  7. <url-pattern>/*</url-pattern>
  8. </filter-mapping>

@Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取当前登陆的用户名
        String loginName = 
              (String) principalCollection.fromRealm(getName()).iterator().next();
        //根据用户名查找对象
        User user = userService.findByLoginName(loginName);
        if(user != null) {
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            //添加角色(Set集合<字符串>)
            info.setRoles(user.getGroupNameSet());
            //迭代用户对应的角色集合,为了获取角色对应的权限
            for(UserGroup g : user.getUserGroupList()) {
                //添加permission
                info.addStringPermissions(g.getPermissionStringList());
            }
            return info;
        }
        return null;
    }
   
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        //根据用户名去查找对象
        User user = userService.findByLoginName(token.getUsername());
       
        if(user != null) {
          return new SimpleAuthenticationInfo(user.getName(),
              user.getPassword(),getName());
        }
        return null;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
各部分代码功能上面注释已基本解释了,我要说的是,我们平时有可能比较喜欢使用currUser对象,但是貌似在这里没有办法得到了。其实不然,首先shiro给我们提供的Subject的会话可以满足我们的需求
Session session = subject.getSession();
Session session = subject.getSession(boolean create);
这些方法在概念上等同于HttpServletRequest API。第一个方法会返回Subject的现有会话,或者如果还没有会话,它会创建一个新的并将之返回。
第二个方法接受一个布尔参数,这个参数用于判定会话不存在时是否创建新会话。一旦获得Shiro的会话,你几乎可以像使用HttpSession一样使用它。Shiro团队觉得对于Java开发者,HttpSession API用起来太舒服了,所以我们保留了它的很多感觉。当然,最大的不同在于,你可以在任何应用中使用Shiro会话,不仅限于Web应用。因此你可以再验证登陆里写这样的一句话来完成我们的代码转换SecurityUtils.getSubject().getSession().setAttribute("currUser", user);注意在异常处理里需要移除此currUser。当然官方推荐使用Subject currentUser = SecurityUtils.getSubject()。


最后就是我们的Controller了。在这里我介绍登陆和退出
@RequestMapping("/user/login")
    public String login(User user,HttpSession session) {
        try {
            SecurityUtils.getSubject().login(new UsernamePasswordToken(user.getName(), user.getPassword()));
            return "redirect:/index";
        } catch (AuthenticationException e) {
            session.setAttribute("msg","用户密码错误或用户名不存在");
            return "redirect:/user/tologin";
        }
            
    }   

@RequestMapping("/user/exit")
    public String exit() {
        SecurityUtils.getSubject().logout();
        return "redirect:/user/login";
    }
好了,这样基本算是完成任务了,接下来就是页面上的操作了。为此shiro还提供了相应的标签,在这里我就照搬官方的了,因为这个实在简单,大家一看就明白
引用<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
guest标签 
验证当前用户是否为“访客”,即未认证(包含未记住)的用户
  1. <shiro:guest>
  2. Hi there!  Please <a href="login.jsp">Login</a> or <a href="signup.jsp">Signup</a> today!
  3. </shiro:guest>

user标签 
认证通过或已记住的用户

  1. <shiro:user>
  2. Welcome back John!  Not John? Click <a href="login.jsp">here<a> to login.
  3. </shiro:user>

authenticated标签 
已认证通过的用户。不包含已记住的用户,这是与user标签的区别所在。 

  1. <shiro:authenticated>
  2. <a href="updateAccount.jsp">Update your contact information</a>.
  3. </shiro:authenticated>

notAuthenticated标签 
未认证通过用户,与authenticated标签相对应。与guest标签的区别是,该标签包含已记住用户。

  1. <shiro:notAuthenticated>
  2. Please <a href="login.jsp">login</a> in order to update your credit card information.
  3. </shiro:notAuthenticated>

principal 标签 
输出当前用户信息,通常为登录帐号信息 

  1. Hello, <shiro:principal/>, how are you today?

hasRole标签 
验证当前用户是否属于该角色 

  1. <shiro:hasRole name="administrator">
  2. <a href="admin.jsp">Administer the system</a>
  3. </shiro:hasRole>

lacksRole标签 
与hasRole标签逻辑相反,当用户不属于该角色时验证通过 

  1. <shiro:lacksRole name="administrator">
  2. Sorry, you are not allowed to administer the system.
  3. </shiro:lacksRole>

hasAnyRole标签 
验证当前用户是否属于以下任意一个角色。

  1. <shiro:hasAnyRoles name="developer, project manager, administrator">
  2. You are either a developer, project manager, or administrator.
  3. </shiro:lacksRole>

hasPermission标签 
验证当前用户是否拥有制定权限 

  1. <shiro:hasPermission name="user:create">
  2. <a href="createUser.jsp">Create a new User</a>
  3. </shiro:hasPermission>

lacksPermission标签 
与hasPermission标签逻辑相反,当前用户没有制定权限时,验证通过 

Xml代码 
  1. <shiro:hasPermission name="user:create">
  2. <a href="createUser.jsp">Create a new User</a>
  3. </shiro:hasPermission>

还有一个重要的就是数据库的设计,我把图贴出来大家一看也就明白了,我在这里简单的描述下吧


user == subject:用户,group(role):角色
permission:权限(拥有权限比较细的情况,一般只要user和group就满足要求了)
最后 提一下jar包,别弄错了。是shiro-all.jar。可以从官网下载http://shiro.apache.org/download.html


Shiro权限框架简单快速入门的更多相关文章

  1. Shiro安全框架【快速入门】就这一篇!

    Shiro 简介 照例又去官网扒了扒介绍: Apache Shiro™ is a powerful and easy-to-use Java security framework that perfo ...

  2. Shiro安全框架「快速入门」就这一篇

    Shiro 简介 照例又去官网扒了扒介绍: Apache Shiro is a powerful and easy-to-use Java security framework that perfor ...

  3. php-laravel4.0框架 简单快速入门

    前提必须已经安装好了laravel4.0版本. 写入权限: 安装完 Laravel ,你还需要为web服务器设置 app/storage 目录的写入权限. 目录结构: 安装完框架后,你需要熟悉一下该项 ...

  4. Shiro权限框架简介

    http://blog.csdn.net/xiaoxian8023/article/details/17892041   Shiro权限框架简介 2014-01-05 23:51 3111人阅读 评论 ...

  5. Nodejs ORM框架Sequelize快速入门

    Nodejs ORM框架Sequelize快速入门 什么是ORM? 简单的讲就是对SQL查询语句的封装,让我们可以用OOP的方式操作数据库,优雅的生成安全.可维护的SQL代码.直观上,是一种Model ...

  6. 在前后端分离的SpringBoot项目中集成Shiro权限框架

    参考[1].在前后端分离的SpringBoot项目中集成Shiro权限框架 参考[2]. Springboot + Vue + shiro 实现前后端分离.权限控制   以及跨域的问题也有涉及

  7. (转) shiro权限框架详解06-shiro与web项目整合(上)

    http://blog.csdn.net/facekbook/article/details/54947730 shiro和web项目整合,实现类似真实项目的应用 本文中使用的项目架构是springM ...

  8. Spring Boot:整合Shiro权限框架

    综合概述 Shiro是Apache旗下的一个开源项目,它是一个非常易用的安全框架,提供了包括认证.授权.加密.会话管理等功能,与Spring Security一样属基于权限的安全框架,但是与Sprin ...

  9. SpringBoot整合Shiro权限框架实战

    什么是ACL和RBAC ACL Access Control list:访问控制列表 优点:简单易用,开发便捷 缺点:用户和权限直接挂钩,导致在授予时的复杂性,比较分散,不便于管理 例子:常见的文件系 ...

随机推荐

  1. Linux 知识框架

    Linux要点 一.Linux的基本要求 1. 掌握至少50个以上的常用命令. 2. 熟悉Gnome/KDE等X-windows桌面环境操作 . 3. 掌握.tgz..rpm等软件包的常用安装方法 4 ...

  2. PHP 记录日志

    public function writelog($message) { $logFileDir = LOG_PATH; $logFile = 'monitor' . date('Y-m-d') . ...

  3. ABAP屏幕设计

    转自 http://www.cnblogs.com/aBaoRong/archive/2012/06/05/2536591.html abap 屏幕控制 ******************** 屏幕 ...

  4. WinForm DataGridView根据选中的行多删

    private void btndel_Click(object sender, EventArgs e)        {            int count = 0;            ...

  5. ViewStub的简单用法和说明

    最近无意间知道了ViewStub,所以特地的去了解了一下 都知道ViewStub是一个不可见的,大小为0的View,实际上跟include差不多,但是ViewStub要更加节约资源.被称为是" ...

  6. ad

    取消class和id前的元素限定 当你写给一个元素定义class或者id,你可以省略前面的元素限定,因为ID在一个页面里是唯一的,而clas s可以在页面中多次使用.你限定某个元素毫无意义.例如: d ...

  7. js创建对象的四种方式

    (1)对象字面量         var clock = { hour:12, minute: 10, second: 10, showTime: function(){ alert(this.hou ...

  8. 如何换ionic里面的图标

    (转自http://www.zcool.com.cn/article/ZMTM4NDQw.html) 第一步,你把你的项目的SVG文件跟你自己做的图标的SVG文件都上到icomoon.io/app(上 ...

  9. codeforces B.Fixed Points

    link:http://codeforces.com/contest/347/problem/B 很简单,最多只能交换一次,也就是说,最多会增加两个.可能会增加一个.也可能一个也不增加(此时都是fix ...

  10. PHP实例开发(3)PHP中MVC学习之ThinkPHP

    PHP中MVC学习之ThinkPHP 1.什么是MVC MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是将M和V的实现代码分离 MVC是一个设 ...