设为首页 加入收藏

TOP

shiro小记
2019-10-09 20:03:23 】 浏览:14
Tags:shiro 小记

今天主要看了shiro的认证,授权功能初步了解了一下,其他的功能用的不多,之后再看。

认证

下面的例子是以继承了AuthenticatingRealm的自定义Realm来实现自定义认证。
认证依赖于方法doGetAuthenticationInfo,需要返回一个AuthenticationInfo,通常返回一个他的子类SimpleAuthenticationInfo,构造方法的第一个参数是用户名,第二个是验证密码,第三个是当前realm的className。

 

package com.demo.realms;

import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;

public class MyRealm extends AuthenticatingRealm {
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)  {
        System.out.println("MyRealm认证中---->用户:"+token.getPrincipal());
        // 可以从token中获取用户名来从数据库中查询数据
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String password="123456";// 假设这是从数据库中查询到的用户密码
        // 创建一个SimpleAuthenticationInfo,第一个参数是用户名,第二个是验证密码,第三个是当前realm的className
        // 验证密码会与用户提交的密码进行比对
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(upToken.getUsername(),password,this.getName());
        return info;
    }
}

 

授权

下面的例子是以继承了AuthorizingRealm的自定义Realm来实现自定义认证和自定义授权。
授权依赖于方法doGetAuthorizationInfo,需要返回一个AuthorizationInfo,通常返回一个他的子类SimpleAuthorizationInfo。构造SimpleAuthorizationInfo可以空构造,也可以传入一个Set<String> roles来构造。

 

package com.demo.realms;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.HashSet;
import java.util.Set;

public class RealmForDouble extends AuthorizingRealm {
//    授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        // 1. 获取授权的用户
        Object principal = principals.getPrimaryPrincipal();
        System.out.println("RealmForDouble授权中---->用户:"+principal);
        //2.下面使用Set<String> roles来构造SimpleAuthorizationInfo
        SimpleAuthorizationInfo info = null;
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Set<String> roles = new HashSet<>();
        if ("admin".equals(principal)){
            roles.add("admin"); // 假设这个角色是从数据库中查出的
            // 如果SimpleAuthorizationInfo实例化了,
            // 可以这样来加角色,行为需要这样添加
            // 角色可以传构造函数来实例化SimpleAuthorizationInfo
//            info.addRole("admin");
//            info.addStringPermission("*");
        }
        if ("guest".equals(principal)){
            roles.add("guest");
        }
        info = new SimpleAuthorizationInfo(roles);
        return info;
    }

//    认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("RealmForDouble认证中---->用户:"+token.getPrincipal());
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String password="123456";// 假设这是从数据库中查询到的用户密码
        // 创建一个SimpleAuthenticationInfo,第一个参数是用户名,第二个是验证密码,第三个是当前realm的className
        // 验证密码会与用户提交的密码进行比对
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(upToken.getUsername(),password,this.getName());
        return info;
    }
}

  

以上内容摘自博客:https://www.cnblogs.com/progor/p/10970971.html#%E4%BE%9D%E8%B5%96%E5%8C%85

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇springboot+vue2.x 解决session跨.. 下一篇Eureka参数配置->Server端参数

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目