首页 > 解决方案 > 在 Spring Security 之前使用 Spring Mobile 设备检测

问题描述

我想在登录后将用户重定向到不同的页面,具体取决于他们是什么类型的用户,以及他们是否在移动设备上。我的项目使用 Spring MVC 和 Spring Security。为了使重定向逻辑正常工作,我使用 Spring Security AuthenticationSuccessHandler 来识别用户登录后的类型并将他们引导到正确的页面。

我希望此时使用 Spring Mobile 检查它们是否在移动设备上,如果是,则将它们发送到 URL 的移动版本。但是,似乎 DeviceResolverHandlerInterceptor 之类的拦截器在 Spring Security 过滤器之后执行,因此在我需要它的时候还没有解决。

目前,我能看到的唯一解决方案是禁用拦截器,而是直接使用 LiteDeviceResolver 的 resolveDevice() 方法。这感觉有点hacky,但我看不到另一个明显的选择。

有没有更好的方法来配置这个人们都知道?

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {
        return new DeviceResolverHandlerInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(deviceResolverHandlerInterceptor()).order(Ordered.HIGHEST_PRECEDENCE);

    }

}



    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {

        boolean isMobile = DeviceUtils.getCurrentDevice(request) != null
                && DeviceUtils.getCurrentDevice(request).isMobile();

...

标签: javaspring-bootspring-mvcspring-mobile

解决方案


推荐阅读