shiro连接数据库

建一个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();
    }
}

数据库

 

 

 

posted on 2017-01-08 00:48  老邱2  阅读(434)  评论(0编辑  收藏  举报

导航