前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【jfinal修仙系列】修改ShiroPlugin支持jfinal3.0

【jfinal修仙系列】修改ShiroPlugin支持jfinal3.0

作者头像
冷冷
发布2018-02-08 12:20:15
6270
发布2018-02-08 12:20:15
举报
文章被收录于专栏:冷冷冷冷冷冷

在升级到jfinal3.0 以后,原有的shiroplugin不兼容,原来的 routes 是用通过JfinalConfig 注入

new ShiroPlugin(routes)

3.0 routes.getEntrySet() 不再支持。

修改ShiroPlugin 的 star方法

public boolean start() {
        Set<String> excludedMethodName = buildExcludedMethodName();
        ConcurrentMap<String, AuthzHandler> authzMaps = new ConcurrentHashMap<String, AuthzHandler>();
        //逐个访问所有注册的Controller,解析Controller及action上的所有Shiro注解。
        //并依据这些注解,actionKey提前构建好权限检查处理器。
        for (Routes routes : Routes.getRoutesList()) {
            for (Routes.Route route : routes.getRouteItemList()) {
                Class<? extends Controller> controllerClass = route.getControllerClass();

                String controllerKey = route.getControllerKey();

                // 获取Controller的所有Shiro注解。
                List<Annotation> controllerAnnotations = getAuthzAnnotations(controllerClass);
                // 逐个遍历方法。
                Method[] methods = controllerClass.getMethods();
                for (Method method : methods) {
                    //排除掉Controller基类的所有方法,并且只关注没有参数的Action方法。
                    if (!excludedMethodName.contains(method.getName())
                            && method.getParameterTypes().length == 0) {
                        //若该方法上存在ClearShiro注解,则对该action不进行访问控制检查。
                        if (isClearShiroAnnotationPresent(method)) {
                            continue;
                        }
                        //获取方法的所有Shiro注解。
                        List<Annotation> methodAnnotations = getAuthzAnnotations(method);
                        //依据Controller的注解和方法的注解来生成访问控制处理器。
                        AuthzHandler authzHandler = createAuthzHandler(
                                controllerAnnotations, methodAnnotations);
                        //生成访问控制处理器成功。
                        if (authzHandler != null) {
                            //构建ActionKey,参考ActionMapping中实现
                            String actionKey = createActionKey(controllerClass, method, controllerKey);
                            //添加映射
                            authzMaps.put(actionKey, authzHandler);
                        }
                    }
                }
            }
        }
        //注入到ShiroKit类中。ShiroKit类以单例模式运行。
        ShiroKit.init(authzMaps);
        /**
         * 设定登录,登录成功,未授权等url地址
         */
        ShiroKit.setLoginUrl(loginUrl);
        ShiroKit.setSuccessUrl(successUrl);
        ShiroKit.setUnauthorizedUrl(unauthorizedUrl);
        return true;
    }

OK shiro 就可以启动起来了。

扩展shiro标签支持 JfinalTemplate

jfinal提供了很简单的指令扩展,我们通过继承Directive来实现。

/**
 * Created by jie on 2017/4/3.
 * 用户已经身份验证/记住我登录后显示相应的信息。
 * #shiroUser()
 * body
 * #end
 */
public class ShiroUserTag extends SecureTag {

    public void exec(Env env, Scope scope, Writer writer) {
        if (getSubject() != null && getSubject().getPrincipal() != null)
            stat.exec(env, scope, writer);
    }

    public boolean hasEnd() {
        return true;
    }
}

在config中配置模板
me.addDirective("shiroUser", new ShiroUserTag());

则页面中可以

#shiroUser()
  登录认证以后显示
#end

其他标签定义看参考 http://git.oschina.net/log4j

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 修改ShiroPlugin 的 star方法
  • 扩展shiro标签支持 JfinalTemplate
相关产品与服务
多因子身份认证
多因子身份认证(Multi-factor Authentication Service,MFAS)的目的是建立一个多层次的防御体系,通过结合两种或三种认证因子(基于记忆的/基于持有物的/基于生物特征的认证因子)验证访问者的身份,使系统或资源更加安全。攻击者即使破解单一因子(如口令、人脸),应用的安全依然可以得到保障。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档