gpt4 book ai didi

spring - 使用 Spring Security In-Memory Authentication 内部化

转载 作者:行者123 更新时间:2023-12-04 04:38:52 25 4
gpt4 key购买 nike

我已经阅读了引用手册,查看了论坛帖子(例如,How to hold japanese characters in SPRING MVC POJO's field),据我所知,我已经完成了国际化 Spring 支持所需的所有工作,但是我在 Spring Security 3 中遇到了问题-使用 Unicode/特殊字符匹配内存用户名。请注意,我的应用程序使用 UTF-8 并且一切看起来都很好,并且包括输入 Unicode 密码在内的一切都运行良好(但是,这个 Unicode 密码很可能具有误导性,因为它可能会在幕后将其转换为另一个字符,而该字符已被编码?) .我有一种感觉,这种不匹配的 Unicode 用户名可能是 Spring Security In-Memory 内部化的一个错误,但需要确认这一点。

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

我的片段如下...

网页.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"/>

用户安全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"%>

......在头部部分,我有......
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

和我的 xml 文件,UTF-8 声明在第一行,例如,
<?xml version="1.0" encoding="UTF-8"?>

最佳答案

为了其他可能遇到类似问题的人的利益,订购 web.xml 文件中的 encodingFilter 非常重要,它必须在 顶部 过滤器列表,否则它将无法工作。即使我已经阅读了 link我发布的,不知何故我错过了两次,直到我昨晚深夜第三次阅读它。我忘了在我的帖子中提到我已经通过在适当的位置添加 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" />

我希望这个答案(连同问题中所述的设置)将帮助某人节省数小时甚至数夜的挫折,让特殊的 Unicode 字符与 Spring MVC/Security 一起使用。

关于spring - 使用 Spring Security In-Memory Authentication 内部化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19226183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com