码迷,mamicode.com
首页 > 数据库 > 详细

shiro连接数据库

时间:2017-01-08 07:46:14      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:ken   src   use   blog   url   nis   out   nbsp   eal   

建一个jdbcRealm.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/test
dataSource.user=root
dataSource.password=qiu
jdbcRealm.dataSource=$dataSource
securityManager.realms=$jdbcRealm

java文件

package shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.util.Factory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

public class ShiroTest {
    
    public static void main(String[] args) {
        //初始化工厂
        Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro/jdbcRealm.ini");
         SecurityManager securityManager=factory.getInstance();
         SecurityUtils.setSecurityManager(securityManager);
         Subject subject=SecurityUtils.getSubject();
         UsernamePasswordToken token=new UsernamePasswordToken("tom","123");
        try {
             subject.login(token);
             System.out.println("登陆成功");
        } catch (Exception e) {
             e.printStackTrace();
             System.out.println("登陆失败");
        }
        subject.logout();
    }
}

数据库

 

 

 

技术分享

shiro连接数据库

标签:ken   src   use   blog   url   nis   out   nbsp   eal   

原文地址:http://www.cnblogs.com/javaweb2/p/6260787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!