zoukankan      html  css  js  c++  java
  • shiro身份认证

    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>com.fjm</groupId> <artifactId>shiro1</artifactId> <version>0.0.1-SNAPSHOT</version> <build/> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.2.3</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> <scope>provided</scope> </dependency> <!--c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.2</version> </dependency> <!--mysql-connector jdk8对应驱动6.0以上,jdk7/6对应驱动5.0以下--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> </dependencies> </project>

    jdbc_realm.ini

    [main]
    jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
    dataSource=com.mchange.v2.c3p0.ComboPooledDataSource
    dataSource.driverClass=com.mysql.jdbc.Driver
    dataSource.jdbcUrl=jdbc:mysql://localhost:3306/shiro_db
    dataSource.user=root
    dataSource.password=521314
    jdbcRealm.dataSource=$dataSource
    securityManager.realms=$jdbcRealm

    testClass

    public class JdbcTealmTest {
    
        public static void main(String[] args) {
            //获得shiro工厂  【从数据库中获取用户名和密码和令牌中的比较】
           Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:jdbc_realm.ini");
          //获得工厂实例
           SecurityManager securityManager = factory.getInstance();
          //绑定securityManager到SecurityUtils
           SecurityUtils.setSecurityManager(securityManager);
           //获得当前执行用户
           Subject currentUser = SecurityUtils.getSubject();
           //创建令牌
          UsernamePasswordToken token = new UsernamePasswordToken("fengjunming", "123456");
          try {
              currentUser.login(token);
              System.out.println("身份验证成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("身份验证失败!!!");
         }
          
        }
    
    }
  • 相关阅读:
    BOS项目 第1天(项目概述、PowerDesigner、svn、easyui的使用)
    mavenbos项目第二天(msyql操作、ssh配置集成、PowerDesinger、自定义登陆拦截器)
    mavenbos项目第一天(maven、远程部署、svn、easyui的使用。)
    json字符串和java对象的互相转换
    mysql设置环境变量
    windows下mysql的安装
    Unity3d 创建线程 子线程与主线程通信
    Unity3d 子线程能做的事
    Unity3d Resources TextAsset 文本
    NGUI Checkbox与PlayerPrefs
  • 原文地址:https://www.cnblogs.com/fengjunming/p/7828815.html
Copyright © 2011-2022 走看看