内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
我评估了基于Java的安全框架,我是Spring 3.0的用户,因此SpringSecurity似乎是正确的选择,但是SpringSecurity似乎受到了过度复杂的影响,它并不能使安全性更易于实现,Shiro似乎更连贯,更易于理解。这两个框架的优劣如何?
我也觉得Spring的安全太复杂了(对我来说)。当然,他们做了一些事情来降低复杂性,比如创建自定义XML名称空间来减少XML配置的数量,但对我来说,这些并不能解决我的SpringSecurity的基本问题:它的名称和概念通常会让我感到头疼。
一旦你开始使用Shiro,你就会明白了。在安全世界里很难理解的事情就是更容易理解。在JDK中难以使用的东西(例如密码)被简化到一个不仅可以忍受。
例如,如何在Java或SpringSecurity中对密码进行散列+加草,并对base 64进行编码?这两种方法都没有Shiro的解决方案那么简单和直观:
ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); new Sha512Hash(password, salt).toBase64();
不需要共用编解码器或其他任何东西。只有一句。
考虑这个线程中另一篇文章中的SpringXML配置示例。下面是在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>
所以shiro真很好玩
我没有使用Shiro的经验,但我同意一部分你所说的Spring安全性。在SpringSecurity3.x之前,SpringSecurity的设置非常痛苦。一个简单的配置至要敲140行。)
但SpringSecurity3.x,它得到了极大的改进。将配置从140行大幅度缩短到30行。以下是我的一个的SpringSecurity3.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>
然后隔壁的shiro几行就完成了