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安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户“登录”: 授权 ...
随机推荐
- ansible的剧本
ansible的playbook的介绍-yaml ansible的playbook是使用yaml语言写的 YAML标记语言介绍YAML是一个可读性高的用来表达资料序列的格式.YAML参考了其他多种语言 ...
- 1753 -- Flip Game
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48663 Accepted: 20724 Descr ...
- 【Mybatis】向MySql数据库插入千万记录 单条插入方式,用时 1h16m30s
本例代码下载:https://files.cnblogs.com/files/xiandedanteng/InsertMillionComparison20191012.rar 相对于批量插入,这种方 ...
- excel导出显示默认格子
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:o ...
- 使用vagrant一键部署本地php开发环境(二)制作自己的vagrant box
在上篇的基础上 ,我们已经安装好了virtualbox和vagrant,没有安装的话,参照上篇 使用vagrant一键部署本地php开发环境(一) 1.从网易镜像或阿里等等镜像下载Centos7 ht ...
- CentOS 7系统配置上的变化
http://www.linuxidc.com/Linux/2014-09/107375p4.htm CentOS 7系统配置上的变化解析 ip ss指令替代 ifconfig route arp n ...
- Lucence简单学习---1
package cn.itheima.lucene; import java.io.File; import java.util.ArrayList; import java.util.List; i ...
- react-native-picke Cannot read property '_init' of undefined
使用react-native-picker报以下错误: 查看了react-native-picke的issues: https://github.com/beefe/react-native-pick ...
- Golang gRPC微服务01: 介绍
gRPC 是什么 gRPC是goole开源的一个RPC框架和库,支持多语言之间的通信.底层通信采用的是 HTTP2 协议.gRPC在设计上使用了 ProtoBuf 这种接口描述语言.这种IDL语言可以 ...
- 【OpenCV入门教程之一】 OpenCV 2.4.8 +VS2010的开发环境配置
目录(?)[-] 因为读研期间的研究方向是图像处理所以浅墨这段时间闭门研究了很多OpenCV和图像处理相关的知识与内容眼看自己积累到一定的程度了于是决定开始开设这个OpenCV系列专栏总结自己所学也分 ...