2014-07-26 56 views
3

我正在開發一個Cordova應用程序。Cordova POST - 請求禁止403.未到達調度程序Servlet

當我從我的物理設備(而不是仿真器)上運行的Cordova應用程序提交$ .ajax POST請求時,我收到狀態碼403禁止。

我可以從設備發出GET請求沒有問題。我也可以使用POST登錄(接收302 Found Response)。

來自Chrome的請求處理完美。

我正在使用Spring/Tomcat。我在我的tomcat web.xml中添加了CORS過濾器,並且在Cordova的config.xml中添加了allow-originins *。

以下是當我發佈POST請求時產生的日誌摘要,首先從Chrome瀏覽器,其次從我的設備。

鉻請求:

org.springframework.security.web.FilterChainProxy:/提交檢查以在附加的過濾器鏈11 1的位置; org.springframework.security.web.FilterChainProxy:/提交 - 檢查在11的位置2在另外的過濾器鏈中;發射過濾器:'SecurityContextPersistenceFilter' org.springframework.security.web.context.HttpSessionSecurityContextRepository:目前沒有HttpSession org.springframework.security.web.context.HttpSessionSecurityContextRepository:沒有SecurityContext從HttpSession中可用:null。一個新的將被創建。 org.springframework.security.web.FilterChainProxy:/提交檢查在11的位置3在另外的過濾器鏈中;發送過濾器:'HeaderWriterFilter' org.springframework.security.web.header.writers.HstsHeaderWriter:由於它與requestMatcher不匹配,因此未注入HSTS標頭org.springframework.se[email protected]461e0eb8 org.springframework.security.web.FilterChainProxy:/ submit-check在11位的第4位添加過濾鏈; org.springframework.security.web.util.matcher.AntPathRequestMatcher:檢查請求匹配:'/ submit-check';反對'/ j_spring_security_logout' org.springframework.security.web.FilterChainProxy:/ submit-check在11位的第5位附加的過濾器鏈中;點擊過濾:'UsernamePasswordAuthenticationFilter' org.springframework.security.web.util.matcher.AntPathRequestMatcher:檢查請求匹配:'/ submit-check';反對'/ j_spring_security_check' org.springframework.security.web.FilterChainProxy:/ submit-check在11位的第6位添加過濾器鏈; org.springframework.security.web.FilterChainProxy:/ submit-check在11位的第7位,在另外的過濾器鏈中;過濾器:'RequestCacheAwareFilter' org.springframework.security.web.FilterChainProxy: org.springframework.security.web.FilterChainProxy:/提交 - 檢查11位的第8位在額外的過濾器鏈中; org.springframework.security.web.authentication.AnonymousAuthenticationFilter:使用匿名標記填充SecurityContextHolder:'org.sprin[email protected]9055c2bc:Principal:anonymousUser;證書:[PROTECTED];已驗證:true;詳細信息:org.sprin[email protected]b364:RemoteIpAddress:0:0:0:0:0:0:0:1; SessionId:null;授予權限:ROLE_ANONYMOUS' org.springframework.security.web.FilterChainProxy:/ submit-check在11位的第9位的附加過濾器鏈中;發射過濾器:'SessionManagementFilter' org.springframework.security.web.session。SessionManagementFilter:請求的會話ID 2BB345F22D731DB9A10B0BB65950502D無效。 org.springframework.security.web.FilterChainProxy:/提交檢查以在附加的過濾器鏈11的位置10;燒製過濾器:「的ExceptionTranslationFilter」 org.springframework.security.web.FilterChainProxy:/提交檢查以在附加的過濾器鏈11的位置11;發射器:「FilterSecurityInterceptor」 org.springframework.security.web.util.matcher.AntPathRequestMatcher:請求檢查匹配:「/提交檢查」;反對「/**.html」 org.springframework.security.web.access.intercept.FilterSecurityInterceptor:公共對象 - 沒有試圖 org.springframework.security.web.FilterChainProxy認證:/提交檢查達到了額外的過濾器鏈的末端;處理原始鏈 org.springframework.web.servlet.DispatcherServlet:DispatcherServlet,名稱爲'dispatcher'處理POST請求[/ ab/submit-check] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping:仰望處理方法路徑/提交檢查 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping:返回處理方法[公共org.springframework.web.servlet.ModelAndView com.gm.ab.controller.MobileNavigation .save(java.lang.String)] org.springframework.beans.factory.support.DefaultListableBeanFactory:返回單例bean的緩存實例'mobileNavigation' org.springframework.web.servlet.DispatcherServlet:Null ModelAndView以名稱返回到DispatcherServlet 'dispatcher':假設HandlerAdapter完成請求處理 org.springframework.web.servlet.DispatcherServlet:成功完成請求 org.springframework.security.web.access.ExceptionTranslationFilter:鏈正常處理 org.springframework.security.web.context.HttpSessionSecurityContextRepository:SecurityContext爲空或內容爲匿名 - 上下文不會存儲在HttpSession中。 org.springframework.security.web.context.SecurityContextPersistenceFilter:SecurityContextHolder中現在清零,作爲請求處理完成

科爾多瓦請求

org.springframework.security.web.FilterChainProxy:/呈交在另外的過濾器鏈中檢查11的位置1; org.springframework.security.web.FilterChainProxy:/提交 - 檢查在11的位置2在另外的過濾器鏈中;發射過濾器:'SecurityContextPersistenceFilter' org.springframework.security.web.context.HttpSessionSecurityContextRepository:目前沒有HttpSession org.springframework.security.web.context.HttpSessionSecurityContextRepository:沒有SecurityContext從HttpSession中可用:null。一個新的將被創建。 org.springframework.security.web.FilterChainProxy:/提交檢查在11的位置3在另外的過濾器鏈中;發送過濾器:'HeaderWriterFilter' org.springframework.security.web.header.writers.HstsHeaderWriter:由於它與requestMatcher不匹配,因此未注入HSTS標頭org.springframework.se[email protected]461e0eb8 org.springframework.security.web.FilterChainProxy:/ submit-check在11位的第4位添加過濾鏈; org.springframework.security.web.util.matcher.AntPathRequestMatcher:檢查請求匹配:'/ submit-check';反對'/ j_spring_security_logout' org.springframework.security.web.FilterChainProxy:/ submit-check在11位的第5位附加的過濾器鏈中;點擊過濾:'UsernamePasswordAuthenticationFilter' org.springframework.security.web.util.matcher.AntPathRequestMatcher:檢查請求匹配:'/ submit-check';反對 '/ j_spring_security_check' org.springframework.security.web。FilterChainProxy:/提交 - 檢查在11的位置6在另外的過濾器鏈中; org.springframework.security.web.FilterChainProxy:/ submit-check在11位的第7位,在另外的過濾器鏈中;過濾器:'RequestCacheAwareFilter' org.springframework.security.web.FilterChainProxy: org.springframework.security.web.FilterChainProxy:/提交 - 檢查11位的第8位在額外的過濾器鏈中;發起過濾:'AnonymousAuthenticationFilter' org.springframework.security.web.authentication.AnonymousAuthenticationFilter:使用匿名標記填充SecurityContextHolder:'org.sprin[email protected]90550640:Principal:anonymousUser;證書:[PROTECTED];已驗證:true;詳細信息:org.sprin[email protected]7798:RemoteIpAddress:192.168.1.5; SessionId:null;授予權限:ROLE_ANONYMOUS' org.springframework.security.web.FilterChainProxy:/ submit-check在11位的第9位的附加過濾器鏈中;發射過濾器:'SessionManagementFilter' org.springframework.security.web.session.SessionManagementFilter:請求的會話ID F26DAEDA16CA5DAE443ABF8A4ADD836F無效。 org.springframework.security.web.FilterChainProxy:/ submit-check在11位的第10位的附加過濾器鏈中; org.springframework.security.web.FilterChainProxy:/提交 - 在11位的11位檢查其他過濾器鏈; 'FilterSecurityInterceptor' org.springframework.security.web.util.matcher.AntPathRequestMatcher:檢查請求匹配:'/ submit-check';針對'/**.html' org.springframework.security.web.access.intercept.FilterSecurityInterceptor:公共對象 - 未嘗試身份驗證 org.springframework.security.web.FilterChainProxy:/ submit-check附加過濾器鏈到達結尾;繼續使用原始鏈 org.springframework.security.web.access.ExceptionTranslationFilter:鏈正常處理 org.springframework.security.web.context.HttpSessionSecurityContextRepository:SecurityContext爲空或內容爲匿名 - 上下文將不會存儲在HttpSession中。 org.springframework.security.web.context.SecurityContextPersistenceFilter:SecurityContextHolder中現在清零,作爲請求處理完成

日誌是相同的,除了這些線其在從瀏覽器發起該請求:

org.springframework.web.servlet.DispatcherServlet:名稱爲'dispatcher'的DispatcherServlet處理[/ ab/submit-check]的POST請求 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping:查找處理程序方法路徑/提交檢查 org.springframework.web.servlet。 mvc.method.annotation.RequestMappingHandlerMapping:返回處理程序方法[public org.springframework.web.servlet.ModelAndView com.gm.ab.controller.MobileNavigation.save(java.lang.String)] org.springframework.beans.factory。 support.DefaultListableBeanFactory:返回單例bean的緩存實例'mobileNavigation' org.springframework.web.servlet.DispatcherServlet:Null ModelAndView返回到名爲'dispatcher'的DispatcherServlet:假設HandlerAdapter完成請求處理 org.springframework.web.servlet.DispatcherServlet :成功完成請求

由於某種原因,源自Cordova的請求沒有被髮送到Spring的DispatcherServlet,我對此感到不知所措。

我已經安裝Weinre進行遠程調試,Chrome和Cordova發送的請求數據似乎是相同的(儘管Weinre忽略了大部分頭信息)。

回答

1

管理解決此問題。

問題是在我的tomcat web.xml(conf中的tomcat全局web.xml)中有一個CORS過濾器。對於不需要在那裏的科爾多瓦應用程序。

Cordova通過頭文件「Origin:file://」發送請求。如果在Tomcat中設置CORS過濾器,則請求將失敗。

從web.xml中刪除CORS過濾器的工作,我現在可以POST數據。

+0

非常感謝。這是一個很好的答案.. – Keerthivasan

+0

嗨,我有類似的麻煩。你如何配置web.xml以允許「file://」? –

+0

嗨,我知道它已經很久了。你在回答中說刪除cors過濾器。在這種情況下,如何確保tomcat接受交叉源請求? –