1

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

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

web.xml

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

应用程序上下文.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(HttpServletRequest) 方法更改为 getRequiredCurrentDevice(HttpServletRequest)。

现在我得到这个错误。

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

1 回答 1

0

验证您的web.xml 是否包含filter-mapping. deviceResolverRequestFilter以下是 Spring Mobile Samples 存储库中的一个工作示例。希望有帮助!

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>
于 2013-07-24T03:29:58.730 回答