未找到AuthenticationFailureHandler内部的Spring Mobile



我是Spring Security的开发成功和失败处理程序。

取决于设备类型,我必须显示一个html视图或发送一个json响应。为此,我使用了SpringMobile,但当我创建带有HttpServletRequest的Device对象时,找不到它。有什么想法吗?

web.xml

<filter>
    <filter-name>deviceResolverRequestFilter</filter-name>
    <filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
</filter>

ApplicationContext.xml

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
    </mvc:argument-resolvers>
</mvc:annotation-driven>

public class AuthFailureHandler implements AuthenticationFailureHandler{
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException ae) throws IOException, ServletException {
        Device device = DeviceUtils.getCurrentDevice(request);
        if(device.isNormal()){
            response.sendRedirect(response.encodeRedirectURL("./userNoAuth"));
        } else {
            response.sendRedirect(response.encodeRedirectURL("./rest/userNoAuth"));
        }
    }
}

错误

java.lang.NullPointerException at com.myapp.security.handler.AuthFailureHandler.onAuthenticationFailure(AuthFailureHandler.java:19)

更新:

我正在将.getCurrentDevice(HttpServlet请求)方法更改为getRequiredCurrentDevice(Httpservlet请求)。

现在我得到了这个错误。

java.lang.IllegalStateException: No currenet device is set in this request and one is required - have you configured a DeviceResolvingHandlerInterceptor?

验证web.xml是否包含deviceResolverRequestFilterfilter-mapping。以下是SpringMobileSamples存储库中的一个工作示例。希望能有所帮助!

https://github.com/SpringSource/spring-mobile-samples/tree/master/lite-device-resolver-xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Use the DeviceResolverRequestFilter OR the DeviceResolverHandlerInterceptor in the servlet-context.xml -->
    <filter>
        <filter-name>deviceResolverRequestFilter</filter-name>
        <filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>deviceResolverRequestFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

最新更新