shiro之自定义realm
Shiro认证过程
创建SecurityManager---》主体提交认证---》SecurityManager认证---》Authenticsto认证---》Realm验证 Shiro授权过程
创建SecurityManager---》主体授权---》ecurityManager授权---》Authorizer授权---》Realm获取角色权限数据
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ylht-shiro</artifactId>
<groupId>com.ylht</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>shiro-test</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency> <!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency> </dependencies> </project>
2.自定义realm(自定义realm可以的编写可以参考源码)
package com.ylht.shiro.realm; import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set; public class CustomerRealm extends AuthorizingRealm { {
super.setName("customRealm");
} //该方法用来授权
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//1.从认证信息中获取用户名
String username = (String) principalCollection.getPrimaryPrincipal();
//2.从数据库或者缓存中获取用户角色数据
Set<String> roles = getRolesByUserName(username);
//3.从数据库或者缓存中获取用户权限数据
Set<String> permissions = getPermissionsByUserName(username); SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.setRoles(roles);
simpleAuthorizationInfo.setStringPermissions(permissions);
return simpleAuthorizationInfo;
} //该方法用来认证
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//1.从认证信息中获取用户名
String username = (String) authenticationToken.getPrincipal(); //2.通过用户名到数据库中获取凭证
String password = getPwdByUserName(username);
if (null == password) {
return null;
}
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
username, password, "customRealm");
simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("zzz"));
return simpleAuthenticationInfo;
} //模拟数据库
private String getPwdByUserName(String username) {
Map<String, String> userMap = new HashMap<String, String>(16);
userMap.put("kk", "bdd170a94d02707687abc802b2618e19");
return userMap.get(username);
} //模拟数据库
private Set<String> getRolesByUserName(String username) {
Set<String> sets = new HashSet<String>();
sets.add("admin");
sets.add("user");
return sets;
} //模拟数据库
private Set<String> getPermissionsByUserName(String username) {
Set<String> sets = new HashSet<String>();
sets.add("user:select");
sets.add("user:update");
return sets;
}
}
3.测试类(加密方式,盐等)
package com.ylht.shiro.test; import com.ylht.shiro.realm.CustomerRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Test; public class CustomRealmTest { @Test
public void testCustomRealm() {
//创建JdbcRealm对象
CustomerRealm customerRealm = new CustomerRealm();
//设置JdbcRealm属性 //1.创建SecurityManager对象
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//securityManager对象设置realm
securityManager.setRealm(customerRealm); //shiro加密
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
//加密方式
matcher.setHashAlgorithmName("md5");
//加密次数
matcher.setHashIterations(2); //customerRealm设置matcher
customerRealm.setCredentialsMatcher(matcher); //2.主题提交认证
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject(); //token
UsernamePasswordToken token = new UsernamePasswordToken("kk", "123456", false); //认证
subject.login(token);
boolean flag = subject.isAuthenticated();
if (flag) {
System.out.println("用户认证通过");
} else {
System.out.println("用户认证失败");
} //角色验证
try {
subject.checkRole("admin");
System.out.println("角色验证通过");
} catch (AuthorizationException e) {
System.out.println("角色验证失败");
e.printStackTrace();
} //角色权限验证
try {
subject.checkPermission("user:select");
System.out.println("角色权限验证通过");
} catch (AuthorizationException e) {
System.out.println("角色权限验证失败");
e.printStackTrace();
} } public static void main(String[] args) {
//Md5Hash md5Hash = new Md5Hash("123456","zzz");
Md5Hash md5Hash = new Md5Hash("123456");
System.out.println(md5Hash);
Md5Hash md5Hash1 = new Md5Hash(md5Hash);
System.out.println(md5Hash1.toString());
}
}
shiro之自定义realm的更多相关文章
- shiro中自定义realm实现md5散列算法加密的模拟
shiro中自定义realm实现md5散列算法加密的模拟.首先:我这里是做了一下shiro 自定义realm散列模拟,并没有真正链接数据库,因为那样东西就更多了,相信学到shiro的人对连接数据库的一 ...
- shiro(二)自定义realm,模拟数据库查询验证
自定义一个realm类,实现realm接口 package com; import org.apache.shiro.authc.*; import org.apache.shiro.realm.Re ...
- Shiro -- (三) 自定义Realm
简介: Realm:域,Shiro 从从 Realm 获取安全数据(如用户.角色.权限),就是说 SecurityManager 要验证用户身份,那么它需要从 Realm 获取相应的用户进行比较以确定 ...
- 6、Shiro之自定义realm
1.创建一个包存放我们自定义的realm文件: 创建一个类名为CustomRealm继承AuthorizingRealm并实现父类AuthorizingRealm的方法,最后重写: CustomRea ...
- 使用Spring配置shiro时,自定义Realm中属性无法使用注解注入解决办法
先来看问题 纠结了几个小时终于找到了问题所在,因为shiro的realm属于Filter,简单说就是初始化realm时,spring还未加载相关业务Bean,那么解决办法就是将springmvc ...
- (十)shiro之自定义Realm以及自定义Realm在web的应用demo
数据库设计 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...
- 权限框架 - shiro 自定义realm
上篇文章中是使用的默认realm来实现的简单登录,这仅仅只是个demo,真正项目中使用肯定是需要连接数据库的 首先创建自定义realm文件,如下: 在shiro中注入自定义realm的完全限定类名: ...
- Shiro第二篇【介绍Shiro、认证流程、自定义realm、自定义realm支持md5】
什么是Shiro shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. spring中有spring security (原名Acegi),是一个权限框架,它和sp ...
- shiro自定义Realm
1.1 自定义Realm 上边的程序使用的是shiro自带的IniRealm,IniRealm从ini配置文件中读取用户的信息,大部分情况下需要从系统的数据库中读取用户信息,所以需要自定义realm. ...
随机推荐
- ViewPagerIndicator 取代TabHost,实现滑动tab,引导页等效果
https://github.com/eltld/ViewPagerIndicator 取代TabHost,实现滑动tab,引导页等效果
- Java堆内存与栈内存对比
在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有什么异同,以及和数据结构中的堆栈有何关系? 一.Java 堆存储空间 堆内存(堆存储空间)会在Java运行时分配 ...
- Spark 学习笔记:(四)MLlib基础
MLlib:Machine Learning Library.主要内容包括: 数据类型 统计工具 summary statistics correlations stratified sampling ...
- Cookie 和 Session 的区别 与联系
一. 概念理解 你可能有留意到当你浏览网页时,会有一些推送消息,大多数是你最近留意过的同类东西,比如你想买桌子,上淘宝搜了一下,结果连着几天会有各种各样的桌子的链接.这是因为 你浏览某个网页的时候,W ...
- 4408: [Fjoi 2016]神秘数
4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 452 Solved: 273 [Submit][Stat ...
- 20170301 Excel 导出函数XXL_SIMPLE_API
* XMPLT_V-COL_NO = . * XMPLT_V-COL_NAME = '物料号码'. * APPEND XMPLT_V. * * XMPLT_V-COL_NO = . * XMPLT_V ...
- Codility经典算法题之九:MissingInteger
Task description: This is a demo task. Write a function: that, given an array A of N integers, retur ...
- (linux)自旋锁及其衍生锁
自旋锁 毫秒以下. 自旋锁用于多个CPU系统中,在单处理器系统中,自旋锁不起锁的作用,只是禁止或启用内核抢占.在自旋锁忙等待期间,内核抢占机制还是有效的,等待自旋锁释放的线程可能被更高优先级的线程 ...
- 对soc-audio体系snd_soc_machine和snd_soc_dai_link简单理解
ASOC (ALSA system on chip) // 主要为嵌入式系统专门开发的sound管理体系结构[luther.gliethttp].Digital Audio ...
- 目前最新版本ActiveMQ 5.15.3 和JDK版本有关的问题
java.lang.UnsupportedClassVersionError: org/apache/activemq/ActiveMQConnectionFactory : Unsupported ...