shiro(三),使用第三方jdbcRealm连接数据库操作

这里采用第三方实现好的JdbcRealm连接数据库;首先来看一下源码;

shiro(三),使用第三方jdbcRealm连接数据库操作

shiro(三),使用第三方jdbcRealm连接数据库操作

接着前面的说;就把这个类当做我们自己写的就好了,我们需要实例化它,然后给他注入一个数据源

下面是ini文件配置

[main]
#采用第三方JdbcRealm连接数据库
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm

#实例化数据源
dataSource=com.alibaba.druid.pool.DruidDataSource

#设置参数
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/shiro
dataSource.username=root
dataSource.password=root

#将数据源设置到realm中
jdbcRealm.dataSource=$dataSource
jdbcRealm.permissionsLookupEnabled=true
securityManager.realms=$jdbcRealm

下面是sql脚本;大家测试可以直接复制去数据库执行

/*
Navicat MySQL Data Transfer

Source Server         : localhost_3306
Source Server Version : 50527
Source Host           : localhost:3306
Source Database       : shiro

Target Server Type    : MYSQL
Target Server Version : 50527
File Encoding         : 65001

Date: 2018-03-30 22:31:17
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `roles_permissions`
-- ----------------------------
DROP TABLE IF EXISTS `roles_permissions`;
CREATE TABLE `roles_permissions` (
  `id` int(11) NOT NULL DEFAULT '0',
  `role_name` varchar(20) DEFAULT NULL,
  `permission` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of roles_permissions
-- ----------------------------
INSERT INTO `roles_permissions` VALUES ('1', 'system', 'update');

-- ----------------------------
-- Table structure for `users`
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) DEFAULT NULL,
  `password` varchar(20) DEFAULT NULL,
  `password_salt` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES ('1', 'admin', '666', null);

-- ----------------------------
-- Table structure for `user_roles`
-- ----------------------------
DROP TABLE IF EXISTS `user_roles`;
CREATE TABLE `user_roles` (
  `id` int(11) NOT NULL DEFAULT '0',
  `username` varchar(20) DEFAULT NULL,
  `role_name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user_roles
-- ----------------------------
INSERT INTO `user_roles` VALUES ('1', 'admin', 'system');

下面是测试代码

package com;

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

/**
 * 测试shiro第一个案例
 *
 */
public class App 
{
    public static void main( String[] args )
    {

            /*实例化工厂*/
            Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
           /*获取实例*/
            SecurityManager securityManager = factory.getInstance();
            /*设置参数*/
            SecurityUtils.setSecurityManager(securityManager);
            /*获取登录实例*/
            Subject subject = SecurityUtils.getSubject();
            /*实例化token,传入登录的用户和密码*/
            UsernamePasswordToken token = new UsernamePasswordToken("admin", "666");
            subject.isAuthenticated();

            try {
                /*进行登录*/
                subject.login(token);
                System.out.println("登录成功!!");
                if (subject.hasRole("system")){
                   System.out.println("拥有该角色");
               }else {
                   System.out.println("没有该角色");
               }

               if (subject.isPermitted("update")){
                    System.out.println("拥有该权限");
                }else {
                   System.out.println("没有该权限");
               }
                /*退出登录*/
                subject.logout();
            }catch (Exception e){
                System.out.println("登录错误!!");
            }

    }
}

需要依赖文件请点击此处链接http://www.cnblogs.com/qq376324789/p/8640651.html

 

下面的代码是将ini文件彻底转换为java代码

package com;

import com.alibaba.druid.pool.DruidDataSource;
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.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

public class TestShiro {


    public static void main(String[] args) {
        
        Factory<SecurityManager> factory = new IniSecurityManagerFactory();
        SecurityManager instance = factory.getInstance();
        JdbcRealm realm = new JdbcRealm();
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("mysql://localhost:3306/shiro");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        realm.setDataSource(dataSource);
        realm.setPermissionsLookupEnabled(true);
        SecurityUtils.setSecurityManager(instance);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("admin", "666");
        subject.login(token);


    }
}

 

上一篇:高德地图web端笔记;发送http请求的工具类


下一篇:mysql语法