我有一个实现了 Spring 安全性的项目,该项目运行顺利, View 看起来不错,除了日志消息和电子邮件消息之外,编码没有问题。
电子邮件:
在我的身份验证 Controller 上,我有一种方法可以发送电子邮件来重置密码,我在 Controller 上生成 url
final String url = contextPath + "/user/resetPassword?id=" + user.getId();
如果我使用模板,如下所示:
title = "Restablecer contraseña";
body = String.format("%s:\n" +
"\n" +
"Click on this link and you can reset your password:\n" +
"%s\n" +
user.getFirstName() + " " + user.getLastName(),
url);
String encodingOptions = "text/html; charset=UTF-8";
msg.setHeader("Content-Type", encodingOptions);
msg.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(user.getEmail()));
msg.addFrom(new InternetAddress[]{new InternetAddress(env.getProperty("support.email"))});
msg.setSubject(title, "UTF-8");
msg.setText(body, "UTF-8");
生成的url是:
http://localhost:8080/user/resetPassword?id=3D6
但正确的网址是:
http://localhost:8080/user/resetPassword?id=6
记录器:
记录器上也会发生类似的情况。
LOGGER.info("Restablecer contraseña");
通过控制台显示的消息是:
Restablecer su contraseña
关于记录器的这种情况非常非常奇怪,有时工作正常,有时工作错误。
有什么想法吗?
我的配置:
IntelliJ 14
Apache 8.0.15
带有 org.springframework.web.filter.CharacterEncodingFilter 的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"
>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>org.admin.spring</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<!-- This filter has to come before other filters. -->
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>localizationFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>localizationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
<session-config>
<session-timeout>10</session-timeout>
<cookie-config>
<name>AUTHSESSION</name>
<http-only>true</http-only>
</cookie-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
</web-app>
在 apache server.xml 配置中,我已将 2 个连接器更改为 URIEncoding="UTF-8"
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />
Pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
WebMvcConfigurerAdapter 类:
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setUseCodeAsDefaultMessage(true);
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(0);
return messageSource;
}
最佳答案
控制台中的问题很常见...您用来查看日志的控制台或应用程序不支持 UTF-8(我敢打赌您正在 Windows 上运行您的应用程序)。
电子邮件的问题看起来很奇怪,因为 3D
是 =
符号的八进制表示形式。
编辑
除了生成电子邮件的实际代码之外,我没有想到您生成的电子邮件中可能有什么问题(因为我不认为您的电子邮件提供商或电子邮件客户端会更改网址)。配置 JavaMail 时,设置属性 mail.debug
to true
获取有关您的应用程序发送到电子邮件服务器的内容的更多信息。我很确定你会发现你正在发送奇怪的字符
此外,尝试从生成 url 字符串到 JavaMail 发送电子邮件的那一刻逐行调试代码(进入每个方法),以查明添加这些字符的确切位置。
关于java - Spring security + mvc特殊字符编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31974089/