Apache Shiro vs Spring Security

今天看到一篇关于Shiro的统一认证授权的博文,突然想到了自己曾经在SpringBoot项目中使用过的Spring Security,这两者有什么区别呢,首先我们先简短的介绍一下 Shiro 与 Spring Security。

Apache Shiro

Apache Shiro的两个主要特征(“shiro”= jap。“castle”)是它的简单性和容器独立性。它的核心功能是身份验证,授权,加密和会话管理。

通过设计验证简单直观。该过程基于主题,由开发人员仅使用少量方法调用来执行。丰富的异常层次结构有助于进行错误诊断。记住 - 我的功能本身就包含在内。可插拔DAO可用于实现领域,并且可以将主题登录到多个领域,同时保持统一的视图。

授权也是基于主题的。访问权限由主体角色权限决定。要立即开始实现权限,可以使用Shiro的自制通配符语法。为了改善用户体验和性能,支持不同的缓存解决方案。

密码学功能以简单为中心。Shiro的主要目标是轻松使用Java Cryptography Extension。由于Shiro的API是接口驱动的和基于POJO的,因此可以使用任何兼容JavaBeans的格式轻松配置加密组件。

即使对于非基于Web的应用程序,也可以使用Shiro进行会话管理。由于会话对象也是POJO,因此可以将它们保存在任何类型的存储上。这里也可以使用一些比较流行的缓存解决方案。

此外,Shiro还提供特定于Web的功能。通过web.xml为您的应用程序启用Shiro,您可以保护任何URL,还可以为每个URL指定过滤器链。

Spring Security

Spring Security的功能以身份验证和授权为中心。它可以与Spring-less应用程序一起使用,也可以与基于Spring的应用程序一起使用。这主要是因为Spring Security与Apache Shiro一样,与容器无关。

Spring Security在2003年底开始作为一个名为“Spring的Acegi安全系统”的项目,当时这是一个简单的实现,最初没有发布。随着更多Spring社区成员要求提供安全功能,他们获得了上述项目。2004年初,大约有20人在使用该项目。越来越多的人加入并最终在2004年3月创建了一个SourceForge项目。2006年5月,Acegi成为Spring官方子项目,2007年成为Spring Portfolio的一部分,并更名为“Spring Security”。

两者比较

文档

Spring Security的文档非常全面。如果您想了解更多信息,可以快速入门并继续阅读。除了一些基本的应用程序安全术语,它涵盖了Spring Security可以执行的所有操作。或者至少你可能会觉得这样,因为当你读到某个组件时,可能会提到五个(左右)新的相关组件。所以它可能会让你感到有些不知所措。

与其相比,Apache Shiro它确实解释了一些基本的术语和定义,以及Shiro本身如何工作或者可以用它做什么的大部分内容。但是,请注意它被称为“大多数”,而不是“全部”,因为您可能会遇到一些空的“TODO”部分。

使用便捷度

对于使用声明式样式的非常基本的配置,所有这两个都同样容易,非要排序的话Shiro领先Spring Security。

Apache Shiro示例

Shiro的声明性配置风格真正令人感到舒服的是它的简洁明了而且很容易。您可以在简单的INI文件中编写配置。XML仅用于启用Shiro本身。

这是你的shiro.ini的样子:

[main] 
# login / logout configuration | authc = shiro standard FormAuthenticationFilter
authc.loginUrl = /login.html  # specifying which URL to redirect to, when Shiro attempts to authenticate a user via authc
authc.successUrl = /index.html  # the redirect-URL after a successful login
logout.redirectUrl = /logout.html  # the redirect-URL after a successful logout
 
[users]
# define some test users
jan = passwort, employer  # format: username = password[, role1[, role2[, roleN]]]
naj = passwort, employee
ajn = passwort
 
[roles]
# define permissions for roles
employer = *  # granted all permissions
employee = shop:browse:*  # granted permission to browse the shop and do any related action
 
[urls]
# protect urls
/login.html = authc  # this step is necessary to register /login.html as the loginUrl of the authc filter
/index.html = authc  # catch requests to /index.html and let the authc filter process them
/ = authc  # same as above
 
# specify logout url
/logout = logout  # catch requests to /logout and let the logout filter process them
 
