Shiro笔记---身份验证

2018-09-19 02:47:14来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

 

   1.shiro有哪些主要功能

    

 

   2.搭建shiro环境(*)

    idea2018.2、maven3.5.4、jdk1.8

    

    项目结构:

         

 

  pom.xml:

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.2.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>

        <!--mysql的驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>

        <!--数据库连接池:c3p0、dbcp、druid-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

        <!--日志-->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

 

 

 

  1. 创建一个shiro.ini文件在resources目录下
    [users]
    admin=123456
    lisi=123
    wangwu=1234
  2. 创建ShiroDemo.java
    package cn.java.shiro;
    
    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.mgt.SecurityManager;
    import org.apache.shiro.subject.Subject;
    import org.apache.shiro.util.Factory;
    
    import java.net.SocketOptions;
    
    public class ShiroDemo {
        public static void main(String[] args) {
            //SecurityManager--->Factory
            // Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");   调用数据库
            // Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:jdbc_realm.ini");
            //1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager
             Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
    
            //ctrl+alt+v  快速生成变量及对应的数据类型
            //2、得到SecurityManager实例 并绑定给SecurityUtils
            SecurityManager instance = factory.getInstance();
            SecurityUtils.setSecurityManager(instance);
    
            //当前用户
            /*3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证)*/
            Subject user = SecurityUtils.getSubject();
            //通过UsernamePasswordToken来模拟html/jsp传递过来的用户名与密码
            UsernamePasswordToken token = new UsernamePasswordToken("admin","123]");
            //通过shiro来判断用户是否登录成功   ctrl+alt+t捕获异常
    
            try {
                //4、登录,即身份验证
                user.login(token);
                System.out.println("登录成功");
            } catch (AuthenticationException e) {
                //5、身份验证失败
                System.out.println("登录失败");
            }
    
        }
    }
  3. 连接数据库把配置文件改成  jdbc_realm.ini:

    jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
    
    dataSource=com.alibaba.druid.pool.DruidDataSource
    dataSource.driverClassName=com.mysql.jdbc.Driver
    dataSource.url=jdbc:mysql:///shiro
    dataSource.username=root
    dataSource.password=root
    
    jdbcRealm.dataSource=$dataSource
    securityManager.realms=$jdbcRealm

 

   

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:常用注解记录

下一篇:Maven安装配置