2021-12-28 shiro-core 自定义realm

2021SC@SDUSC
我们要自定义Realm,首先我们需要先确定我们定义的Realm类中所需要的功能都需要什么,我们需要缓存功能,认证功能,授权功能,三大功能 .我们首先看一下AuthorizingRealm的继承图,如下图所示:
在这里插入图片描述
我们只需要继承于 AuthorizingRealm来实现我们的Realm子类即可,并重写缓存,认证,和授权功能。
CustomRealm

package com.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

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

public class CustomRealm extends AuthorizingRealm {

    //模拟用户数据
    HashMap<String,String> user = new HashMap<>();

    {
       user.put("Hash","a01aaefbb27fca4fafd66ba70ecf3b06");
       user.put("Mark","283538989cef48f3d7d8a1c1bdf2008f");
       super.setName("customRealm");
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String userName = (String) principals.getPrimaryPrincipal();
        
        //从数据库或者缓存中获取角色数据和权限
        Set<String> roles = getRolesByUserName(userName);
        Set<String> promissions = getPromissionsByUserName(userName);

        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setRoles(roles);
        authorizationInfo.setStringPermissions(promissions);
        return authorizationInfo;
    }

    private Set<String> getPromissionsByUserName(String userName) {
        Set<String> set = new HashSet<>();
        set.add("user:add");
        set.add("user:delete");
        return set;
    }

    private Set<String> getRolesByUserName(String userName) {
        Set<String> set = new HashSet<>();
        set.add("admin");
        set.add("user");
        return set;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        //1. 从主体传过来的认证信息中,获得用户名
        String userName = (String) token.getPrincipal();

        //2. 通过用户名到数据库中获取凭证
        String password = getPasswordByUserName(userName);

        if (password==null){
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName,password,"customRealm");
        //注意,盐的获取也应该从数据库或者缓存中获得
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(userName));
        return authenticationInfo;
    }

    //模拟数据库
    private String getPasswordByUserName(String userName) {
        return user.get(userName);
    }

    public static void main(String[] args) {
        Md5Hash md5Hash = new Md5Hash("123456","Hash");
        System.out.println(md5Hash.toString());
    }
}

CustomRealmTest

package com.shiro.test;

import com.shiro.realm.CustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class CustomRealmTest {
    @Test
    public void testAuthentication(){

        CustomRealm customRealm = new CustomRealm();

        //1.构建securityManager环境
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        defaultSecurityManager.setRealm(customRealm);


        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");
        matcher.setHashIterations(1);
        customRealm.setCredentialsMatcher(matcher);

        //2.主体提交认证请求
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("Mark","123456");

        subject.login(token);
        System.out.println(token.getUsername()+" isAuthenticated: "+subject.isAuthenticated());

        subject.checkRole("admin");
        subject.checkPermission("user:add");

        subject.logout();
        System.out.println(token.getUsername()+" isAuthenticated: "+subject.isAuthenticated());

    }
}

运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值