mirror of https://github.com/halo-dev/halo
parent
b7b11e2c8f
commit
8f36a5d124
|
@ -69,7 +69,7 @@ public class MainController {
|
|||
public void avatar(HttpServletResponse response) throws IOException {
|
||||
User user = userService.getCurrentUser().orElseThrow(() -> new ServiceException("未查询到博主信息"));
|
||||
if (StringUtils.isNotEmpty(user.getAvatar())) {
|
||||
response.sendRedirect(user.getAvatar());
|
||||
response.sendRedirect(HaloUtils.normalizeUrl(user.getAvatar()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,7 +77,7 @@ public class MainController {
|
|||
public void logo(HttpServletResponse response) throws IOException {
|
||||
String blogLogo = optionService.getByProperty(BlogProperties.BLOG_LOGO).orElse("").toString();
|
||||
if (StringUtils.isNotEmpty(blogLogo)) {
|
||||
response.sendRedirect(blogLogo);
|
||||
response.sendRedirect(HaloUtils.normalizeUrl(blogLogo));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ public class MainController {
|
|||
public void favicon(HttpServletResponse response) throws IOException {
|
||||
String favicon = optionService.getByProperty(BlogProperties.BLOG_FAVICON).orElse("").toString();
|
||||
if (StringUtils.isNotEmpty(favicon)) {
|
||||
response.sendRedirect(favicon);
|
||||
response.sendRedirect(HaloUtils.normalizeUrl(favicon));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package run.halo.app.utils;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.lang.NonNull;
|
||||
|
@ -232,6 +233,24 @@ public class HaloUtils {
|
|||
return String.valueOf(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize url
|
||||
*
|
||||
* @param originalUrl original url
|
||||
* @return normalized url.
|
||||
*/
|
||||
@NonNull
|
||||
public static String normalizeUrl(@NonNull String originalUrl) {
|
||||
Assert.hasText(originalUrl, "Original Url must not be blank");
|
||||
|
||||
if (StringUtils.startsWithAny(originalUrl, "/", "https://", "http://")
|
||||
&& !StringUtils.startsWith(originalUrl, "//")) {
|
||||
return originalUrl;
|
||||
}
|
||||
|
||||
return URLUtil.normalize(originalUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets machine IP address.
|
||||
*
|
||||
|
|
|
@ -143,4 +143,15 @@ public class HaloUtilsTest {
|
|||
url = HaloUtils.compositeHttpUrl("https://halo.run/", "/path1/", "/path2/");
|
||||
assertEquals("https://halo.run/path1/path2", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void normalizeUrl() {
|
||||
assertEquals("/2019/2/2/avatar.jpg", HaloUtils.normalizeUrl("/2019/2/2/avatar.jpg"));
|
||||
|
||||
assertEquals("http://cn.gravatar.com/avatar?d=mm", HaloUtils.normalizeUrl("//cn.gravatar.com/avatar?d=mm"));
|
||||
|
||||
assertEquals("http://cn.gravatar.com/avatar?d=mm", HaloUtils.normalizeUrl("cn.gravatar.com/avatar?d=mm"));
|
||||
|
||||
assertEquals("https://cn.gravatar.com/avatar?d=mm", HaloUtils.normalizeUrl("https://cn.gravatar.com/avatar?d=mm"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue