2017-08-18 145 views
1

我寫了一個控制器導航到名爲accessDenied.jsp的頁面。 我使用Spring 4.3.6版本爲什麼在spring中返回ModelAndView提示下載視圖名稱的.json?

@Controller 
public class BatchAccessDeniedController { 

static String ERRORPAGE = "accessDenied"; 

public static final Logger LOG = Logger.getLogger(BatchAccessDeniedController.class); 

@RequestMapping(value = "/accessDenied" , method = RequestMethod.GET) 
public ModelAndView accessDenied(Principal user, ModelAndView modelAndView) { 

    //Log the user who tried to access the restricted resource 
    if (user != null) { 
     LOG.info(user.getName() + " you do not have permission to access this page!"); 
    } else { 
     LOG.info("You do not have permission to access this page!"); 
    } 
    modelAndView.setViewName(ERRORPAGE); 
    return modelAndView; 
} 
} 

,一切工作正常與上面的代碼,直到我介紹Spring Batch的聯繫依賴,並在web.xml中批量管理配置爲波紋管

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     classpath:/root-context.xml, 
     classpath*:/org/springframework/batch/admin/web/resources/webapp-config.xml, 
     classpath:/config/spring-ldap-servlet.xml 
    </param-value> 
</context-param> 

<!-- Filter for Spring Security LDAP --> 
<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

<filter> 
    <filter-name>shallowEtagHeaderFilter</filter-name> 
    <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class> 
</filter> 

<filter> 
    <filter-name>hiddenHttpMethodFilter</filter-name> 
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<filter-mapping> 
    <filter-name>shallowEtagHeaderFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<filter-mapping> 
    <filter-name>hiddenHttpMethodFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<listener> 
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
</listener> 

<servlet> 
    <servlet-name>runlauncher</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      classpath:/config/application-web-context.xml, 
      classpath*:/org/springframework/batch/admin/web/resources/servlet-config.xml 
     </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>runlauncher</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

<!-- Invalidate user session after 15 minutes of inactivity --> 
<session-config> 
    <session-timeout>15</session-timeout> 
</session-config> 

<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

批次管理依賴關係是如下面

<dependency> 
     <groupId>org.springframework.batch</groupId> 
     <artifactId>spring-batch-admin-manager</artifactId> 
     <version>${spring.batch.admin}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.batch</groupId> 
     <artifactId>spring-batch-admin-resources</artifactId> 
     <version>${spring.batch.admin}</version> 
    </dependency> 

通過添加此,認爲來到BatchAccessDeniedController類的控制,它會提示給downl oad index.json文件。我不明白爲什麼它要求下載index.json文件而不是重定向到accessDenied.jsp頁面。另外我的配置是spring-ldap,其中index.jsp是用戶登錄後的默認頁面。

我懷疑是導致問題的批量管理用戶界面出了問題。如果有人遇到類似的問題,請幫我解決這個問題。

下面是顯示下載圖標快照的鏈接。 index.json file

回答

1

最後經過大量的研究發現修復。配置的runlauncher servlet有兩個servlet xml文件。

  1. 的classpath *:/組織/ springframework的/批號/管理/網絡/資源/的servlet-config.xml中
  2. 類路徑:/config/application-web-context.xml,

的第一個xml文件來自spring-batch-admin-domain jar,第二個xml文件是在應用程序級別配置的文件

Spring批次管理UI在內部具有在spring-batch-admin-manager.jar中配置的JSONViewResolver。由於JSONViewResolver優先於在application-web-context.xml中配置的InternalResouceViewResolver,因此使用ModelAndVeiw提示下載ModelAndView中返回的視圖名稱的.json

通過爲每個xml創建兩個單獨的servlet解決了此問題配置文件和具有不同的映射如下

<servlet> 
    <servlet-name>runlauncher</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      classpath:/config/application-web-context.xml 
     </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet> 
    <servlet-name>Batch Servlet</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      classpath*:/org/springframework/batch/admin/web/resources/servlet-config.xml 
     </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>runlauncher</servlet-name> 
    <url-pattern>/batch/*</url-pattern> 
</servlet-mapping> 

<servlet-mapping> 
    <servlet-name>Batch Servlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

SO具有兩個servlet與用於彈簧批處理管理界面與URL映射爲一個小服務程序「/」,將攔截所有以「/」,接着由映射傳入請求。另一個servlet會攔截所有匹配請求「/ batch/*」

通過這種方式,批量servlet使用JSONViewResolver的請求和使用爲application-web-context.xml配置的「/ batch/*」的請求使用InternalResourceViewResolver。這解決了我的問題。

相關問題