java - Spring消息包问题

标签 java spring spring-mvc spring-security

我的网络应用程序中有一个 spring-security 设置。我想用我的自定义消息替换 Spring Security 的一些消息,即

而不是 Bad Credentials我要AbstractUserDetailsAuthenticationProvider.badCredentials有值(value)invalid username or password, please try again

这是我的 Spring 设置:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages"/>
<!-- Tried this as well <property name="basename" value="/WEB-INF/messages/messages.properties"/> -->
<property name="cacheSeconds" value="0" />
<property name="defaultEncoding" value="UTF-8" />
</bean>

我已经在 WEB-INF 中创建了文件夹叫messages 。该文件夹内有一个名为 messages.properties 的文件。该文件内有一行:

AbstractUserDetailsAuthenticationProvider.badCredentials=invalid username or password, please try again

我在这里做错了什么?

更新:

此 servlet-context 在 Web xml 中定义:

<!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

问题已解决,将 messageSource bean 定义移至另一个上下文。

最佳答案

一切看起来都不错。尝试使用 spring 消息标记获取消息,以检查问题是否出在您的配置或安全消息上:

<spring:message code="AbstractUserDetailsAuthenticationProvider.badCredentials" />

如果这确实有效,则问题不在于您的配置(对我来说看起来不错)。

无论如何,尝试将消息文件放入类路径(源文件夹)中:

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

此外,检查该 bean 是否在 Spring 识别的应用程序上下文文件中声明。

关于java - Spring消息包问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11329080/

相关文章:

spring - 在 SpringBoot 中配置 RequestContextListener

java JTable - 单击单元格时选择整行

java - JSF + JPA 没有 EJB 或 Spring?

java - 一起使用 Spring Boot、QueryDSL 和 Springfox Swagger - Guava 版本不匹配

java - 如何在 Spring Controller 中包含使用 JsonFilter 过滤的属性?

java - Spring 3.1.2 MVC @ExceptionHandler 和 @ResponseBody

java - 在字符串中生成字符组合并不完全有效,为什么?

java - Blender 3D 渲染 Java servlet 接口(interface)

java - 如何在 Java 中使我的 JSON 更好?严格使用 - org.json.JSON

具有动态位置路径的 Spring PropertyPlaceholderConfigurer