Spring Boot 多个域名指向同一IP
2024-09-30 04:53:14
一、需求:直接通过域名访问首页(同一应用下,多个首页,包括PC端、手机端首页)
方法:采用多个域名绑定同一IP下同一应用,不同域名对应不同产品(PC、手机端)的方法,在后台通过拦截器判断 request 中的host字段值,进而重定向到不同的页面
@Configuration
@EnableWebMvc
public class InterceptorConfig extends WebMvcConfigurerAdapter { @Bean
public HostInterceptor hostInterceptor() {
return new HostInterceptor();
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(hostInterceptor()).addPathPatterns("/");
super.addInterceptors(registry);
}
}
package com.zxguan.thymeleaf.interceptor; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* @author zxguan
* @description
* @create 2018-03-16 10:28
*/
public class HostInterceptor implements HandlerInterceptor { @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String host = request.getHeader("host");
if (host.equals("localhost")) {
response.sendRedirect("/home");
} else {
response.sendRedirect("/index");
}
return false;
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
}
@RequestMapping(value = {"/", "/index"})
public String test() {
return "test";
} @RequestMapping(value = "/home")
public String home() {
return "home";
}
最新文章
- [XAF] How to use the Allow/Deny permissions policy in the existing project
- 【position也可以很复杂】当弹出层遇上了鼠标定位(下)
- Python开发【第十七篇】:MySQL(一)
- SQLServer学习笔记<;>;相关子查询及复杂查询
- Jdbc 连接MySQL数据库的方法和问题
- Eclipse 快捷键 快捷输入
- dubbo服务+Spring事务+AOP动态数据源切换 出错
- AI自动寻路
- 08JS高级 ——“继承”
- JAVA入门[9]-mybatis多表关联查询
- [BZOJ]1069 最大土地面积(SCOI2007)
- ListView下拉刷新上拉加载更多实现
- python之读取配置文件模块configparser(三)高级使用---非标准配置文件解析
- [转]Laravel - Where null and Where not null eloquent query example
- stm32 启动文件 C和汇编交叉嵌入
- 使用小技巧加快IDEA的开发速度
- [HNOI2013]比赛 (用Hash实现记忆化搜索)
- SpringBoot添加对Log4j2的支持
- 【Java】通用版URLConnection 带cookie下载PDF等资源文件
- liferay项目经验之BasePortlet