配置Spring Security 4,以便&书信电报;第二节:授权url=''/&燃气轮机&引用;标记可以在XML中使用

配置Spring Security 4,以便&书信电报;第二节:授权url=''/&燃气轮机&引用;标记可以在XML中使用,spring,spring-security,Spring,Spring Security,我有一个工作的Java8Web应用程序,它使用SpringFramework4.2和SpringSecurity4.0.2通过JasigCAS(中央身份验证服务)处理身份验证,在我们的Active Directory服务器上使用LDAP 目前,我使用“Authorize”JSP标记库()如下 <sec:authorize var="allowRename" access="hasRole('ROLE_TEST_RENAME')" /> <input id="allowRenam

我有一个工作的Java8Web应用程序,它使用SpringFramework4.2和SpringSecurity4.0.2通过JasigCAS(中央身份验证服务)处理身份验证,在我们的Active Directory服务器上使用LDAP

目前,我使用“Authorize”JSP标记库()如下

<sec:authorize var="allowRename" access="hasRole('ROLE_TEST_RENAME')" />
<input id="allowRename" type="hidden" value="${allowRename}" />
<sec:authorize var="allowRename" url="/rename.json" />
<input id="allowRename" type="hidden" value="${allowRename}" />

此方法的缺点是在视图中直接引用Active Directory安全组。我宁愿不这样做。相反,我希望改变它,使它看起来像这样

<sec:authorize var="allowRename" access="hasRole('ROLE_TEST_RENAME')" />
<input id="allowRename" type="hidden" value="${allowRename}" />
<sec:authorize var="allowRename" url="/rename.json" />
<input id="allowRename" type="hidden" value="${allowRename}" />

通过这种方式,我可以在一个地方配置访问—在spring安全配置中

根据文档,要在authorize标记上使用url属性,必须在我的应用程序上下文中有一个webinvocationprivilegeeevaluator实例。当我使用安全名称空间时,它不是xml配置文件中的基本名称空间-因此我知道我必须手动声明DefaultWebInvocationPrivilegeEvaluator类的实例。然而,该类的构造函数只接受一个AbstractSecurityInterceptor类型的参数,就我所见,我没有对任何参数的引用

