一、前言
随着为服务越来越流行,项目越来越多,多项目之间全局处理的需求也随之增加,比如统一登录,统一语言切换等场景,接下来我就讲一讲统一语言切换的问题。
例如https://a.daily.yunu-inc.org/,https://b.daily.yunu-inc.org项目之间有个共同的特点 ,他们都属于 ***.yunu-inc.org***这个域下,那么就为统一语言切换提供可能,具体的思路是:
切换语言时把cookie信息放在 .yunu-inc.org 这个根域下,那么只要是这个根域下的项目都可以访问到这个域。
二、代码实现:
public enum LanguageEnum {
LANGUAGE_EN("en","英文"),
LANGUAGE_CN("cn","中文");
private String code;
private String name;
LanguageEnum(String code, String name) {
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
package org.yungu.app.common.util;
import static org.yungu.app.common.enums.LanguageEnum.LANGUAGE_CN;
import static org.yungu.app.common.enums.LanguageEnum.LANGUAGE_EN;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.yungu.app.common.enums.LanguageEnum;
import org.yungu.app.util.LanguageConstant;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author lm
* @ClassName: CookieUtils
* @Description: (这里用一句话描述这个类的作用)
* @date 2018/1/7 13:49
*/
public class CookieUtils {
private static final String LANGUAGE_COOKIE_NAME = "cookie-language";
private static final Pattern PATTERN = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
public static void addCookieToResponse(HttpServletResponse response, Cookie cookie) {
response.addCookie(cookie);
}
public static Cookie getCookieByNameFromRequest(HttpServletRequest request, String cookieName) {
if (request == null || cookieName == null) {
return null;
}
Cookie[] cookies = request.getCookies();
if (cookies == null || cookies.length == 0) {
return null;
}
for (Cookie cookie : cookies) {
if (cookie == null) {
continue;
}
String name = cookie.getName();
if (cookieName.equals(name)) {
return cookie;
}
}
return null;
}
public static Cookie createCookie(String name, String value, HttpServletRequest request) {
Cookie cookie = new Cookie(name, value);
cookie.setDomain(getDomainForUrl(request.getServerName()));
return cookie;
}
/**
* 根据URL获取domain
*
* @param url
* @return
*/
public static String getDomainForUrl(String url) {
String domainUrl = null;
if (url == null) {
return null;
} else {
Matcher matcher = PATTERN.matcher(url);
while (matcher.find()) {
domainUrl = matcher.group();
}
return domainUrl;
}
}
public static String getLanguageFromCookie(HttpServletRequest request) {
Cookie languageCookie = getCookieByNameFromRequest(request, LANGUAGE_COOKIE_NAME);
if (languageCookie == null) {
String language = request.getLocale().getLanguage();
String en = LanguageConstant.EN;
if (language.contains(en)) {
return en;
}
return LanguageConstant.CN;
}
String language = languageCookie.getValue();
if (StringUtils.isBlank(language)) {
return LanguageConstant.CN;
}
return language;
}
/**
* 放在根域下
*
* @param request
* @param response
* @param language
*/
public static void setLanguage(HttpServletRequest request, HttpServletResponse response, LanguageEnum language) {
Cookie cookie = getCookieByNameFromRequest(request, LANGUAGE_COOKIE_NAME);
if (cookie == null) {
cookie = createCookie(LANGUAGE_COOKIE_NAME, language.getCode(), request);
}
cookie.setValue(language.getCode());
cookie.setPath("/");
cookie.setDomain(getDomainForUrl(request.getServerName()));
response.addCookie(cookie);
}
}