`
cugfy
  • 浏览: 1914 次
社区版块
存档分类
最新评论

shiro入门学习

阅读更多
声明本文只适合初学者,本人也是刚接触而已,经过一段时间的研究小有收获,特来分享下希望和大家互相交流学习。
首先配置我们的web.xml代码如下,固定格式,记死就成
<filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>
              org.springframework.web.filter.DelegatingFilterProxy
          </filter-class>
      </filter>
    <filter-mapping>
      <filter-name>shiroFilter</filter-name>
      <url-pattern>
    @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(); 利用这个currentUser可以方便根据权限确定用户的操作。此用法请参考 http://kdboy.iteye.com
文中有详细说明,我这里只说简单部分。
最后就是我们的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标签
验证当前用户是否为“访客”,即未认证(包含未记住)的用户
<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标签
已认证通过的用户。不包含已记住的用户,这是与user标签的区别所在。
<shiro:authenticated> 
    <a href="updateAccount.jsp">Update your contact information</a>. 
</shiro:authenticated> 



notAuthenticated标签
未认证通过用户,与authenticated标签相对应。与guest标签的区别是,该标签包含已记住用户。
<shiro:notAuthenticated> 
    Please <a href="login.jsp">login</a> in order to update your credit card information. 
</shiro:notAuthenticated>


principal 标签
输出当前用户信息,通常为登录帐号信息
Hello, <shiro:principal/>, how are you today? 



hasRole标签
验证当前用户是否属于该角色
<shiro:hasRole name="administrator"> 
    <a href="admin.jsp">Administer the system</a> 
</shiro:hasRole>


lacksRole标签
与hasRole标签逻辑相反,当用户不属于该角色时验证通过
<shiro:lacksRole name="administrator"> 
    Sorry, you are not allowed to administer the system. 
</shiro:lacksRole>


hasAnyRole标签
验证当前用户是否属于以下任意一个角色。
<shiro:hasAnyRoles name="developer, project manager, administrator"> 
    You are either a developer, project manager, or administrator. 
</shiro:lacksRole> 



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

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

lacksPermission标签
与hasPermission标签逻辑相反,当前用户没有制定权限时,验证通过
Xml代码
<shiro:hasPermission name="user:create"> 
    <a href="createUser.jsp">Create a new User</a> 
</shiro:hasPermission>
还有一个重要的就是数据库的设计,我把图贴出来大家一看也就明白了,我在这里简单的描述下吧


shiro权限框架简单快速入门




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

相关推荐

    shiro入门学习.ppt

    shiro入门学习.ppt

    shiro入门学习PPT

    shiro入门学习的一份文档,简单的介绍,简单的配置,简单的扩展 。

    shiro入门学习demo源码

    &lt;bean id="myShiroRealm" class="com.z.shiro.realm.ShiroRealm"&gt; &lt;property name="cacheManager" ref="cacheManager" /&gt; &lt;/bean&gt; &lt;!-- Shiro Filter --&gt; &lt;bean id="shiroFilter" class="org.apache...

    简单的介绍,简单的配置,简单的扩展 shiro入门学习手册 共35页.pptx

    Apache Shiro是一个强大而灵活的开源安全框架,它能够干净利落地处理身份认证,授权,企业会话管理和加密。 以下是你可以用 Apache Shiro所做的事情: ? 验证用户 ? 对用户执行访问控制,如: ? 判断用户是否...

    Shiro入门教程

    Shiro入门实例 Shiro入门教程 Shiro框架学习demo http://www.xttblog.com/?p=669 权限管理教程。Shiro是一个强大易用的Java安全框架,提供了认证、授权、加密和会话管理等功能。 Shiro可以在任何环境下运行,小到最...

    shiro学习资料

    包含Apache_Shiro_reference(中文版).pdf,shiro 安全框架--最好的中文配置文档.pdf,shiro入门学习.ppt,shiro使用方法.ppt

    shiro入门demo

    shiro学习入门程序

    shiro学习笔记

    shiro学习笔记以及代码,包括权限基础、shiro入门、shiro实例等

    shiro框架的基础学习

    shiro框架的基础学习、包含shiro框架的认证、授权、session管理、缓存管理

    Shiro框架学习代码

    Shiro框架学习入门代码,自定义Realm 继承AuthorizingRealm 重写 AuthorizationInfo(授权) 和 AuthenticationInfo(认证)等三种认证方式

    shiro学习笔记(四次课,从入门到案例)

    Shiro01-概念、入门、认证的基本使用; Shiro02-认证加密,授权; Shiro03-SpringBoot集成、注册、登录; Shiro04-SpringBoot授权,Shiro注解、Shiro标签

    Shiro学习手册.md

    Apache Shiro的操作手册,涵盖整个Shiro的大部分知识点,入门好资料。对于公司中要使用的童鞋来说,非常有用。

    学习shiro中的加密程序

    此代码用eclipse编写开发,是为了学习入门shiro安全框架而编写的,用于学习和测试shiro的中的加密机制

Global site tag (gtag.js) - Google Analytics