自定义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"); }
}

最新文章

  1. event.srcElement ,event.fromElement,event.toElement
  2. Python抓取网页中的图片到本地
  3. DOJO官方API翻译或解读-dojo/store (自定制存储器)
  4. 设计模式之适配器模式(Adapter)
  5. Python定时调度--多任务同一时间开始跑 scheduler.enterabs
  6. EF selection expression 与 Linq备忘
  7. bzoj 1195: [HNOI2006]最短母串 爆搜
  8. C++ const 限定符
  9. DirectFB的架构介绍
  10. shiro缓存管理
  11. linux7 安装rac 执行root脚本时候报错
  12. centos yum install oracle java
  13. 记一次Springboot启动异常
  14. Gym 101775C - Traffic Light - [思维题]
  15. OKHttp使用详解
  16. Cordova+Vue快速搭建Hybrid App
  17. MPT树详解
  18. FreeCommander 学习手册
  19. 页面title改变浏览器兼容性问题
  20. springboot实现java代理IP池 Proxy Pool,提供可用率达到95%以上的代理IP

热门文章

  1. idea git操作 -- 已有项目添加到git
  2. LC 275. H-Index II
  3. Hive explode
  4. Error response from daemon: driver failed programming external connectivity on endpoint httptest (9bb351e8d0738501ae2c57d1bfe3b18aced708d9dc66a63f642c5918cb144340): (iptables failed: iptables --wait
  5. Java内部类(4):静态内部类&amp;接口内部类
  6. IntelliJ IDEA入门之常用配置以及问题解决(持续更新中)
  7. MariaDB基本知识点总结01--介绍+语句
  8. freeRTOS学习8-22
  9. Fabric Raft机制理解
  10. git stash save -a 遇到的坑 , 弹出匿藏错误