当前位置:网站首页>How to configure the jdbcrealm data source?
How to configure the jdbcrealm data source?
2022-07-26 11:11:00 【qq_ twenty-five million seventy-three thousand two hundred and 】
from :
How to configure JdbcRealm Data sources ?
The following author describes the configuration JdbcRealm Method sharing of data sources , As shown below :
IniRealm It is the data source of the configuration database Usually : stay ini Data related information is set in the file example :
establish user.ini
//shiro-jdbc.ini To configure
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=123456
jdbcRealm.dataSource=$dataSource
securityManager.realms=$jdbcRealm
mysql Build table
//mysql sentence
drop database if exists shiro;
create database shiro;
use shiro;
create table users (
id bigint auto_increment,
username varchar(100),
password varchar(100),
password_salt varchar(100),
constraint pk_users primary key(id)
) charset=utf8 ENGINE=InnoDB;
create unique index idx_users_username on users(username);
create table user_roles(
id bigint auto_increment,
username varchar(100),
role_name varchar(100),
constraint pk_user_roles primary key(id)
) charset=utf8 ENGINE=InnoDB;
create unique index idx_user_roles on user_roles(username, role_name);
create table roles_permissions(
id bigint auto_increment,
role_name varchar(100),
permission varchar(100),
constraint pk_roles_permissions primary key(id)
) charset=utf8 ENGINE=InnoDB;
create unique index idx_roles_permissions on roles_permissions(role_name, permission);
insert into users(username,password)values('zhang','123');
securityManager User authentication
//test.java
@Test
public void testShiroJdbcRealm(){
//1. obtain securityManager And instantiate
IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro-jdbc.ini");
SecurityManager securityManager = factory.getInstance();
//2. Bind to SecurityUtils
SecurityUtils.setSecurityManager(securityManager);
//3. obtain subject And user authentication token
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhang","123");
try{
subject.login(token);
}catch (AuthenticationException e){
}
Boolean b = subject.isAuthenticated();
subject.logout();
}
It uses jdbcrealm The default table and query language , If you need to customize the table , Custom query statement , You can refer to the following code .
JdbcRealm Customize
public class JdbcRealmTest {
private static DruidDataSource dataSource;
static {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/shiro");
dataSource.setUsername("root");
dataSource.setPassword("root");
}
@Test
public void testJdbcRealm(){
//1、 initialization JdbcRealm
JdbcRealm jdbcRealm = new JdbcRealm();
jdbcRealm.setDataSource(dataSource);
/**
* a key :JdbcRealm Permission check is not enabled by default , To judge whether a role has certain permissions , This setting needs to be turned on , Otherwise, the permission table cannot be checked ,
* As a result, the queried permission is empty .
*/
jdbcRealm.setPermissionsLookupEnabled(true);
//(1) Custom query password ( If you don't customize ,JdbcRealm There are default query statements )
String authenticationQuery = "select password from users where username = ?";
jdbcRealm.setAuthenticationQuery(authenticationQuery);
//(2) Custom query role ( If you don't customize ,JdbcRealm There are default query statements )
String userRolesQuery = "select role_name from user_roles where username = ?";
jdbcRealm.setUserRolesQuery(userRolesQuery);
//(3) Customize query permissions ( If you don't customize ,JdbcRealm There are default query statements )
String permissionsQuery = "select permission from roles_permissions where role_name = ?";
jdbcRealm.setPermissionsQuery(permissionsQuery);
//2、 establish SecurityManager
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//3、 Set data source
securityManager.setRealm(jdbcRealm);
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
AuthenticationToken token = new UsernamePasswordToken("root", "root");
subject.login(token);
System.out.println(subject.isAuthenticated());
subject.checkRole("admin");
subject.checkPermission("user:delete");
}
}边栏推荐
猜你喜欢

菜鸟看源码之HashTable

Wireshark basic tutorial Ethernet frame analysis.

How the ThreadPoolExecutor performs tasks

菜鸟看源码之LinkedBlockingQueue

由浅入深搭建神经网络

Bash shell学习笔记(二)

看源码之LinkedList

MySQL锁机制

0x00007ffd977c04a8 (qt5sqld.dll) (in a.exe): 0xc0000005: an access violation occurred when reading position 0x0000000000000010

PyQt5快速开发与实战 第1章 认识PyQt5
随机推荐
MySql基础知识汇总
MySQL死锁分析
Relationship between pixels and memory
35. Search the insertion position
企鹅龙(DRBL)无盘启动+再生龙(clonezilla)网络备份与还原系统
Access rights - private, public, protected
ArrayList of novice source code
Tutorial of putty
easyui05
mysql数据库进阶
Le audio specification overview
242. Effective letter heteronyms
微信公众号消息通知 “errcode“:40164,“errmsg“:“invalid ip
ESXi6.5补丁更新
Sword finger offer (49): convert a string to an integer
The assignment of member pointer defined in C structure and the use of structure pointer as member function parameter
easyui01
MySQL事务详解
There is an unhandled exception at 0x003b66c3 in MFC: 0xc000041d: unhandled exception encountered during user callback
postman 导出导入