shiro学习(三,shiro加密)
shiro加密

使用MD5加密 认证
//自定义的Realm 域
public class CustomRealmSecret extends AuthorizingRealm { @Override //授权
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String username = (String) principals.getPrimaryPrincipal();
//通过用户名username,获取角色
Set<String> roles=getRolesByUsername(username);
//通过用户名username,获取角色de权限
Set<String> permissions=getPermissionsByUsrname(username); //返回AuthorizationInfo对象,先创建
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
//设置授权 ---角色
info.setRoles(roles);
//设置授权 ---权限
info.setStringPermissions(permissions);
return info;
} private Set<String> getPermissionsByUsrname(String username) {
//也是从数据库去,这里写死
Set<String> permissions=new HashSet<>();
permissions.add("user:select");
permissions.add("user:update");
permissions.add("user:delete");
permissions.add("user:insert");
return permissions;
} private Set<String> getRolesByUsername(String username) {
//也是从数据库去,这里写死
Set<String> roles=new HashSet<>();
roles.add("admin");
roles.add("user");
return roles;
} @Override //renz
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = (String) token.getPrincipal();
String pwd=getPswByUsername(username);
if (pwd==null){
return null;
}
SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(username,pwd,"customRealmSecret"); return info;
} private String getPswByUsername(String username) {
Map<String,String> map=new HashMap<>();
//加密的密码admin --->21232f297a57a5a743894a0e4a801fc3
map.put("admin","21232f297a57a5a743894a0e4a801fc3");
super.setName("customRealmSecret");
return map.get(username);
} @Test
public void MD5Test() {
Md5Hash md5Hash=new Md5Hash("admin");
System.out.println(md5Hash);
}
}
测试类
public class CustomRealmSecretTest {
@Test
public void testcustomRealmSecret(){
CustomRealmSecret realmSecret=new CustomRealmSecret();
//密码加密
HashedCredentialsMatcher matcher=new HashedCredentialsMatcher();
//使用MD5加密
matcher.setHashAlgorithmName("MD5");
//加密次数 1次
matcher.setHashIterations(1);
//自定的域中加入加密
realmSecret.setCredentialsMatcher(matcher);
DefaultSecurityManager securityManager=new DefaultSecurityManager();
securityManager.setRealm(realmSecret);
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token=new UsernamePasswordToken("admin","admin");
//认证
subject.login(token);
System.out.println(subject.isAuthenticated());
//授权
// subject.checkRoles("admin","user");
// subject.checkPermissions("user:delete","user:update");
}
}
使用MD5,再 加盐salt
测试类(其实没有变到,和之前代码一样,变的是自定义realm类,realm添加了盐salt的操作)
public class CustomRealmSecretTest {
@Test
public void testcustomRealmSecret(){
CustomRealmSecret realmSecret=new CustomRealmSecret();
//密码加密
HashedCredentialsMatcher matcher=new HashedCredentialsMatcher();
//使用MD5加密
matcher.setHashAlgorithmName("MD5");
//加密次数 1次
matcher.setHashIterations(1);
//自定的域中加入加密
realmSecret.setCredentialsMatcher(matcher);
DefaultSecurityManager securityManager=new DefaultSecurityManager();
securityManager.setRealm(realmSecret);
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token=new UsernamePasswordToken("admin","admin");
//认证
subject.login(token);
System.out.println(subject.isAuthenticated());
//授权
// subject.checkRoles("admin","user");
// subject.checkPermissions("user:delete","user:update");
}
}
自定义realm//自定义的Realm 域
//自定义的Realm 域
public class CustomRealmSecret extends AuthorizingRealm { @Override //授权
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String username = (String) principals.getPrimaryPrincipal();
//通过用户名username,获取角色
Set<String> roles=getRolesByUsername(username);
//通过用户名username,获取角色de权限
Set<String> permissions=getPermissionsByUsrname(username); //返回AuthorizationInfo对象,先创建
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
//设置授权 ---角色
info.setRoles(roles);
//设置授权 ---权限
info.setStringPermissions(permissions);
return info;
} private Set<String> getPermissionsByUsrname(String username) {
//也是从数据库去,这里写死
Set<String> permissions=new HashSet<>();
permissions.add("user:select");
permissions.add("user:update");
permissions.add("user:delete");
permissions.add("user:insert");
return permissions;
} private Set<String> getRolesByUsername(String username) {
//也是从数据库去,这里写死
Set<String> roles=new HashSet<>();
roles.add("admin");
roles.add("user");
return roles;
} @Override //renz
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = (String) token.getPrincipal();
String pwd=getPswByUsername(username);
if (pwd==null){
return null;
}
SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(username,pwd,"customRealmSecret");
info.setCredentialsSalt(ByteSource.Util.bytes("salt"));
return info;
} private String getPswByUsername(String username) {
Map<String,String> map=new HashMap<>();
//加密的密码admin再加盐 --->c657540d5b315892f950ff30e1394480
map.put("admin","c657540d5b315892f950ff30e1394480");
super.setName("customRealmSecret");
return map.get(username);
} @Test
public void MD5Test() {
Md5Hash md5Hash=new Md5Hash("admin","salt");
System.out.println(md5Hash);
//c657540d5b315892f950ff30e1394480
}
}
shiro学习(三,shiro加密)的更多相关文章
- Apache Shiro学习-2-Apache Shiro Web Support
Apache Shiro Web Support 1. 配置 将 Shiro 整合到 Web 应用中的最简单方式是在 web.xml 的 Servlet ContextListener 和 Fil ...
- shiro基础学习(三)—shiro授权
一.入门程序 1.授权流程 2.授权的三种方式 (1)编程式: 通过写if/else 授权代码块完成. Subject subject = SecurityUtils.getSubjec ...
- Shiro学习
Shiro学习资源 Shiro官网,http://shiro.apache.org/index.html 学习网站链接,http://blog.java1234.com/blog/articles/4 ...
- 学习shiro第三天
今天比较晚,所以只看了shiro的认证策略Authentication Strategy,下面讲讲shiro的三种认证策略. 1.AtLeastOneSuccessfulStrategy:这个是shi ...
- shiro学习详解(开篇)
一.前言 要开始接触公司另外一个项目了,RX和我说了下整个项目框架的结构,其中提到权限的控制是通过shiro来处理的,对我而言又是一个全新的知识点,于是今天花了一点时间去学习shiro的使用,看了好几 ...
- Shiro学习(总结)
声明:本文原文地址:http://www.iteye.com/blogs/subjects/shiro 感谢开涛提供的博文,让我学到了非常多.在这里由衷的感谢你,同一时候我强烈的推荐开涛的博文.他的博 ...
- Apache shiro学习总结
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- Shiro学习笔记总结,附加" 身份认证 "源码案例(一)
Shiro学习笔记总结 内容介绍: 一.Shiro介绍 二.subject认证主体 三.身份认证流程 四.Realm & JDBC reaml介绍 五.Shiro.ini配置介绍 六.源码案例 ...
- Apache Shiro 学习记录1
最近几天在学习Apache Shiro......看了一些大神们的教程.....感觉收获不少.....但是毕竟教程也只是指引一下方向....即使是精品教程,仍然有很多东西都没有说明....所以自己也稍 ...
- Shiro学习详解
1.Shiro基本架构 一.什么是Shiro Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户“登录”: 授权 ...
随机推荐
- ArcGIS中国工具3.0正式发布
ArcGIS中国工具3.0正式发布,新功能有 1. 支持面积分割(见4.6),见https://weibo.com/tv/v/HsM2ksYY3?fid=1034:4368578107884427 ...
- cache magic对pms模块的cache访问模式分析结果
其中43.184的命中率只有不到70%,是要分析的对象:3.189是命中率98%左右,是做参考的对象. 基本统计:可以知道43.184的update和delete操作占总操作的比例要比3.189打不少 ...
- Flume-事务与传输流程
一.Flume 事务 流程图 Put 事务流程 doPut:将批数据先写入临时缓冲区 putList doCommit:检查 channel 内存队列是否足够合并 doRollback:channel ...
- usage memcache in linux
set和add的区别 set可以重写存在的键值对, 也可以添加新的/ 而add不行, 如果存在已有的键名, 则add不会做更新该键值对, 不做任何事, 就是一次无效操作, 也就是, add可以防止重写 ...
- Docker容器时间与主机时间相差8小时
查看主机时间 [root@localhost ~]# date 2016年 07月 27日 星期三 22:42:44 CST 查看容器时间 root@b43340ecf5ef:/# date Wed ...
- 快速幂 -- cogs1437 转圈游戏
题目链接:http://cogs.pro:8081/cogs/problem/problem.php?pid=vJimmkqjW [题目描述] 思路:简单模拟,重点在于如何求这个轮数,由于k的范围过大 ...
- Java学习笔记-GUI
Java也提供图像化编程 图形化 GUI(图形用户界面) GUI Graphical User Interface(图形用户接口) 用图形的方式,来显示计算机操作的界面,这样更方便更直观 CLI Co ...
- 20个「MySQL」经典面试题,答对转dba 2w+「附答案」
1.MySQL的复制原理以及流程 基本原理流程,3个线程以及之间的关联: 2.MySQL中myisam与innodb的区别,至少5点 (1).问5点不同: (2).innodb引擎的4大特性 (3). ...
- 贪心学院 scrapy爬虫
生成爬虫 scrapy genspider 爬虫名 网址 打开调试用shell scrapy shell 网址 主体 stock.py # -*- coding: utf-8 -*- import r ...
- springboot整合filter
新建一个项目 新建Firstfilter类 Firstfliter.java package com.example.filter; import java.io.IOException; impor ...