shiro简单实现认证+授权(不连数据库)

shiro实现认证+授权简单实现(不连数据库)

  • 自定义realm继承AuthorizingRealm
  • 实现其中两个方法doGetAuthorizationInfo(授权),doGetAuthenticationInfo(认证),在方法中编写业务成需求
  • 返回值是SimpleAuthenticationInfo simpleAuthorizationInfo Simple**(授权,认证)
package com.jing.md5;

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.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.Arrays;

public class CustomMd5Realm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        // 获取主权限(登录用户)
        String primaryPrincipal = (String) principals.getPrimaryPrincipal();
//        System.out.println("primaryPrincipal = " + primaryPrincipal);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRole("user");
        simpleAuthorizationInfo.addRole("admin");
        // 对 user资源下所有用户有任意操作
//        simpleAuthorizationInfo.addStringPermission("user:*:*");
        // 添加权限字符串 user:*(user资源下的:所有操作:所有用户),添加多个权限字符串规则
        simpleAuthorizationInfo.addStringPermissions(Arrays.asList("user:*","product:create:01"));
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // 获取登录用户
        String principal = (String) token.getPrincipal();
        if ("xiaojing".equals(principal)) {
            return new SimpleAuthenticationInfo(principal, "7a3865843b5ac72bb4d9e28cd8877681", ByteSource.Util.bytes("zard"), this.getName());
        }
        return null;
    }
}

package com.jing.md5;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

import java.util.Arrays;

public class TestCustomMd5Realm {
    public static void main(String[] args) {

        // 获取默认的安全管理器
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();

        CustomMd5Realm realm = new CustomMd5Realm();
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        hashedCredentialsMatcher.setHashIterations(1024);
        realm.setCredentialsMatcher(hashedCredentialsMatcher);

        // 将自定义的Realm注入到安全管理器
        defaultSecurityManager.setRealm(realm);
        // 使用安全工具类 将安全管理器注入
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        // 获取主体
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("xiaojing", "123");
        try {
            subject.login(token);
            System.out.println("登录成功");
            if (subject.isAuthenticated()) {
                // 1 查询是否具有user角色权限
                boolean user = subject.hasRole("user");
                // 查询是否具有admin角色权限
                boolean admin = subject.hasRole("admin");
                System.out.println("admin = " + admin);     //  admin = true
                System.out.println("user = " + user);       //  user = true

                // 2 是否具有以下多角色权限
                boolean allRoles = subject.hasAllRoles(Arrays.asList("user", "admin","super"));
                System.out.println("allRoles = " + allRoles);  // allRoles = false

                System.out.println("=========================");
                //3 是否具有其中角色权限
                boolean[] booleans = subject.hasRoles(Arrays.asList("user", "super", "admin"));
                for (boolean aBoolean : booleans) {
                    System.out.println(aBoolean);
                    //  true
                    //  false
                    //  true
                }

                //4 基于权限字符串的访问控制   资源标志符:操作:资源类型(资源实例)
                boolean permitted = subject.isPermitted("user:create:*");
                System.out.println("permitted = " + permitted);     // permitted = true
                boolean permitted1 = subject.isPermitted("user:create:01");
                System.out.println("permitted1 = " + permitted1);   // permitted1 = true
                boolean permitted2 = subject.isPermitted("product:create:02");
                System.out.println("permitted2 = " + permitted2);   // permitted2 = false

                // 5 是否具有集合中所有权限
                boolean permittedAll = subject.isPermittedAll("user:create", "product:create:02");
                System.out.println("permittedAll = " + permittedAll);   // permittedAll = false
                
                // 6 是否具有其中权限返回布尔集合
                boolean[] permitteds = subject.isPermitted("user:delete:01", "product:create:01", "product:create:02");
                for (boolean b : permitteds) {
                    System.out.println(b);
                    // true
                    // true
                    // false
                }
            }

        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("账号错误");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("密码错误");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值