• 29、shiro框架入门


    1、建立测试shiro框架的项目,首先建立的项目结构如下图所示

    ini文件 中的内容如下图所示

    pom.xml文件中的内容如下所示

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>ShiroTest</groupId>
      <artifactId>ShiroId</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>ShiroId</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>  
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>4.9</version>  
        </dependency>  
        <dependency>  
            <groupId>commons-logging</groupId>  
            <artifactId>commons-logging</artifactId>  
            <version>1.1.3</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.shiro</groupId>  
            <artifactId>shiro-core</artifactId>  
            <version>1.2.2</version>  
        </dependency>  
    </dependencies>   
    </project>
    

      下边就是一个简单的shiro框架的示例

    package test;
    
    import junit.framework.Assert;
    
    import org.apache.shiro.SecurityUtils;
    import org.apache.shiro.authc.AuthenticationException;
    import org.apache.shiro.authc.UsernamePasswordToken;
    import org.apache.shiro.config.IniSecurityManagerFactory;
    import org.apache.shiro.subject.Subject;
    import org.apache.shiro.util.Factory;
    import org.junit.Test;
    
    public class TestShiro {
        @Test
        public void testShiro1(){
            //1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager  
            Factory<org.apache.shiro.mgt.SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro.ini");
            //2、得到SecurityManager实例 并绑定给SecurityUtils  
            org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
            SecurityUtils.setSecurityManager(securityManager);
            //3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证)  
            Subject subject=SecurityUtils.getSubject();
            UsernamePasswordToken token=new UsernamePasswordToken("zhang", "123");
            try{
                //4、登录,即身份验证 
                subject.login(token);
            }catch (AuthenticationException e) {
                //5、身份验证失败  
            }
            Assert.assertEquals(true, subject.isAuthenticated());//断言用户已经登录
            //退出
            subject.logout();
        }
    }

    2.1、首先通过new IniSecurityManagerFactory并指定一个ini配置文件来创建一个SecurityManager工厂;

    2.2、接着获取SecurityManager并绑定到SecurityUtils,这是一个全局设置,设置一次即可;

    2.3、通过SecurityUtils得到Subject,其会自动绑定到当前线程;如果在web环境在请求结束时需要解除绑定;然后获取身份验证的Token,如用户名/密码;

    2.4、调用subject.login方法进行登录,其会自动委托给SecurityManager.login方法进行登录;

    2.5、如果身份验证失败请捕获AuthenticationException或其子类,常见的如: DisabledAccountException(禁用的帐号)、LockedAccountException(锁定的帐号)、UnknownAccountException(错误的帐号)、ExcessiveAttemptsException(登录失败次数过多)、IncorrectCredentialsException (错误的凭证)、ExpiredCredentialsException(过期的凭证)等,具体请查看其继承关系;对于页面的错误消息展示,最好使用如“用户名/密码错误”而不是“用户名错误”/“密码错误”,防止一些恶意用户非法扫描帐号库;

    2.6、最后可以调用subject.logout退出,其会自动委托给SecurityManager.logout方法退出。

    这块完全是从

    http://jinnianshilongnian.iteye.com/blog/2019547

    这里扒下来的

    从如上代码可总结出身份验证的步骤:

    1、收集用户身份/凭证,即如用户名/密码;

    2、调用Subject.login进行登录,如果失败将得到相应的AuthenticationException异常,根据异常提示用户错误信息;否则登录成功;

    3、最后调用Subject.logout进行退出操作。

    2、

    2.5  Realm

    Realm:域,Shiro从从Realm获取安全数据(如用户、角色、权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法;也需要从Realm得到用户相应的角色/权限进行验证用户是否能进行操作;可以把Realm看成DataSource,即安全数据源。如我们之前的ini配置方式将使用org.apache.shiro.realm.text.IniRealm。

    org.apache.shiro.realm.Realm接口如下: 

    String getName(); //返回一个唯一的Realm名字  
    boolean supports(AuthenticationToken token); //判断此Realm是否支持此Token  
    AuthenticationInfo getAuthenticationInfo(AuthenticationToken token)  
     throws AuthenticationException;  //根据Token获取认证信息 

    Realm配置

    1、自定义Realm实现(com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1): 

    package realm;
    
    import org.apache.shiro.authc.AuthenticationException;
    import org.apache.shiro.authc.AuthenticationInfo;
    import org.apache.shiro.authc.AuthenticationToken;
    import org.apache.shiro.authc.IncorrectCredentialsException;
    import org.apache.shiro.authc.SimpleAuthenticationInfo;
    import org.apache.shiro.authc.UnknownAccountException;
    import org.apache.shiro.authc.UsernamePasswordToken;
    import org.apache.shiro.realm.Realm;
    
    public class MyRealm1 implements Realm{
    
        public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token)
                throws AuthenticationException {
            String username=(String)token.getPrincipal();//得到用户名
            String password=new String((char[])token.getCredentials());//得到密码
            if(!"zhang".equals(username)){
                throw new UnknownAccountException();//如果用户名错误
            }
            if(!"123".equals(password)){
                throw new IncorrectCredentialsException();//如果密码错误
            }
            //如果身份认证成功,返回一个AuthenticationInfo实现;
            return new SimpleAuthenticationInfo(username, password, getName());
        }
    
        public String getName() {
            return "myrealm1";
        }
    
        public boolean supports(AuthenticationToken token) {
            //仅支持UsernamePasswordToken类型的Token  
            return token instanceof UsernamePasswordToken;
        }
    
    }

    2、配置shiroreal.ini文件

  • 相关阅读:
    linux下文件的复制、移动与删除
    031_spark架构原理
    Scala基础篇-05求值策略
    Ceph pg分裂流程及可行性分析
    Ceph中的序列化
    奔跑吧,OpenStack现场分享:超融合架构如何抹平物理硬件差异?
    Ceph中Bufferlist的设计与使用
    IaaS中的统一存储:从设计到实现
    关于Ceph现状与未来的一些思考
    解析Ceph: 数据的端到端正确性和 Scrub 机制
  • 原文地址:https://www.cnblogs.com/weizhen/p/5936255.html
Copyright © 2020-2023  润新知