Apache shiro MySQL实现 JSP RBAC

ER图

Apache shiro MySQL实现 JSP RBAC_第1张图片

 MySQL数据库脚本

-- --------------------------------------------------------
-- 主机:                           127.0.0.1
-- 服务器版本:                        8.0.22 - MySQL Community Server - GPL
-- 服务器操作系统:                      Win64
-- HeidiSQL 版本:                  11.3.0.6295
-- --------------------------------------------------------

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!50503 SET NAMES utf8mb4 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;


-- 导出 shiro 的数据库结构
CREATE DATABASE IF NOT EXISTS `shiro` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
USE `shiro`;

-- 导出  表 shiro.permissions 结构
CREATE TABLE IF NOT EXISTS `permissions` (
  `name` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL,
  `description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 正在导出表  shiro.permissions 的数据:~0 rows (大约)
/*!40000 ALTER TABLE `permissions` DISABLE KEYS */;
INSERT IGNORE INTO `permissions` (`name`, `description`) VALUES
	('DELETE', 'delete'),
	('READ', 'read'),
	('WRITE', 'write');
/*!40000 ALTER TABLE `permissions` ENABLE KEYS */;

-- 导出  表 shiro.roles 结构
CREATE TABLE IF NOT EXISTS `roles` (
  `name` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
  `description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 正在导出表  shiro.roles 的数据:~3 rows (大约)
/*!40000 ALTER TABLE `roles` DISABLE KEYS */;
INSERT IGNORE INTO `roles` (`name`, `description`) VALUES
	('ADMIN', 'Administrator role'),
	('USER_P1', 'Perfil 1'),
	('USER_P2', 'Perfil 2');
/*!40000 ALTER TABLE `roles` ENABLE KEYS */;

-- 导出  表 shiro.roles_permissions 结构
CREATE TABLE IF NOT EXISTS `roles_permissions` (
  `role_name` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
  `permission` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL,
  KEY `RP_1` (`role_name`),
  KEY `RP_2` (`permission`),
  CONSTRAINT `RP_1` FOREIGN KEY (`role_name`) REFERENCES `roles` (`name`),
  CONSTRAINT `RP_2` FOREIGN KEY (`permission`) REFERENCES `permissions` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 正在导出表  shiro.roles_permissions 的数据:~0 rows (大约)
/*!40000 ALTER TABLE `roles_permissions` DISABLE KEYS */;
INSERT IGNORE INTO `roles_permissions` (`role_name`, `permission`) VALUES
	('ADMIN', 'READ'),
	('ADMIN', 'WRITE'),
	('ADMIN', 'DELETE'),
	('USER_P1', 'READ'),
	('USER_P2', 'WRITE');
/*!40000 ALTER TABLE `roles_permissions` ENABLE KEYS */;

-- 导出  表 shiro.users 结构
CREATE TABLE IF NOT EXISTS `users` (
  `username` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `name` varchar(65) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 正在导出表  shiro.users 的数据:~3 rows (大约)
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
INSERT IGNORE INTO `users` (`username`, `email`, `name`, `password`) VALUES
	('admin', '[email protected]', 'Administrator', '$shiro1$SHA-256$500000$QmLtx8PaCMe72i+yVuqH+A==$P5ohK5uWi30u38ujuTnmmeUK2gPwqhxTnke2wd9fZXw='),
	('u1', '[email protected]', 'User P1', '$shiro1$SHA-256$500000$QmLtx8PaCMe72i+yVuqH+A==$P5ohK5uWi30u38ujuTnmmeUK2gPwqhxTnke2wd9fZXw='),
	('u2', '[email protected]', 'User P2', '$shiro1$SHA-256$500000$QmLtx8PaCMe72i+yVuqH+A==$P5ohK5uWi30u38ujuTnmmeUK2gPwqhxTnke2wd9fZXw=');
/*!40000 ALTER TABLE `users` ENABLE KEYS */;

-- 导出  表 shiro.users_roles 结构
CREATE TABLE IF NOT EXISTS `users_roles` (
  `username` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL,
  `role_name` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
  KEY `UR_1` (`username`),
  KEY `UR_2` (`role_name`),
  CONSTRAINT `UR_1` FOREIGN KEY (`username`) REFERENCES `users` (`username`),
  CONSTRAINT `UR_2` FOREIGN KEY (`role_name`) REFERENCES `roles` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 正在导出表  shiro.users_roles 的数据:~3 rows (大约)
/*!40000 ALTER TABLE `users_roles` DISABLE KEYS */;
INSERT IGNORE INTO `users_roles` (`username`, `role_name`) VALUES
	('admin', 'ADMIN'),
	('u1', 'USER_P1'),
	('u2', 'USER_P2');
/*!40000 ALTER TABLE `users_roles` ENABLE KEYS */;

/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40111 SET SQL_NOTES=IFNULL(@OLD_SQL_NOTES, 1) */;

shiro.ini

[main]
authc.loginUrl = /login.jsp
authc.successUrl = /home.jsp

# password matcher
passwordMatcher = org.apache.shiro.authc.credential.PasswordMatcher
passwordService = org.apache.shiro.authc.credential.DefaultPasswordService
passwordMatcher.passwordService = $passwordService


ds = com.mysql.cj.jdbc.MysqlDataSource
ds.url=jdbc:mysql://localhost:3306/shiro?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
ds.user = root
ds.password = root

jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled = true

# If not filled, subclasses of JdbcRealm assume "select password from users where username = ?"
# first result column is password, second result column is salt
jdbcRealm.authenticationQuery = select password from users where username = ?

# If not filled, subclasses of JdbcRealm assume "select role_name from users_roles where username = ?"
jdbcRealm.userRolesQuery = select role_name from users_roles where username = ?

# If not filled, subclasses of JdbcRealm assume "select permission from roles_permissions where role_name = ?"
jdbcRealm.permissionsQuery = select permission from roles_permissions where role_name = ?


jdbcRealm.credentialsMatcher = $passwordMatcher
jdbcRealm.dataSource=$ds
securityManager.realms = $jdbcRealm

#cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
#securityManager.cacheManager = $cacheManager
#jdbcRealm.authenticationCachingEnabled = true

[urls]
# The /login.jsp is not restricted to authenticated users (otherwise no one could log in!), but
# the 'authc' filter must still be specified for it so it can process that url's
# login submissions. It is 'smart' enough to allow those requests through as specified by the
# shiro.loginUrl above.
/login.jsp = authc
/home.jsp = anon, authc
/logout = logout
/account/** = authc

web.xml




  
    org.apache.shiro.web.env.EnvironmentLoaderListener
  

  
    ShiroFilter
    org.apache.shiro.web.servlet.ShiroFilter
  

  
    ShiroFilter
    /*
    REQUEST
    FORWARD
    INCLUDE
    ERROR
  

home.jsp

<%@ include file="include.jsp"%>


    
        " />
        Auth
    
    

        

Simple Shiro Web App

Hi Guest ! ( ">Log out ">Log in )

Visit your ">account page.

If you want to access the user-only ">account page, you will need to log-in first.

Roles

To show some taglibs, here are the roles you have and don't have. Log out and log back in under different user accounts to see different roles.

Roles you have

Administrator
Perfil 1
Perfil 2

Roles you DON'T have

Administrator
Perfil 1
Perfil 2

Permissions you have

read
write
delete

login.jsp

<%@ include file="include.jsp"%>


    
        " />
    
    

        

Please Log in

Here are a few sample accounts to play with in the default text-based Realm (used for this demo and test installs only). Do you remember the movie these names came from? ;)

Username Password
admin 123qwe
u1 123qwe
u2 123qwe


Username:
Password:
Remember Me

include.jsp

<%--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements.  See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership.  The ASF licenses this file
  ~ to you under the Apache License, Version 2.0 (the
  ~ "License"); you may not use this file except in compliance
  ~ with the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing,
  ~ software distributed under the License is distributed on an
  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~ KIND, either express or implied.  See the License for the
  ~ specific language governing permissions and limitations
  ~ under the License.
--%>
<%@ page import="org.apache.shiro.SecurityUtils" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

Apache shiro MySQL实现 JSP RBAC_第2张图片

 Apache shiro MySQL实现 JSP RBAC_第3张图片

 Apache shiro MySQL实现 JSP RBAC_第4张图片

Apache shiro MySQL实现 JSP RBAC_第5张图片

 Apache shiro MySQL实现 JSP RBAC_第6张图片

完整源码:https://github.com/allwaysoft/Apache-shiro-rbac-JdbcRealm-MySQL

你可能感兴趣的:(java,apache,数据库)