001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.support.shiro;
017
018import com.jfinal.aop.Interceptor;
019import com.jfinal.aop.Invocation;
020import io.jboot.support.shiro.processer.AuthorizeResult;
021
022/**
023 * Shiro 拦截器
024 */
025public class JbootShiroInterceptor implements Interceptor {
026
027    @Override
028    public void intercept(Invocation inv) {
029        // 优先执行 onInvokeBefore,得到 AuthorizeResult
030        // 如果 AuthorizeResult 不为 null,则说明用户自定义了其认证方式,比如 jwt、oss 等,此时直接返回给 onInvokeAfter
031        // 如果 AuthorizeResult 为 null,则由系统去执行(主要是去判断 Shiro 注解,然后通过对应的 Processor 去执行 )
032
033        JbootShiroManager manager = JbootShiroManager.me();
034
035        AuthorizeResult result = manager.getInvokeListener().onInvokeBefore(inv);
036
037        if (result == null) {
038            result = manager.invoke(inv);
039        }
040
041        if (result == null) {
042            result = AuthorizeResult.ok();
043        }
044
045        manager.getInvokeListener().onInvokeAfter(inv, result);
046    }
047
048}