我有一个实现了Spring安全性的项目,项目运行顺畅,视图看起来没问题,编码没有问题,除了记录消息和电子邮件消息。
电子邮件:
在我的auth控制器上我有一种发送电子邮件重置密码的方法,我在控制器上生成网址
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");
生成的网址是:
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
web.xml with org.springframework.web.filter.CharacterEncodingFilter
<?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配置中,我用URIEncoding =“UTF-8”更改了2个连接器
<!-- 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;
}
答案 0 :(得分:1)
控制台中的问题非常普遍......您用来查看日志的控制台或应用程序不支持UTF-8(我敢打赌您正在运行您的应用程序视窗)。
电子邮件的问题看起来很奇怪,因为3D
是=
符号的八进制表示。
除了生成电子邮件的实际代码之外,我不会想到您生成的电子邮件中可能出现的错误(因为我不认为您的电子邮件提供商或电子邮件客户端会更改网址)。配置JavaMail时,请设置属性mail.debug
to true
以获取有关您的应用程序发送到电子邮件服务器的更多信息。我很确定你会发现你发送了这些奇怪的字符
此外,尝试从生成url字符串的那一刻开始逐行调试代码(进入每个方法),直到JavaMail发送电子邮件以确定添加这些字符的确切位置为止。