使用Spring Security内存身份验证进行内部化 [英] Internalisation using Spring Security In-Memory Authentication

查看:71
本文介绍了使用Spring Security内存身份验证进行内部化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了参考手册,查看了论坛帖子(例如

I have read the references manuals, looked at forum posts (such as, How to hold japanese characters in SPRING MVC POJO's field), and I as far as I know I have done all the things necessary for internationalisation Spring support, but I am having problems the Spring Security 3 In-Memory username matching using Unicode/special characters. Note, that my application uses UTF-8 and all appears well and all works well including entering Unicode password (however, this Unicode password may well be misleading as under the covers it may convert it to another character and this character is encoded instead?). I have a feeling that this non-matching of Unicode Usernames may be a bug with Spring Security In-Memory internalisation, but need this confirmed.

为了读者的利益,我已经完成了所有必要的基本配置,并且请注意,如果我不对用户名使用Unicode字符,那么一切都会按预期进行,否则即使输入正确的凭据,身份验证也会失败.另外,不会引发任何异常.

For the benefit of the reader, I have done all the necessary basic configurations, and please note that if I do not use Unicode character for username, everything works as expected otherwise Authentication fails even though the correct credentials are entered. Also, no exceptions are thrown.

我的摘录如下...

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

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

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-security-bean-config.xml

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

        </user-service>

请注意,如果我拥有用户名,例如isigi(没有特殊的unicode字符),则一切正常.

Note, if I have user name as, for example, isigi (without special unicode characters), then all works well.

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

In my jsp files, I have at the top line:

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

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

... and in the head section, I have...

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

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

And my xml files, UTF-8 declared at the top line, e.g.,

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

推荐答案

为了使其他可能遇到类似问题的人受益,请在网络中 encodingFilter ORDER .xml文件非常重要,它必须位于过滤器列表的顶部,否则它将不起作用.即使我已经阅读了链接,我发贴了,以某种方式错过了两次,直到深夜第三次阅读为止.我忘了在帖子中提到我已经通过在适当的位置添加URIEncoding ="UTF-8"来将server.xml文件中的Tomcat设置正确配置为UTF-8,如下所示:

For the benefit of others who may experience similar problems, the ORDER of the encodingFilter in the web.xml file is very important and it must be at the top of the filter list otherwise it will not work. Even though I have read the link that I posted, somehow I missed it twice, until I read it the third time late last night. I forgot to mention in my post that I had correctly configured Tomcat settings to UTF-8 in the server.xml file by adding URIEncoding="UTF-8" in the appropriate places as follows:

<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" />

我希望这个答案(连同问题中所述的设置)将帮助某人节省数小时甚至数夜的挫败感,以致于无法使用特殊的Unicode字符与Spring MVC/Security一起工作.

I hope this answer (together with the setup as stated in the question) will help someone save hours or even nights of frustration of getting special Unicode characters to work with Spring MVC / Security.

这篇关于使用Spring Security内存身份验证进行内部化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