shiro使用jdbc_realm登录验证
生活随笔
收集整理的這篇文章主要介紹了
shiro使用jdbc_realm登录验证
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
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=$jdbcRealmJdbcRealm.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綁定到SecurityUtilsSecurityUtils.setSecurityManager(securityManager);//得到當(dāng)前執(zhí)行的用戶Subject?subject?=?SecurityUtils.getSubject();//創(chuàng)建token令牌??用戶名/密碼形式UsernamePasswordToken?token?=?new?UsernamePasswordToken("test",?"test");try?{//用戶登錄subject.login(token);System.out.println("登錄成功");}?catch?(Exception?e)?{e.printStackTrace();System.out.println("登錄失敗");}//用戶注銷subject.logout();}}注意:數(shù)據(jù)庫的表必須叫: users ?字段為 id,userName, password
轉(zhuǎn)載于:https://my.oschina.net/u/1474779/blog/618193
總結(jié)
以上是生活随笔為你收集整理的shiro使用jdbc_realm登录验证的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ISE中使用DDR3例程的生成步骤与仿真
- 下一篇: JESD204B的AXI4-Lite时序