# REST urls | noSessionCreation is used to avoid creation of sessions | authcBasic = shiro's standard BasicHttpAuthenticationFilter
/rest/shop/browse/** = noSessionCreation, authcBasic, perms["shop:browse:*"]  # the perms filter additionally checks the subject for the specified permissions
/rest/shop/add = noSessionCreation, authcBasic, roles[employer]  # the roles filter checks the subject for the specified roles
/rest/shop/delete/* = noSessionCreation, authcBasic, roles[employer]

Spring安全示例

Spring Security的基本配置非常简单。它甚至可能比Shiro更容易一些。它将创建一个登录页面(如果您没有),并添加针对CSRF和会话固定的保护。尽管如此,自动保护最适合JSP。HTML需要解决方法。

要实现自定义组件,您必须学习XML中的bean定义并使用Spring注入。这可能导致很多bean声明,使得.xml难以阅读。此外,使用注释需要进一步的Spring或AspectJ依赖项。

例:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">
 
    <!-- use the <http> element to configure a filter chain -->
    <http pattern="/rest/**" create-session="never">  <!-- apply chain on a specific URL using 'pattern' | control session creation using 'create-session' -->
        <intercept-url pattern="/**" access="hasRole('EMPLOYER')" />  <!-- access to every URL following /rest/ is restricted to the 'EMPLOYER' role -->
        <http-basic />  <!-- turn on HTTP Basic Authentication -->
    </http>
    <http>  <!-- a <http> element without a pattern attribute is applied to all incoming requests that are not caught by other <http> elements before it -->
        <intercept-url pattern="/login.jsp" access="hasRole('ANONYMOUS')" />  <!-- grant everyone access to /login.html -->
        <intercept-url pattern="/index.jsp" access="hasRole('EMPLOYEE') or hasRole('EMPLOYER')" />  <!-- restrict access to index.jsp to subjects in the 'EMPLOYEE' or 'EMPLOYER' role -->
        <form-login login-page="/login.jsp" default-target-url="/index.jsp"
            always-use-default-target="true" />  <!-- turn on form authentication, set default success url, tell spring to always use that url -->
        <logout />  <!-- turn on logout -->
        <remember-me />  <!-- turn on remember me -->
        <csrf disabled="true" />  <!-- turn on csrf protection -->
    </http>
         
    <authentication-manager>  <!-- turn on authentication services -->
        <authentication-provider>  <!-- compares the supplied username/password combination with the ones stored in <user-service> -->
            <user-service>  <!-- store username/password pairs in-memory -->
                <user name="jan" password="passwort" authorities="ROLE_EMPLOYER" />
                <user name="naj" password="passwort" authorities="ROLE_EMPLOYEE" />
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

社区支持

这里没有什么可说的,因为Shiro相比,Spring Security拥有庞大的社区。关于Spring Security的Stack Overflow(SO)有很多问题和答案,因此最有可能找到一些问题的提示。此外,官方论坛不再受支持,它们仅在读取模式下可用。支持已转移到SO。在那里,mods监视某些标签。

Shiro的支持也很多。有一个中等活动的官方用户论坛。可能你在其他地方去查找你的问题不会有确切的答案并且解决意外问题可能需要一些时间。

然而,Spring Security与Shiro都在积极发展不断改进中。

使用Google趋势对流行度进行视觉比较。

差异化功能

Shiro以其简洁和灵活性而闪耀。INI配置进一步强调了这一点。Shiro也尽可能独立于其他技术。它还需要非常少的依赖项,使其轻量级。非Web应用程序(如CLI客户端或移动应用程序)和通配符权限语法的会话管理也很可观。

Spring Security的自动保护机制使其在Web应用程序的上下文中具有初始优势。总的来说,可以说该框架在网络方面相对全面。然而,在某些情况下,这是以相当重量级并且与Spring技术绑定为代价的。与Shiro的通配符权限语法相比,Spring Security中的权限可以基于Spring EL实现。

结论

如果您的应用程序相当小而用户和角色不是太多而且不需要使用任何过于高级的功能,请随意使用Java EE安全性。它为此提供了坚实的基础。但是,Java EE Security的可能性很快就会耗尽。例如,您只能为整个应用程序指定一种身份验证机制。此外,如果应用程序需要是可移植的,那么一定要使用其他两个框架中的一个。

现在,如果需要一个基本上独立,轻量级和可扩展的安全解决方案,那么Apache Shiro就是您的最佳选择。然而,缺点是可能需要一些时间来克服问题。可能还必须自己实现一些功能。然而,Shiro的设计(界面驱动和基于POJO)促进了这一点。

最后,如果应用程序已经是基于Spring的,那么可能有人会保持一致性并使用Spring Security,在这并没有任何问题(除了Spring Security实施起来有点困难)。如果之前从未使用过Spring,首先,高级功能的实现更加困难,除非包含Spring本身或AspectJ,否则无法使用注释。此外,如果需要Spring OAuth2,则必须使用spring-mvc而不是Jersey或RESTeasy来创建REST资源。

有了这个,我们的比较就结束了。再次提醒我们观察的相对性。自己试验框架并使用最适合您需求的框架。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值