Spring 启动桌面独立应用程序中的 Apache Shiro

Apache Shiro in Spring Boot Desktop Standalone Application

我曾经在桌面独立应用程序中成功实现了 Apache Shiro(没有 Spring 框架或任何与此相关的框架)。

我使用 INI 文件得到 SecurityManager 像这样:

Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
                            "classpath:resources/shiro.ini");
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject currentUser = SecurityUtils.getSubject();

我在 Whosebug post 中读到,在使用 spring 时不建议使用 shiro INI。

在我的例子中,我现在使用 Spring Boot with Spring Data JPA with Hibernate 在此应用程序中。什么是配置 Apache Shiro 的最佳方式,以便我可以最大限度地利用这个安全框架。

编辑

我遇到的所有示例,都显示了网络示例。我特别需要独立应用程序。示例将不胜感激。

解决方案

在从这个 post 中学习和回答之后,我设法想出了 this solution 并创建了 github 存储库。如果有人想让它变得更好,请成为我的客人。

据我所知,如果您正在开发基于 Web 的应用程序或独立应用程序,除非您在此处看到的是 webfilters official Apache shiro doc,否则不会抱怨这个示例显示了如何为两者正确配置 apache shiro ,基于网络和独立。

您仍然可以使用基于 xml 的配置,as is described here with spring boot and it's easy migrate configuration from INI files to XML using tags like or constructor-args as shown here spring xsd doc

在我看来,使用 INI 文件或基于 XML 的配置具有类似的优点和缺点,在这两种方法中,您都可以 "hot-swap" beans 配置。

例如,如果您使用 Java 或在代码上配置 Shiro 很容易,您可以查看此 github link,您可以在其中看到 Class 配置示例apache Shiro spring-引导应用程序。

忘记 Web 过滤器并配置其他 bean,因为在此链接上它应该可以正常工作。

据我所知,在 spring 启动时使用 xml 或 class 配置并没有好的或坏的方法,它只取决于您的需求.

为了结束,我编写了一个简单的示例 Spring-boot-standalone with shiro. 供您演示基于 XML 的配置,这将打印领域 bean 以显示上下文已启动并且已注释代码以实现你的需要,剩下的必须是 shiro 网页教程的完整 bean。

你们好。

您可以使用 yml 文件添加 shiro 配置以及应用程序属性。 例如,看看 https://github.com/johntostring/spring-boot-shiro