我也同意 Spring Security 感觉太复杂(对我来说)。当然,他们已经采取了一些措施来降低复杂性,例如创建自定义 XML 命名空间来减少 XML 配置的数量,但对我来说,这些并不能解决mySpring Security 的个人基本问题:它的名称和概念对我来说常常令人困惑。仅仅“得到它”是很难的。
然而,当你开始使用 Shiro 的那一刻,你就“明白了”。在安全领域中难以理解的事情变得更容易理解。 JDK 中难以使用的东西(例如密码)被简化到不仅可以忍受,而且通常使用起来很有趣的程度。
例如,如何在 Java 或 Spring Security 中对密码进行 hash+salt 加密并进行 base64 编码?两者都不如 Shiro 的解决方案那么简单直观:
ByteSource salt = new SecureRandomNumberGenerator().nextBytes();
new Sha512Hash(password, salt).toBase64();
不需要 commons-codec 或其他任何东西。只是四郎罐子。
现在就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 示例稍微冗长,但在我看来更容易阅读。
您还会发现使用 Shiro 的过滤器链定义可能是定义通用过滤器链和基于 Web 的安全规则的最简单方法!比在 web.xml 中定义它们要好得多。
最后,Shiro 还提供了极高的“可插拔性”。您会发现,由于 Shiro 的 POJO/注入友好架构,您可以配置和/或替换几乎任何内容。 Shiro 将几乎所有内容默认为合理的默认值,您可以仅覆盖或配置您需要的内容。
归根结底,我认为选择这两者中的任何一个更多的是关于你的心理模型——这两者中哪一个对你来说更有意义、更直观?对于某些人来说,它会是 Shiro,对于其他人来说,它会是 Spring Security。 Shiro 在 Spring 环境中工作得很好,所以我想说根据您更喜欢这两个环境中的哪一个来选择,并且对您来说最有意义。
有关 Shiro 的 Spring 集成的更多信息:http://shiro.apache.org/spring.html http://shiro.apache.org/spring.html