shiro学习(二)
2024-09-05 10:48:09
自定义realm(主要就是自定义的realm要继承AuthorizingRealm 类,重写两个方法(一是认证,而是授权,两个方法一般要查询数据库,这里用的是模拟数据)
)
public class CustomRealm extends AuthorizingRealm {
Map<String,String> map=new HashMap<>(16);
{
map.put("mark","123456");
super.setName("customRealm");
}
//Principal 重要,主角的意思 ,Set集合无序,且不可重复
@Override //z 授权
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String username = (String) principals.getPrimaryPrincipal();
//从数据库或者缓存中获取角色数据
Set<String> roles=getRolesByUserName(username);
//从数据库或者缓存中获取角色的权限数据
Set<String> permissions=getPermissionsByUserName(username);
//创建AuthorizationInfo授权对象
SimpleAuthorizationInfo simpleAuthorizationInfo=
new SimpleAuthorizationInfo();
//设置角色
simpleAuthorizationInfo.setRoles(roles);
//设置权限
simpleAuthorizationInfo.setStringPermissions(permissions);
return simpleAuthorizationInfo;
} private Set<String> getPermissionsByUserName(String username) {
//模拟数据取数据,roles的权限数据
Set<String> permissions=new HashSet<>();
permissions.add("user:select");
permissions.add("user:delete");
permissions.add("user:update");
permissions.add("user:insert");
return permissions;
} private Set<String> getRolesByUserName(String username) {
//模拟数据取数据,roles数据
Set<String> roles=new HashSet<>();
roles.add("admin");
roles.add("user");
return roles;
} @Override //c 认证
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//1.从主体传过来的认证信息中,获取用户名
String username = (String) token.getPrincipal();
//2.通过用户名,到数据库中获取凭证(这里不查数据库,写死)
String password = getPasswordByUserName(username);
if (password==null){
return null;
}
//返回对象SimpleAuthenticationInfo
SimpleAuthenticationInfo authenticationInfo=
new SimpleAuthenticationInfo(username,password,"customRealm");
return authenticationInfo;
} //模拟数据库查凭证
private String getPasswordByUserName(String username) {
//从map集合获取密码
return map.get(username);
}
}
测试代码
public class CustomRealmTest {
@Test
public void testCustomRealm(){
CustomRealm customRealm=new CustomRealm();
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
defaultSecurityManager.setRealm(customRealm);
SecurityUtils.setSecurityManager(defaultSecurityManager); Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("mark","123456");
//认证
subject.login(token);
//授权
subject.checkRoles("admin","user");
subject.checkPermissions("user:select","user:update"); }
}
最新文章
- event.srcElement ,event.fromElement,event.toElement
- Python抓取网页中的图片到本地
- DOJO官方API翻译或解读-dojo/store (自定制存储器)
- 设计模式之适配器模式(Adapter)
- Python定时调度--多任务同一时间开始跑 scheduler.enterabs
- EF selection expression 与 Linq备忘
- bzoj 1195: [HNOI2006]最短母串 爆搜
- C++ const 限定符
- DirectFB的架构介绍
- shiro缓存管理
- linux7 安装rac 执行root脚本时候报错
- centos yum install oracle java
- 记一次Springboot启动异常
- Gym 101775C - Traffic Light - [思维题]
- OKHttp使用详解
- Cordova+Vue快速搭建Hybrid App
- MPT树详解
- FreeCommander 学习手册
- 页面title改变浏览器兼容性问题
- springboot实现java代理IP池 Proxy Pool,提供可用率达到95%以上的代理IP
热门文章
- idea git操作 -- 已有项目添加到git
- LC 275. H-Index II
- Hive explode
- Error response from daemon: driver failed programming external connectivity on endpoint httptest (9bb351e8d0738501ae2c57d1bfe3b18aced708d9dc66a63f642c5918cb144340): (iptables failed: iptables --wait
- Java内部类(4):静态内部类&;接口内部类
- IntelliJ IDEA入门之常用配置以及问题解决(持续更新中)
- MariaDB基本知识点总结01--介绍+语句
- freeRTOS学习8-22
- Fabric Raft机制理解
- git stash save -a 遇到的坑 , 弹出匿藏错误