这是我的配置(已编辑,自然),有人知道如何修改它,以便我可以使用对FilterSecurityInterceptor的单个构造函数参数引用来声明DefaultWebInvocationPrivilegeEvaluator bean吗

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd">

    <security:http entry-point-ref="casEntryPoint">
        <security:intercept-url pattern="/rename.json" method="POST" 
            access="hasRole('ROLE_TEST_RENAME')" />
        <security:intercept-url pattern="/delete.json" method="POST" 
            access="hasRole('ROLE_TEST_DELETE')" />
        <security:intercept-url pattern="/restore.json" method="POST" 
            access="hasRole('ROLE_TEST_DELETE')" />
        <security:intercept-url pattern="/**" 
            access="hasRole('ROLE_TEST_USERS')" />

        <security:custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER" />
        <security:custom-filter ref="singleLogoutFilter" before="CAS_FILTER" />
        <security:custom-filter ref="casFilter" position="CAS_FILTER" />

        <security:logout logout-success-url="${cas.server.rootUrl}/logout" 
            invalidate-session="true" />
    </security:http>

    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider ref="casAuthenticationProvider" />
    </security:authentication-manager>

    <bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
        <constructor-arg value="ldap://ldap.test.com:389/dc=test,dc=com"/>
        <property name="url" value="ldap://ldap.test.com:389/" />
        <property name="base" value="dc=test,dc=com" />
        <property name="pooled" value="true" />
        <property name="userDn" value="${ad.userDn}" />
        <property name="password" value="${ad.password}" />
    </bean>

    <bean id="userSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
        <constructor-arg name="searchBase" value="ou=User Accounts,ou=TEST" />
        <constructor-arg name="searchFilter" value="(sAMAccountName={0})" />
        <constructor-arg ref="contextSource" />
    </bean>

    <bean id="authoritiesPopulator" class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
        <constructor-arg ref="contextSource" />
        <constructor-arg value="ou=Group Accounts,ou=TEST" />
        <property name="groupRoleAttribute" value="cn" />
        <property name="searchSubtree" value="true" />
        <property name="rolePrefix" value="ROLE_" />
        <property name="defaultRole" value="ROLE_USER" />
        <property name="convertToUpperCase" value="true" />
    </bean>

    <bean id="userDetailsMapper" class="com.test.security.auth.ActiveDirectoryUserDetailsMapper" />

    <bean id="ldapUserDetailsService" class="org.springframework.security.ldap.userdetails.LdapUserDetailsService">
        <constructor-arg ref="userSearch" />
        <constructor-arg ref="authoritiesPopulator" />
        <property name="userDetailsMapper" ref="userDetailsMapper" />
    </bean>

    <!-- handles a Single Logout Request from the CAS Server -->
    <bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter">
        <property name="casServerUrlPrefix" value="${cas.server.secureUrl}" />
    </bean>

    <!-- redirects to the CAS Server to signal Single Logout should be performed -->
    <bean id="requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
        <constructor-arg value="${cas.client.contextRootUrl}/logout" />
        <constructor-arg>
            <bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
        </constructor-arg>
        <property name="filterProcessesUrl" value="/logout/cas" />
    </bean>

    <bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
        <property name="service" value="${cas.client.contextRootUrl}/login/cas" />
        <property name="authenticateAllArtifacts" value="true" />
        <property name="sendRenew" value="false" />
    </bean>

    <bean id="casEntryPoint" class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">
        <property name="loginUrl" value="${cas.server.rootUrl}/login" />
        <property name="serviceProperties" ref="serviceProperties" />
    </bean>

    <bean id="casFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">
        <property name="authenticationManager" ref="authenticationManager" />
        <property name="serviceProperties" ref="serviceProperties" />
    </bean>

    <bean id="casAuthenticationProvider" class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
        <property name="authenticationUserDetailsService">
            <bean class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
                <constructor-arg ref="ldapUserDetailsService" />
            </bean>
        </property>
        <property name="serviceProperties" ref="serviceProperties" />
        <property name="ticketValidator">
            <bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
                <constructor-arg index="0" value="${cas.server.rootUrl}" />
            </bean>
        </property>
        <property name="key" value="test-application" />
    </bean>

</beans>

我只是想澄清一下,每当我使用我在第一篇文章中描述的authorize标记时,它总是解析为
true
。问题是,在某些情况下,根据我以谁的身份登录,它应该已解决为
false

我刚刚弄明白了原因,现在它正常工作了

基本上,问题就在这里

<sec:authorize var="allowRename" url="/rename.json" />

感谢大家的帮助。

为什么要在Spring 4中使用XML配置?@Vaelyr这是一个新库的实验(Spring 3.0.2到4.2.0和Spring security 3.0.2到4.0.2)。稍后我将尝试java配置,但我希望旧库和新库之间的XML配置有明确的等价物。@Jazz我不确定“它不是XML配置文件中的基本命名空间-因此我理解我必须手动声明DefaultWebInvocationPrivilegeEvaluator类的实例”是什么意思。如果您声明元素(您这样做),那么将为您创建DefaultWebInvocationPrivilegeEvaluator。您在尝试使用JSP标记库时是否遇到特定问题?另外,您的web.xml看起来像什么?@RobWinch根据基本名称空间,我的意思是beans-tags-xmlns属性设置为bean模式,而不是安全模式。当我无法让authorizeurl标记工作时,我认为这就是原因。我刚刚成功地让authorize url标记工作了-我将立即发布一个解释。@您是否计划在Spring安全参考中介绍Java配置设置?我遇到了完全相同的问题。但是使用Java配置。它总是会变成真的,这是错误的。请看我的问题: