com.quhaodian.web.controller.front.BaseController Maven / Gradle / Ivy
/*
+--------------------------------------------------------------------------
| Mblog [#RELEASE_VERSION#]
| ========================================
| Copyright (c) 2014, 2015 mtons. All Rights Reserved
| http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package com.quhaodian.web.controller.front;
import com.quhaodian.web.data.common.InitConfig;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
/**
* Controller 基类
*
* @author langhsu
*
*/
public class BaseController {
protected String getSuffix(String name) {
int pos = name.lastIndexOf(".");
return name.substring(pos);
}
public String toJson(Object obj) {
return new Gson().toJson(obj);
}
protected String getView(String view) {
return "/theme/"+ InitConfig.getWebConfig().getTheme()+"/" + view;
}
protected String redirect(String view) {
return "redirect:"+view;
}
protected String routeView(String route, String group) {
String format = "/default" + route;
return String.format(format, group);
}
public static String getIpAddr(HttpServletRequest request) throws Exception {
String ip = request.getHeader("X-Real-IP");
if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
return ip;
}
ip = request.getHeader("X-Forwarded-For");
if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个IP值,第一个为真实IP。
int index = ip.indexOf(',');
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
} else {
return request.getRemoteAddr();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy