2013-07-23 62 views
1

我是Spring Security中的開發成功和失敗處理程序。AuthenticationFailureHandler內部的Spring Mobile未找到

取決於設備類型我必須顯示一個html視圖或發送一個json響應。爲此我使用Spring Mobile,但是當我用HtttpServletRequest創建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) 

UPDATE:

我改變.getCurrentDevice(HttpServletRequest的)方法來getRequiredCurrentDevice(HttpServletRequest的)。

現在我得到這個錯誤。

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

回答

0

確認你的web.xml文件中包含了一個deviceResolverRequestFilterfilter-mapping。以下是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> 
+0

我的web.xml這個配置已經... – Dani

相關問題