| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
KristieDens
9年前发布

shiro使用jdbc_realm登录验证

来自: http://my.oschina.net/u/1474779/blog/618193


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/maven-v4_0_0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>com.lihua</groupId>   <artifactId>shiro01</artifactId>   <packaging>war</packaging>   <version>0.0.1-SNAPSHOT</version>   <name>shiro01 Maven Webapp</name>   <url>http://maven.apache.org</url>      <dependencies>    <dependency>     <groupId>junit</groupId>     <artifactId>junit</artifactId>     <version>3.8.1</version>     <scope>test</scope>    </dependency>        <dependency>     <groupId>org.apache.shiro</groupId>     <artifactId>shiro-core</artifactId>     <version>1.2.4</version>    </dependency>        <dependency>     <groupId>org.slf4j</groupId>     <artifactId>slf4j-log4j12</artifactId>     <version>1.7.16</version>    </dependency>        <dependency>    <groupId>c3p0</groupId>    <artifactId>c3p0</artifactId>    <version>0.9.1.2</version>   </dependency>      <dependency>    <groupId>commons-logging</groupId>    <artifactId>commons-logging</artifactId>    <version>1.2</version>   </dependency>      <dependency>    <groupId>mysql</groupId>    <artifactId>mysql-connector-java</artifactId>    <version>5.1.37</version>   </dependency>       </dependencies>         <build>    <finalName>shiro01</finalName>   </build>  </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_jdbc_realm  dataSource.user=root  dataSource.password=root  jdbcRealm.dataSource=$dataSource  securityManager.realms=$jdbcRealm

JdbcRealm.java

package test;    import org.apache.shiro.SecurityUtils;  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;    public class JdbcRealm {      public static void main(String[] args) {    //读取配置文件,初始化SecurityManager工厂    Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:jdbc_realm.ini");    //获取SecurityManager实例    SecurityManager securityManager = factory.getInstance();    //把SecurityManager绑定到SecurityUtils    SecurityUtils.setSecurityManager(securityManager);    //得到当前执行的用户    Subject subject = SecurityUtils.getSubject();    //创建token令牌  用户名/密码形式    UsernamePasswordToken token = new UsernamePasswordToken("test", "test");        try {     //用户登录     subject.login(token);     System.out.println("登录成功");    } catch (Exception e) {     e.printStackTrace();     System.out.println("登录失败");    }    //用户注销    subject.logout();   }    }

注意:数据库的表必须叫: users  字段为 id,userName, password

 本文由用户 KristieDens 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1456048967401.html
Shiro 安全相关