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安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户“登录”: 授权 ...
随机推荐
- 整合pjax无刷新
一:整合pjax的准备工作: 检查你的网站是否引入1.7.0版本以上的jquery.js,如果没有请全局引入 1.新浪CDN提速:<script type="text/javascri ...
- jwt扩展
1.新建扩展类 package com.ireciting.uaaservice.config; import com.ireciting.uaaservice.pojo.TUser; import ...
- 图解Python 【第一篇】:Python基础1
本节内容一览图 一.Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间, ...
- C++中.cpp和.hpp的区别
原文地址:https://blog.csdn.net/qzx9059/article/details/89210571 c++中 cpp和hpp我们可以将所有东西都放在一个.cpp文件内,编译器会将这 ...
- driver.implicitly_wait不起作用的排错方法
环境是python3.6+selenium 3.4.3+chrome64 在编写前端自动化脚本时使用implicitly_wait时不起作用 1.确认自己是否正确使用,可用简单的例子来测试,比如: 此 ...
- 一百一十六:CMS系统之使用阿里大于sdk发送短信验证码
阿里大于短信平台:https://dysms.console.aliyun.com/dysms.htm#/overview 使用教程:https://blog.csdn.net/qq103189393 ...
- mariadb数据库(1)
一.什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方法来管理数据库里的数 ...
- Ubuntu开放指定端口
一般情况下,ubuntu安装好的时候,iptables会被安装上,如果没有的话那就安装上吧 安装 在终端输入 sudo apt-get install iptables 添加规则 在终端输入 ipta ...
- Android View重绘和更新: invalidate和requestLayout 总结的不错 赶紧复制。。哈哈
总述:View有两个很重要的方法:invalidate和requestLayout,常用于View重绘和更新. Invalidate:To farce a view to draw,call inva ...
- KDE-解决.docx .xlsx .pptx文档默认由Ark打开的问题
安装KDE后,默认的压缩解压程序变成了Ark,并且原来默认用WPS Office打开的.docx .xlsx .pptx文档,从文件管理器双击打开时,也变成了用Ark打开. 查了下网上的资料,可通过如 ...