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安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户“登录”: 授权 ...
随机推荐
- springboot项目的maven的pom.xml文件第一行报错 Unknown Error
springboot项目的maven的pom.xml文件第一行报错 Unknown Error https://blog.csdn.net/mini_jike/article/details/9239 ...
- hibernate关联映射之一对多&多对一
package loaderman.b_one2Many; import java.util.HashSet; import java.util.Set; public class Dept { pr ...
- spring cloud consul上下线体验
spring cloud consul中默认会将spring.application.name作为ID 同一服务起多个实例时,ID默认会变成${spring.application.name}-${s ...
- [jquery]JSON.parse()与JSON.stringify()
JSON.parse()[从一个字符串中解析出json对象] 例子: //定义一个字符串 var data='{"name":"goatling"}' //解析 ...
- mac被锁有pin码的解锁方法
停用规律: 错误5次密码停用1分钟 再错误3次停用5分钟 在错误1次就停用15分钟 再错误1次就是60分钟了,而且还没输入框了,这时候我们要通过按 option,commond这2个按钮来唤起输入框 ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_6-06 zuul微服务网关集群搭建
笔记 6.Zuul微服务网关集群搭建 简介:微服务网关Zull集群搭建 1.nginx+lvs+keepalive https://www.cnblogs.com/liuyisai/ ...
- 在jsp页面如何获得url参数
方法一:当一个url过来时,如:http://localhost:8080/pro/demo/hello.jsp?name=john,在hello.jsp页面,我们可以这样得到name的值: < ...
- SpringBoot之解决一对一、多对一、多对多等关联实体在JSON序列化/输出时产生的无限递归死循环问题(infinite recursion)
前言 这问题着实让人苦不堪言,有必要把它记下了. @JsonBackReference [亲测有效] 1.使用注解@JsonBackReference标记在有关联关系的实体属性上 2.仅导入此注解类有 ...
- nodeslector使用
问题: node节点挂了一个, 无法切换到另一个node上 解决: .指定了 nodeslector .设置了下面: hostNetwork: true dnsPolicy: ClusterFirst ...
- js如何控制select展开
找了一圈也没找到靠谱的方案,后来通过动态的控制select的size属性实现了. 这也算是一种方法吧. 先判断option的数量n,然后把select的size调整到n,当用户选择后,再把size设置 ...