我的网络应用程序中有一个 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/