• pom.xml
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.shyroke.www</groupId>
  5. <artifactId>firstShiro</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>firstShiro Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10.  
  11. <properties>
  12. <shiro.version>1.3.2</shiro.version>
  13. </properties>
  14.  
  15. <dependencies>
  16. <dependency>
  17. <groupId>javax.servlet</groupId>
  18. <artifactId>servlet-api</artifactId>
  19. <version>2.5</version>
  20. <scope>provided</scope>
  21. </dependency>
  22.  
  23. <dependency>
  24. <groupId>org.slf4j</groupId>
  25. <artifactId>slf4j-api</artifactId>
  26. <version>1.7.21</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.slf4j</groupId>
  30. <artifactId>jcl-over-slf4j</artifactId>
  31. <version>1.7.21</version>
  32. <scope>runtime</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>ch.qos.logback</groupId>
  36. <artifactId>logback-classic</artifactId>
  37. <version>1.1.7</version>
  38. <scope>runtime</scope>
  39. </dependency>
  40.  
  41. <!-- Shiro dependencies: -->
  42. <dependency>
  43. <groupId>org.apache.shiro</groupId>
  44. <artifactId>shiro-core</artifactId>
  45. <version>${shiro.version}</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.apache.shiro</groupId>
  49. <artifactId>shiro-web</artifactId>
  50. <version>${shiro.version}</version>
  51. </dependency>
  52.  
  53. <dependency>
  54. <groupId>c3p0</groupId>
  55. <artifactId>c3p0</artifactId>
  56. <version>0.9.1.2</version>
  57. </dependency>
  58.  
  59. <dependency>
  60. <groupId>mysql</groupId>
  61. <artifactId>mysql-connector-java</artifactId>
  62. <version>6.0.6</version>
  63. </dependency>
  64.  
  65. <dependency>
  66. <groupId>org.slf4j</groupId>
  67. <artifactId>slf4j-log4j12</artifactId>
  68. <version>1.7.25</version>
  69. <scope>test</scope>
  70. </dependency>
  71.  
  72. </dependencies>
  73. <build>
  74. <finalName>firstShiro</finalName>
  75. </build>
  76. </project>
  • shiro.ini
  1. [main]
  2. jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
  3. dataSource=com.mchange.v2.c3p0.ComboPooledDataSource
  4. dataSource.driverClass=com.mysql.jdbc.Driver
  5. dataSource.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
  6. dataSource.user=root
  7. dataSource.password=""
  8. jdbcRealm.dataSource=$dataSource
  • 本例中使用连接池。
  • 注:密码为空就填双引号,但是不能不填。
  • 测试类
  1. public class TestMain {
  2. public static void main(String[] args) {
  3.  
  4. Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro.ini");
  5. SecurityManager securityManager=factory.getInstance();
  6.  
  7. SecurityUtils.setSecurityManager(securityManager);
  8.  
  9. Subject subject=SecurityUtils.getSubject();
  10. UsernamePasswordToken token=new UsernamePasswordToken("admin", "12342");
  11.  
  12. try {
  13. subject.login(token);
  14. System.out.println("登录成功");
  15. } catch (UnknownAccountException e) {
  16. System.out.println("用户名错误");
  17. }catch(IncorrectCredentialsException e){
  18. System.out.println("密码错误");
  19. }
  20.  
  21. System.out.println(securityManager);
  22.  
  23. }
  24. }
  • 数据库。注意:这里的userName和password不能修改为其他名字,而且表明users也不能修改.

  • 结果:

