132

I have currently evaluating Java based security frameworks, I am a Spring 3.0 user so it seemed that SpringSecurity would be the right Choice, but Spring security seems to suffer from excessive complexity, it certainly does not seem like it is making security easier to implement, Shiro seems to be much more coherent and easier to understand. I am looking for lists of pros and cons between these two frameworks.

4

3 回答 3

118

我也同意 Spring Security 感觉太复杂了(对我来说)。当然,他们已经做了一些事情来降低复杂性,比如创建自定义 XML 命名空间来减少 XML 配置的数量,但对我来说,这些并没有解决个人对 Spring Security 的基本问题:它的名称和概念通常会混淆我。很难“得到它”。

但是,当您开始使用 Shiro 的那一刻,您就“明白”了。在安全世界中难以理解的东西就更容易理解了。JDK 中难以忍受的难以使用的东西(例如密码)被简化到不仅可以忍受,而且通常使用起来很愉快的水平。

例如,如何在 Java 或 Spring Security 中对密码进行 hash+salt 和 base64 编码?两者都不像 Shiro 的解决方案那样简单直观:

ByteSource salt = new SecureRandomNumberGenerator().nextBytes();
new Sha512Hash(password, salt).toBase64();

不需要公共编解码器或其他任何东西。只是 Shiro 罐子。

现在关于 Spring 环境,大多数 Shiro 开发人员使用 Spring 作为他们的主要应用程序环境。这意味着 Shiro 的 Spring 集成非常出色,而且效果非常好。您可以放心,如果您正在编写 Spring 应用程序,您将获得全面的安全体验。

例如,请考虑此线程中另一篇文章中的 Spring XML 配置示例。以下是你在 Shiro 中(基本上)做同样事情的方法:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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>

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <property name="loginUrl" value="/login.jsp"/>
    <property name="successUrl" value="/home.jsp"/>
    <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
    <property name="filterChainDefinitions">
        <value>
        /secure/** = authc
        /** = anon
        </value>
    </property>
</bean>

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="realm" ref="myRealm"/>
</bean>

<bean id="myRealm" class="...">
    ...
</bean>

虽然比其他 Spring 示例稍微冗长,但它更易于阅读 IMO。

您还会发现使用 Shiro 的过滤器链定义可能是定义通用过滤器链和基于 Web 的安全规则的最简单方法!比在 web.xml 中定义它们要好得多。

最后,Shiro 还提供了极高的“可插拔性”。由于 Shiro 的 POJO/注入友好型架构,您将看到几乎可以配置和/或替换任何东西。Shiro 几乎将所有内容都默认为正常的默认值,您可以仅覆盖或配置您需要的内容。

归根结底,我认为选择这两者中的任何一个都更多地取决于您的心理模型-两者中的哪一个更有意义,对您来说更直观?对一些人来说是 Shiro,对另一些人来说是 Spring Security。Shiro 在 Spring 环境中工作得很好,所以我会说根据你更喜欢和对你最有意义的两个来选择。

有关 Shiro 的 Spring 集成的更多信息:http: //shiro.apache.org/spring.html

于 2011-02-14T21:25:21.097 回答
32

我没有使用 Shiro 的经验,我“部分”同意您对 Spring Security 的看法。在 Spring Security 3.x 之前,Spring Security(或 Acegi)的设置非常痛苦。一个简单的基于角色的配置至少需要 140 行神秘的 XML 配置……我知道这一点是因为我自己计算了这些行。这是您设置一次的东西,并且您祈祷它可以永远工作而无需再次触摸配置,因为您可以保证您已经忘记了所有配置的含义。:)

在 Spring Security 3.x 中,它得到了极大的改进。它引入了security命名空间,将配置从 140 行大幅缩短到约 30 行。这是我的一个项目的 Spring Security 3.x 示例:-

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:security="http://www.springframework.org/schema/security" 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">

    <security:http auto-config="true">
        <security:form-login login-page="/index.do" authentication-failure-url="/index.do?login_error=1" default-target-url="/index.do"
            always-use-default-target="true" />
        <security:logout logout-success-url="/index.do" />
        <security:intercept-url pattern="/secure/**" access="ROLE_ADMIN,ROLE_USER" />
        <security:intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    </security:http>

    <bean id="customAuthenticationProvider" class="my.project.CustomAuthenticationProviderImpl">
        ...
    </bean>

    <security:authentication-manager>
        <security:authentication-provider ref="customAuthenticationProvider" />
    </security:authentication-manager>

</beans>

Spring Security 3.x 的美妙之处在于它非常可配置,这导致了主要缺点之一:太复杂而难以理解。该文档也不容易阅读,因为我只部分熟悉 Spring Security 使用的一些术语。但是,如果您需要创建自定义配置或控制您希望安全性的精细程度,则可以使用这些选项。否则,您可以坚持使用上述 < 30 行来执行基于角色的安全检查。

我真正喜欢 Spring Security 的地方在于,一旦设置了安全性,它就会无缝地集成到项目中。就好像实际的项目代码不知道安全性的存在......这很好,因为它允许我将来轻松分离或升级安全性组件(例如:将数据库身份验证更改为 LDAP/CAS授权)。

于 2011-02-14T14:16:02.727 回答
22

我已经使用 Spring Security(3.1 版)几个月了,对它非常满意。它真的很强大,并且有一些非常好的特性,尤其是在像我以前那样手动实现所有东西之后!不过,就像我在某处读到的那样,你在应用程序开发开始时设置了一次,然后祈祷它继续工作到最后,因为如果你必须去修复它,你会可能已经忘记了您必须参数化的大部分内容。

但随后出现了一个新项目,具有更复杂的安全要求。简而言之,我们必须在几个相关的 web 应用程序之间实现某种自定义 SSO。

我确切地知道我想要在 HTTP 逻辑、cookie、会话 ID 和其他方面实现什么,以及应该以什么顺序发生什么,但是我花了一天的大部分时间在 Spring Security API 上苦苦挣扎,仍然无法弄清楚明确我应该实现或覆盖什么类或接口,以及如何将它们插入上下文。整个 API 感觉真的很复杂,有时有点深奥。虽然该文档对于一般用例甚至一些自定义非常有用,但它的深度不足以满足我的需求。

在阅读了此处和网络上其他一些地方的答案后,我的印象是 Shiro 会更容易理解并根据我的需求进行定制。所以我试了一下。

我很高兴我做到了,因为在一天的工作之后,我设法了解了足够多的 API,不仅可以在我的 Spring webapp 中轻松设置基本的身份验证和授权系统,而且还可以实现我之前的自定义 SSO 行为寻找。我只需要扩展 2 或 3 个类,整个事情在我的 spring 上下文中只需要大约 25 行 XML 配置。

所以总结一下,在易用性和学习曲线方面,Shiro 真的很讨人喜欢,我想我以后可能会用它,除非我遇到一些功能缺失或其他问题(我没有至今)。

TL;DR:两者都很强大,但 Shiro 更容易学习。

于 2013-09-10T16:54:45.080 回答