未找到AuthenticationFailureHandler内的Spring Mobile

时间:2013-07-23 07:46:11

标签: spring spring-security spring-mobile

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

取决于设备类型我必须显示一个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)

更新

我将.getCurrentDevice(HttpServletRequest)方法改为getRequiredCurrentDevice(HttpServletRequest)。

现在我收到了这个错误。

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

1 个答案:

答案 0 :(得分:0)

验证您的web.xml包含filter-mapping的{​​{1}}。以下是Spring Mobile Samples存储库中的工作示例。希望有所帮助!

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

deviceResolverRequestFilter