Shiro框架原理

Shiro框架原理

一个简单shiro案例

@Component
public class SimpleService {

    private static Logger log = LoggerFactory.getLogger(SimpleService.class);

    @RequiresPermissions("write")
    public void writeRestrictedCall() {
        log.info("executing method that requires the 'write' permission");
    }

    @RequiresPermissions("read")
    public void readRestrictedCall() {
        log.info("executing method that requires the 'read' permission");
    }
}
@Component
public class QuickStart {

    private static Logger log = LoggerFactory.getLogger(QuickStart.class);

    @Autowired
    private SecurityManager securityManager;

    @Autowired
    private SimpleService simpleService;

    public void run() {

        // get the current subject
        Subject subject = SecurityUtils.getSubject();

        // Subject is not authenticated yet
        Assert.isTrue(!subject.isAuthenticated());

        // login the subject with a username / password
        UsernamePasswordToken token = new UsernamePasswordToken("joe.coder", "password");
        subject.login(token);

        // joe.coder has the "user" role
        subject.checkRole("user");

        // joe.coder does NOT have the admin role
        Assert.isTrue(!subject.hasRole("admin"));

        // joe.coder has the "read" permission
        subject.checkPermission("read");

        // current user is allowed to execute this method.
        simpleService.readRestrictedCall();

        try {
            // but not this one!
            simpleService.writeRestrictedCall();
        }
        catch (AuthorizationException e) {
            log.info("Subject was NOT allowed to execute method 'writeRestrictedCall'");
        }

        // logout
        subject.logout();
        Assert.isTrue(!subject.isAuthenticated());
    }


    /**
     * Sets the static instance of SecurityManager. This is NOT needed for web applications.
     */
    @PostConstruct
    private void initStaticSecurityManager() {
        SecurityUtils.setSecurityManager(securityManager);
    }
}

运行结果
Connected to the target VM, address: ‘127.0.0.1:8304’, transport: ‘socket’
2019-08-20 23:12:10,976 INFO [org.apache.shiro.session.mgt.AbstractValidatingSessionManager] - Enabling session validation scheduler…
Disconnected from the target VM, address: ‘127.0.0.1:8304’, transport: ‘socket’
2019-08-20 23:12:11,257 INFO [org.apache.shiro.samples.spring.SimpleService] - executing method that requires the ‘read’ permission
2019-08-20 23:12:11,258 INFO [org.apache.shiro.samples.spring.QuickStart] - Subject was NOT allowed to execute method ‘writeRestrictedCall’

  • shiro会向容器中注册自动代理创建器,以及增强器。
 @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    protected DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        return super.defaultAdvisorAutoProxyCreator();
    }

    @Bean
    protected AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        return super.authorizationAttributeSourceAdvisor(securityManager);
    }

StaticMethodMatcherPointcutAdvisor 既是增强器,也是切点。
AuthorizationAttributeSourceAdvisor  UML
UML组件基本介绍:

  • PointcutAdvisor 帮忙获取Pointcut,获取通知Advice (AopUtils 277-279)
    在这里插入图片描述
  • Pointcut 帮助找到MethodMatcher (AopUtils 224行)
    在这里插入图片描述
  • MethodMatcher 决定当前类的实例是否需要自动代理 (AopUtils 243行)
    在这里插入图片描述
  • Advice 本质上就是MethodInterceptors(复数)方法拦截器数组
    在这里插入图片描述
//StaticMethodMatcherPointcutAdvisor 重写了MethodMatcher方法,当spring获取到所有的Advisor增强器
//后,会判断当前这个类,是否需要应用代理。A
  public boolean matches(Method method, Class targetClass) {
        Method m = method;

        if ( isAuthzAnnotationPresent(m) ) {
            return true;
        }

        //The 'method' parameter could be from an interface that doesn't have the annotation.
        //Check to see if the implementation has it.
        if ( targetClass != null) {
            try {
                m = targetClass.getMethod(m.getName(), m.getParameterTypes());
                return isAuthzAnnotationPresent(m) || isAuthzAnnotationPresent(targetClass);
            } catch (NoSuchMethodException ignored) {
                //default return value is false.  If we can't find the method, then obviously
                //there is no annotation, so just use the default return value.
            }
        }

        return false;
    }

AopUtils 242行体现
在这里插入图片描述
如果在方法上标有这些注解,那么则创建代理对象。

@ RequiresPermissions,@RequiresRoles.class,
                    @RequiresUser.,@ RequiresGuest.@RequiresAuthentication
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值