1. 程式人生 > >shiro(三),使用第三方jdbcRealm連接數據庫操作

shiro(三),使用第三方jdbcRealm連接數據庫操作

void 一個數 drop source QQ 技術 post classpath uid

這裏采用第三方實現好的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

shiro(三),使用第三方jdbcRealm連接數據庫操作