在 Spring 中使用自定义处理程序拦截器将用户重定向到基于设备的移动网站的最佳方法是什么

发布于 2024-12-20 13:44:57 字数 528 浏览 6 评论 0原文

目前,我们正在开发一个项目,该项目将有桌面版和移动版网站,在 Spring 中创建。我们希望移动版和桌面版共享相同的 Spring 安全系统等。

我们使用 Spring mobile 来检测设备是否是移动的,并且可能会使用 Spring 站点首选项参数。

我们不想使用 spring 移动重定向器,因为这似乎会重定向到另一个域(例如 m.website.com),并且不会轻易使用相同的 spring security。

所以我想使用 HandlerInterceptor 重定向到站点内的特定目录 - 例如“/jsp/mobile/”。

1)在此扩展的HandlerInterceptor中是否可以在preHandle方法中使用Spring Mobile设备检测器?

2)重定向的最佳方式是什么?是 a) 仅使用 http 响应,还是 b) 以某种方式更改 viewResolver 并将前缀从“/jsp/”更改为“/jsp/mobile/”

抱歉,这个冗长的问题,但我希望将其作为尽可能多地了解我们正在尝试做的事情!

干杯

Right now we are working on a project that will have a desktop and a mobile version of the site, created in Spring. We want the mobile and the desktop version to share the same Spring security system etc.

We are using Spring mobile to detect whether or not the device is mobile, and may possibly use the Spring site preference parameter.

We dont want to use the spring mobile redirector, as this appears to redirect to another domain (eg m.website.com), and would not use the same spring security easily.

So I am thinkin gof using a HandlerInterceptor to redirect to a specfic directory within the site - eg "/jsp/mobile/".

1) Is it possible in this extended HandlerInterceptor to use the Spring Mobile device detector in the preHandle method?

2) What is the best way to redirect? Is it a) to just use the http response, or b) to somehow change the viewResolver and change the prefix from "/jsp/" to "/jsp/mobile/"

Sorry for the long winded question, but I hoped to put as much info in as possible about what we are trying to do!

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沦落红尘 2024-12-27 13:44:57

我目前在预处理方法中使用 HandlerInterceptor 来评估应用程序是否是移动设备,如果来自移动设备,则通过方法 requestUri 获取当前地址并在名称后面添加前缀“m”应用程序上下文。然后进行重定向:

response.sendRedirect("/application/m/.....") ( 文档 )

示例

/application/user           //recived
/application/m/user         //sent

问题在于该拦截器会拦截所有请求,因此有必要评估以下内容:

  1. 如果请求带有类似“/application/m”的路径/.....”不验证来自移动设备,因为“m”并说它对我有用

  2. 如果你让spring处理静态资源,如果请求来自移动设备(移动版本和普通版本共享这些资源),拦截器不得重定向

I am currently using a HandlerInterceptor in the pre handler method makes the assessment of whether or not the application is a mobile device, if coming from a mobile device, through the method requestUri get current address and places as prefix "m" after the name of the application context. Then do a redirect:

response.sendRedirect("/application/m/.....") ( documentation )

Example

/application/user           //recived
/application/m/user         //sent

The problem is that this interceptor intercepts all requests, therefore it is necessary to assess the following:

  1. if the request comes with a path like "/application/m/....." not verify that comes from a mobile device, because the "m" and says it does me

  2. if you let spring handle static resources, the interceptor must not redirect if the requests come from a mobile device (the mobile version and the normal share these resources)

情场扛把子 2024-12-27 13:44:57

感谢您的回答。我所做的是使用 HandlerInterceptor 并在 post 句柄方法中更改视图名称,如下所示:

if (modelAndView.getViewName() != null)
{
    modelAndView.setViewName("/mobile/" + modelAndView.getViewName());
}
else
{
    modelAndView.setViewName("/mobile/");
}

另外,无论设备是否移动,都将其包装在 if else 中,方法是将 DeviceResolver 注入构造函数,然后解析本地设备。

Thanks for the answer. What I did is use a HandlerInterceptor and changed the view name in the post handle method like so:

if (modelAndView.getViewName() != null)
{
    modelAndView.setViewName("/mobile/" + modelAndView.getViewName());
}
else
{
    modelAndView.setViewName("/mobile/");
}

Plus that is wrapped in an if else agains whether the device is mobile or not, by injecting DeviceResolver into the constructor and then resolving the local device.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文