(八)shiro之jdbcRealm的更多相关文章

  1. shiro之jdbcRealm

    Shiro认证过程 创建SecurityManager--->主体提交认证--->SecurityManager认证--->Authenticsto认证--->Realm验证 ...

  2. 5、Shiro之jdbcRealm认证授权

    登录认证: 注意,下面我是以连接orcal数据库为例的依赖,如果各位同仁使用的是骑她数据库,可以换成对应数据库的依赖(数据源不用换) Pom.xml增加依赖: <!--引入连接orcal的jar ...

  3. java开源安全框架-------Apache Shiro--第二天

    身份验证 即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标志信息来表明他就是他本人,如提供身份证.用户名.密码来证明 在shiro中,用户需要提供principals(身份)和crede ...

  4. 10_SpringBoot更加详细

    一. 原理初探 1.1 自动装配 1.1.1 pom.xml spring-boot-dependencies: 核心依赖在父工程中 我们在写入或者引入一些SpringBoot依赖的时候, 不需要指定 ...

  5. shiro(三),使用第三方jdbcRealm连接数据库操作

    这里采用第三方实现好的JdbcRealm连接数据库:首先来看一下源码: 接着前面的说:就把这个类当做我们自己写的就好了,我们需要实例化它,然后给他注入一个数据源 下面是ini文件配置 [main] # ...

  6. Apache shiro集群实现 (八) web集群时session同步的3种方法

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  7. shiro实战系列(八)之安全管理器

    Apache Shiro 提供安全框架界独一无二的东西:一个完整的企业级 Session 解决方案,从最简单的命令行及智能手机 应用到最大的集群企业 Web 应用程序.   这对许多应用有着很大的影响 ...

  8. Shiro身份认证-JdbcRealm

    Subject 认证主体 Subject认证主体包含两个信息 Principals : 身份,可以是用户名.邮箱.手机号等,用来标识一个登录主体身份. Credentials : 凭证,常见有密码,数 ...

  9. shiro框架学习-4- Shiro内置JdbcRealm

    1.  JdbcRealm 数据库准备 JdbcRealm就是用户的角色,权限都从数据库中读取,也就是用来进行用户认证授权的安全数据源更换为从数据库中读取,其他没有差别,首先在数据库创建三张表: CR ...

随机推荐

  1. redis之为什么redis是单线程?

    官方FAQ表示,因为Redis是基于内存的操作,CPU不是Redis的瓶颈,Redis的瓶颈最有可能是机器内存的大小或者网络带宽.既然单线程容易实现,而且CPU不会成为瓶颈,那就顺理成章地采用单线程的 ...

  2. Mybatis 中的转义字符(转帖)

    下文来自:https://www.cnblogs.com/dato/p/7028723.html  在此感谢作者的辛勤付出. 记录以下mybatis中的转义字符,方便以后自己看一下 Mybatis转义 ...

  3. P5663 加工零件

    P5663 加工零件 题解 暴力搜索 搜索显然会TLE #include<iostream> #include<cstdio> #include<cstdlib> ...

  4. 【转】nodejs获取post请求发送的formData数据

    前端post请求发送formData的类型数据时,需要服务端引入中间件body-parser,主要原因是post请求发送的数据,是在http的body里面,所以需要进行解析,否则获取不到数据(数据为空 ...

  5. LC 450. Delete Node in a BST

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  6. NewLife.XCode 上手指南

    想了解什么是XCode 在这里我不对XCode做过多介绍,XCode曾经是一个轻量级ORM组件,现在是一个重量级数据映射框架,支持实体对象数据到不同媒体的数据映射,提供面向对象的方式操作数据库,解决9 ...

  7. Linux -- Reactor

    结构 1. handles 资源的标志.这些资源通常包含网络连接,文件,定时器,同步对象等.handles 被用在注册服务器来标记socket,以便同步事件复用(Synchronous Event D ...

  8. 123457123457#1#-----com.threeapp.circlerunner01----儿童旋转跑酷游戏

    com.threeapp.circlerunner01----儿童旋转跑酷游戏

  9. ContextCleaner ——Spark 应用程序的垃圾回收器

    ContextCleaner是一个Spark服务,负责在应用程序范围内清除 shuffles, RDDs, broadcasts, accumulators和checkpointed RDDs,目的是 ...

  10. 未清SO关闭处理

    使用:  SD_SALESDOCUMENT_CHANGE   关闭订单 DATA: ls_header_in LIKE bapisdhd1 . DATA: ls_header_inx LIKE bap ...