【问题标题】:How can I use boolean parameters from application.properties in spring security context configuration xml file?如何在 spring 安全上下文配置 xml 文件中使用 application.properties 中的布尔参数?
【发布时间】:2013-02-02 23:21:45
【问题描述】:

我正在尝试在我的spring-security configuration xml 文件中使用我的application.properties 中的boolean 参数。 我不知道为什么我可以使用非布尔参数,但我得到一个布尔错误。

如何使用布尔参数?

这是我的 application.properties:

JDBC_CONNECTION_STRING=jdbc:mysql://localhost:3306/schema?user=username&password=password
protocol=http
USE_SECURE=false

我的 spring-security.xml 是:

< remember-me user-service-ref="internalUserDetails" data-source-ref="dataSource" key="this-is-my-key02203452416fw" use-secure-cookie="${USE_SECURE}" />

... 但我收到此错误: cvc-datatype-valid.1.2.1:“${USE_SECURE}”不是“布尔”的有效值

我也尝试设置USE_SECURE=False,但我再次遇到同样的错误。 如何在 spring 安全配置 xml 文件中使用布尔参数?

这是我的 web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0" >

    <display-name> Name-MyApp</display-name> 

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- Servlets -->
    <servlet>
        <servlet-name>MyApp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Servlets Mappings -->
    <servlet-mapping>
        <servlet-name>MyApp</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
                /WEB-INF/servlet-context.xml,
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>

    <!-- Filters -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>httpMethodFilter</filter-name>
        <servlet-name>MyApp</servlet-name>
    </filter-mapping>    

    <filter> 
        <filter-name>httpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter> 

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app> 

【问题讨论】:

  • 你有PropertyPlaceholderConfigurer confifured 吗?
  • 我认为我不需要PropertyPlaceholderConfigurer,同一个文件中的其他属性都可以正常工作,PropertyPlaceholderConfigurer 如何帮助我获取布尔值?
  • 如何将application.properties 文件加载到spring?
  • @ArunPJohny &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /&gt; &lt;property name="location" value="classpath:application.properties" /&gt; &lt;/bean&gt;属性文件中的其他值都加载好了,可能@zagyi是对的,稍后我会检查一下
  • 它是一个 Web 应用程序吗?您在哪个文件中定义了 PropertyPlaceholderConfig‌ bean?

标签: spring web-applications spring-mvc spring-security application-server


【解决方案1】:

看起来是传递了键“${USE_SECURE}”的值。当我想启动布尔值时遇到了类似的问题

<bean id="flag" class="java.lang.Boolean">
    <constructor-arg value="${FLAG}"/>
</bean>

它适用于“属性”,所以我以其他方式解决了我的情况。不知道是不是spring bug?

【讨论】:

  • 我如何设置use-secure-cookie 值?像这样use-secure-cookie=flag?
  • 我认为 Spring 足够聪明,它知道如果 setter 需要一个布尔值,并且您输入“true”,它会使用适当的 PropertyEditor 对其进行转换。至少这是我过去的经验。
  • @CodeChimp,我在上面写过use-secure-cookie=flaguse-secure-cookie="true" 吗?我的记忆力很短
    use-secure-cookie="true"正是我想要避免的,请阅读上面的帖子
  • 首先,您的回复没有必要让人讨厌。你正在创造财产,是吗?您正在将该属性值设置为“true”或“false”,是吗?然后,根据我的经验,Spring 将使用 PropertyEditor 将其转换为 setter 期望的布尔类型。这就是该框架存在的原因。价值来自 EL 是否重要?我不会这么认为。我认为 Spring 的人,他们看起来很聪明,认为如果你允许某人使用 EL 来提取属性值,他们仍然会使用相同的 PropertyEditor 机制。
  • 此外,如果我对“我正在尝试使用 application.properties 中的布尔参数...”的假设不正确,并且您将其设置为“true”或“ false”,然后编写一个自定义的 PropertyEditor 来处理您期望的值。同样,这就是为什么他们采用这种机制的原因是他们看到了基于文本输入注入复杂类型的必要性。
【解决方案2】:

安全命名空间的 xsd 模式定义只允许在 use-secure-cookie 属性中使用布尔值。如果您没有指定允许的文字之一(“true”或“false”),您的 xml 将不会通过模式验证,甚至不会被解析。

因此,如果您使用安全命名空间配置,您将无法使用外部属性来设置此值。为了证明我的观点,这里是来自RememberMeBeanDefinitionParser.parse()的相关代码sn-p:

String useSecureCookie = element.getAttribute("use-secure-cookie");
if (StringUtils.hasText(useSecureCookie)) {
    services.getPropertyValues().addPropertyValue(
                    "useSecureCookie", Boolean.valueOf(useSecureCookie));
}

如您所见,该属性立即转换为布尔值,因此没有任何机制可以进一步处理该值。

我不完全确定,但很可能通过简单地放宽 xsd 以允许任何字符串值并将该值传递给 bean 定义(上面的services)而不将其转换为布尔值来解决此问题。然后 PropertyPlaceholderConfigurer 可以稍后解析给定的值,如果它恰好是一个属性占位符。

如果您想尝试一下,请随时在Spring Security issue tracker 中开票。

【讨论】:

    猜你喜欢
    • 2016-04-04
    • 2013-09-08
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2016-08-10
    • 2016-06-12
    • 2014-12-16
    相关资源
    最近更新 更多