首页 > 其他 > 详细

shiro笔记

时间:2015-11-19 16:32:18      阅读:285      评论:0      收藏:0      [点我收藏+]

shiro入门demo

建立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/shiro
 6 
 7 dataSource.user=root
 8 
 9 #dataSource.password=
10 
11 jdbcRealm.dataSource=$dataSource
12 
13 securityManager.realms=$jdbcRealm

一开始,我按照教程的方法,配置的方法是

1 dataSource=com.mchange.v2.c3p0.ComboPooledDataSource
2 dataSource.driverClassName=com.mysql.jdbc.Driver
3 dataSource.url=jdbc:mysql://localhost:3306/shiro
4 dataSource.username=root
5 #dataSource.password=

但是控制台一直报错

Line argument must contain a key anda value. Only one string token was found.
纠结了半天,后来下午的时候查了资料发现c3p0的配置写法好像不太一样,修改了以后就跑通了。

具体原因还不是很清楚。mark。

测试代码

 1 public static void main(String[] args) {
 2         Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:jdbc.ini");
 3         SecurityManager securityManager = factory.getInstance();
 4         SecurityUtils.setSecurityManager(securityManager);
 5         Subject currentUser = SecurityUtils.getSubject();
 6         UsernamePasswordToken token = new UsernamePasswordToken("admin","123");
 7         try{
 8             currentUser.login(token);
 9             System.out.println("身份认证成功");
10         }catch(AuthenticationException e){
11             e.printStackTrace();
12             System.out.println("身份认证失败");
13         }
14     }

另外,数据库里的表 表名必须为users ,字段必须为userName,password,不然会报错。

测试通过的话。

log4j:WARN No appenders could be found for logger (com.mchange.v2.log.MLog).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2015-11-19 15:46:34 org.apache.shiro.realm.AuthorizingRealm getAuthorizationCacheLazy
信息: No cache or cacheManager properties have been set.  Authorization cache cannot be obtained.
2015-11-19 15:46:34 org.apache.shiro.config.IniSecurityManagerFactory isAutoApplyRealms
信息: Realms have been explicitly set on the SecurityManager instance - auto-setting of realms will not occur.
2015-11-19 15:46:35 org.apache.shiro.session.mgt.AbstractValidatingSessionManager enableSessionValidation
信息: Enabling session validation scheduler...
身份认证成功

不通过的话会报错。

shiro笔记

原文:http://www.cnblogs.com/mamayi/p/4977849.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!