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安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户“登录”: 授权 ...
随机推荐
- HearthBuddy 日志模块
// Triton.Common.LogUtilities.CustomLogger // Token: 0x04000BD8 RID: 3032 private Level level_0 = Le ...
- 如何配置git send-email相关的邮箱信息?
关键是配置smtpserver,请参考此处
- IE下 CSS hover iframe失效
预期:某个div下存在iframe子元素,当鼠标移动到该div下,该iframe出现,移出则iframe消失,移入iframe不会引起iframe消失. 问题:在火狐下结果满足预期,在IE下,鼠标移入 ...
- vue引入插件方法
jQuery插件npm install jquery --save-dev 需要用jQuery的文件中引入:import $ from 'jquery' 轮播图插件安装:npm install vue ...
- WebApi实现通讯加密 (转)
http://www.cnblogs.com/jonneydong/p/WebApi_Encryption.html 一. 场景介绍: 如题如何有效的,最少量的现有代码侵入从而实现客户端与服务器之间的 ...
- React Native创建项目等待时间长解决
1. 初始化工程 在终端输入命令 :react-native init AwesomeProject 从命令上看,看起来是初始化一个工程,于是, 1分钟...... 10分钟...... 1小时... ...
- AC自动机--summer-work之我连模板题都做不出
这章对现在的我来说有点难,要是不写点东西,三天后怕是就一无所有了. 但写这个没有营养的blog的目的真的不是做题或提升,只是学习学习代码和理解一些概念. 现在对AC自动机的理解还十分浅薄,这里先贴上目 ...
- 刚开始使用idea的朋友,可以看一下下面这篇文章
刚开始使用idea的朋友,可以点击本链接看一下这篇文章 以及这些文章 http://www.jetbrains.com/help/idea/getting-help.html------ Gettin ...
- LeetCode.1189-balloon实例数最大值(Maximum Number of Balloons)
这是小川的第次更新,第篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第267题(顺位题号是1189).给定一个字符串文本,使用文本字符来构成单词"balloon&qu ...
- guns系统部署方式及常见问题
项目框架:guns 开发IDE:Idea 2018.1 两种打包方式:war 和jar. 1.正常打包的姿势 1.1按照下图修改为你想到打包的方式. 1.2 执行打包 clean packa ...