【问题标题】:Is there a bug with Spring Security In-Memory Configuration Feature?Spring Security 内存配置功能是否存在错误?
【发布时间】:2013-10-09 19:19:21
【问题描述】:

我相信我已经为我的 Spring Security 完成了所有必要的设置以使用 Unicode,但是当使用内存中身份验证时,它不适用于在用户名中包含特殊 Unicode 字符。由于 Spring 声称具有内部化支持,当使用 Unicode 字符作为用户名时,这可能是 Spring Security In-Memory 的错误吗?请放心,我已经查阅了参考手册、论坛帖子(例如How to hold japanese characters in SPRING MVC POJO's field),并且已经完成了设置 Spring 国际化所需的所有事情。但是,我在使用 Unicode/特殊字符匹配 Spring Security 3 In-Memory 用户名时遇到问题。请注意,我的应用程序、IDE 和文件使用 UTF-8,如果我 对用户名使用特殊的 Unicode 字符,一切都会正常运行。我感觉 Unicode 用户名的这种不匹配可能是 Spring Security In-Memory 内部化的一个错误,但需要确认这一点,或者如果我监督了任何事情,请告诉我?

我的sn-ps如下...

web.xml

      <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
          <param-name>forceEncoding</param-name>
          <param-value>true</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>

spring-servlet-context.xml

        <default-servlet-handler />

        <interceptors>      
            <beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                <beans:property name="paramName" value="lang" />
            </beans:bean>

            <beans:bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
                <beans:property name="paramName" value="theme" />
            </beans:bean>   

        </interceptors> 


          <resources mapping="/resources/**" location="/resources/" />


        <beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <beans:property name="basename" value="classpath:messages" />
            <beans:property name="defaultEncoding" value="UTF-8"/>
        </beans:bean>

        <beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
            <beans:property name="defaultLocale" value="en"/>
        </beans:bean>

        <!-- Theme setup --> 
        <beans:bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
                <beans:property name="basenamePrefix" value="theme-" />
        </beans:bean>

        <beans:bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
            <beans:property name="defaultThemeName" value="default" />
        </beans:bean>


        <beans:import resource="spring-security.xml"/>

         <context:component-scan base-package="com.myproject.platform" />

    </beans:beans>

spring-security.xml

      <beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />              


      <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="userService">                
            <password-encoder ref="encoder" />                      
        </authentication-provider> 
      </authentication-manager>

      <beans:import resource="user-security-bean-config.xml"/> 

user-security-bean-config.xml

        <user-service id="userService">
          <user name="ışığı" password="encodedpasswordstring" 
                         authorities="R_U,R_A"/>

        </user-service>

注意,如果我有用户名,例如 isigi(没有特殊的 unicode 字符),那么一切正常。

在我的 jsp 文件中,我在第一行有:

<%@ page language="java" session="false" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>

...在 head 部分,我有...

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

还有我的 xml 文件,在第一行声明了 UTF-8,例如,

<?xml version="1.0" encoding="UTF-8"?>

不会抛出异常或错误。

【问题讨论】:

  • 因为它是 XML,您不应该在使用特殊字符时对用户名进行编码吗?当您通过网络发布特殊字符时,这就是(或至少应该)发生的事情。
  • @M.Deinum 我已经像&lt;user name="cemış" ... 一样直接从键盘输入了Unicode,我也像&lt;user name="cem&amp;#x131;&amp;#x15F;" ... 一样直接使用了Unicode,但仍然没有成功。并且这个 xml 文件也保存为 UTF-8,并且在最上面一行还有&lt;?xml version="1.0" encoding="UTF-8"?&gt;。有什么想法吗?
  • @M.Deinum 顺便说一句,我除了为特殊字符 ış 输入十六进制代码(上)外,还尝试过放入匹配相同字符的Unicode代码,即&lt;user name="cem\u0131\u015F" ...,但仍然没有成功。我的想法已经用完了,这让我想到在使用特殊的国际 Unicode 字符时 Spring Security In-memory 是否存在错误?有什么建议么?谢谢。
  • 那么这将是一个 Java 错误,而不是 Spring Security 错误。在内存中使用HashMap 进行存储。我的赌注是从网络上提供的数据(由于某种原因可能没有正确编码)。
  • @M.Deinum 感谢您的帮助,但在尝试解决此问题 2 天后,原来是我的 web.xml 配置。

标签: spring spring-mvc spring-security


【解决方案1】:

为了其他可能遇到类似问题的人,web.xml 文件中 encodingFilterORDER 非常重要,必须在 >过滤器列表的顶部,否则它将不起作用。尽管我已经阅读了我发布的链接,但不知何故我错过了两次,直到我昨晚深夜阅读了第三次。我忘了在我的帖子中提到,通过在适当的位置添加 URIEncoding="UTF-8",我已在 server.xml 文件中正确地将 Tomcat 设置配置为 UTF-8,如下所示:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" 
       URIEncoding="UTF-8"/>


<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />

【讨论】:

    猜你喜欢
    • 2017-02-02
    • 2018-04-22
    • 2011-04-12
    • 2011-01-06
    • 2011-03-05
    • 2015-01-25
    • 2018-07-12
    • 2017-10-17
    • 1970-01-01
    相关资源
    最近更新 更多