五.shiro授权入门

一.maven的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tiglle</groupId>
  <artifactId>authorization</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>customRealm</name>
  <description>自定义realm</description>

    <!-- shiro -->
   <dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-core</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-- log -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.12</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>

  </dependencies>

</project>

二.shior的ini配置文件shiro-persimmion.ini:

# 用户 和 角色的配置
[users]
# 格式:
# 用户名=密码,角色1,角色2,角色3,...
wang=123,role1
zhang=abc123,role1,role2

# 角色的权限配置
[roles]
# 格式:角色1对资源user 1实例有create,update操作权限
# 角色1=资源:权限:实例,资源:权限:实例,没有实例时默认为*所有实例
# 细颗粒
role1=user:create:1,user:update:1
# 粗颗粒
role2=user:create,user:delete
role3=user:create

三.log4j的配置文件log4j.properties:

# Configure logging for testing: optionally with log file
log4j.rootLogger=DEBUG, stdout
# log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

四.授权测试的java类ShiroMain.java:

package com.tiglle.shiro;

import java.util.ArrayList;
import java.util.Arrays;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

public class ShiroMain {

    public static void main(String[] args) {
        //1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-persimmion.ini");
        //2、得到SecurityManager实例
        SecurityManager securityManager = factory.getInstance();
        // 并绑定给SecurityUtils  
        SecurityUtils.setSecurityManager(securityManager);
        //3、得到Subject
        Subject subject = SecurityUtils.getSubject();
        //创建用户名/密码身份验证Token(即用户身份/凭证)
        UsernamePasswordToken token = new UsernamePasswordToken("zhang","abc123");

        try {
            //4、登录,即主体身份验证  
            subject.login(token);
        } catch (Exception e) {
            //5、身份验证失败  
            e.printStackTrace();
        }
        boolean b = subject.isAuthenticated();
        System.out.println("身份验证结果:"+b);

        //先验证,在授权
        //1.基于角色的授权
        //判断主题是否有某角色
        boolean hasSomeRule = subject.hasRole("role1");
        System.out.println("单个角色判断:zhang是否拥有role1角色——"+hasSomeRule);
        //判断主体是否同时拥有多个角色
        boolean hasAllRules = subject.hasAllRoles(Arrays.asList("role1","role2"));
        System.out.println("多个角色判断:zhang是否同时拥有role1和role2角色——"+hasAllRules);

        //也可以使用check进行授权,授权失败会抛异常
        subject.checkRole("role1");

        //2.给予资源的授权
        //单个权限判断:是否对user的实例1有create操作
        boolean isPpermitted = subject.isPermitted("user:create:1");
        System.out.println("单个资源判断:zhang是否拥有user:create:1权限——"+isPpermitted);
        //多个权限判断
        boolean isPpermittedAll = subject.isPermittedAll("user:create:1","user:update:1");
        System.out.println("单个资源判断:zhang是否同时拥有user:create:1,user:update:1权限——"+isPpermittedAll);

        //也可以使用check进行授权,授权失败会抛异常
        subject.checkPermission("user:create:1");

        //6.退出
        subject.logout();
        System.out.println("退出后的身份验证结果:"+subject.isAuthenticated());
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值