2016-07-25 2 views
6

Ich habe eine Legacy-Anwendung mit Spring Xml, die ich zu Spring-Boot migrieren.Spring-Boot mit mehreren Sicht Resolver aus Xml-Konfiguration nicht korrekt umleiten

Die Anwendung startet und ich bekomme die Authentifizierungsseite, zugeordnet in der applicationContext-login.xml. Bei erfolgreichem Login sollte es WEB-INF/client/home.jsp laden, aber stattdessen versucht es /WEB-INF/auth/home.jsp zu laden und ich bekomme 404. Im Start-Log sehe ich, wie es alle mappt Pfade. Warum ist es in diesen Weiterleitungen widersprüchlich und was kann ich tun, um das zu beheben? Treten Probleme auf, weil mehrere @ImportResource Sichtlöser enthalten?

Auszug aus Sicherheits http-Konfiguration:

<s:http use-expressions="true" entry-point-ref="delegatingAuthenticationEntryPoint"> 
     <s:form-login login-page="/auth/login" 
         login-processing-url="/auth/j_spring_security_check" 
         authentication-failure-url="/auth/login-secure?loginFailed=true" 
         default-target-url="/auth/defaultEntry"/> 
     <s:logout logout-url="/auth/logout" logout-success-url="/auth/logout-success" delete-cookies="jsessionid"/> 
    </s:http> 

Der Controller es weist auf:

@RequestMapping(value = "/defaultEntry", method = RequestMethod.GET) 
    public String defaultEntry() { 
     if (authentication.isAuthenticated()) { 
       return "redirect:/client/home"; 
     } else { 
      return "redirect:login"; 
     } 
    } 

Die Anwendung ist in XML-Dateien mehr Ansicht Resolvern konfigurieren:

  • classpath *:/springContext/applicationContext-login.xml

    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans.xsd 
             http://www.springframework.org/schema/context 
             http://www.springframework.org/schema/context/spring-context.xsd 
             http://www.springframework.org/schema/mvc 
             http://www.springframework.org/schema/mvc/spring-mvc.xsd" 
        default-init-method="init" 
        default-destroy-method="destroy"> 
    
        <import resource="applicationContext-web-common.xml" /> 
    
        <!-- Static login resources --> 
        <mvc:resources mapping="/css/**" location="/WEB-INF/auth/css/"/> 
        <mvc:resources mapping="/assets/**" location="/WEB-INF/auth/assets/"/> 
        <mvc:resources mapping="/js/**" location="/WEB-INF/auth/js/"/> 
    
        <context:component-scan base-package="org.myCompany.auth" /> 
    
        <!-- view resolver for JSP --> 
        <bean id="loginViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
         <property name="prefix" value="/WEB-INF/auth/"/> 
         <property name="suffix" value=".jsp"/> 
        </bean> 
    
        <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> 
         <property name="defaultLocale" value="en_US"/> 
        </bean> 
    

  • Classpath *:/springContext/application-client.xml“

    <?xml version="1.0" encoding="UTF-8"?> 
    
    <beans xmlns="http://www.springframework.org/schema/beans" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:context="http://www.springframework.org/schema/context" 
         xmlns:mvc="http://www.springframework.org/schema/mvc" 
         xsi:schemaLocation="http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans.xsd 
             http://www.springframework.org/schema/context 
             http://www.springframework.org/schema/context/spring-context.xsd 
             http://www.springframework.org/schema/mvc 
             http://www.springframework.org/schema/mvc/spring-mvc.xsd" 
         default-init-method="init" 
         default-destroy-method="destroy"> 
    
        <import resource="applicationContext-web-common.xml" /> 
    
        <context:component-scan base-package="org.myCompany.client" /> 
    
        <!-- Static resources --> 
        <mvc:resources mapping="/player/**" location="/WEB-INF/client/player/"/> 
        <mvc:resources mapping="/css/**" location="/WEB-INF/client/css/"/> 
        <mvc:resources mapping="/data/**" location="/WEB-INF/client/data/"/> 
        <mvc:resources mapping="/js/**" location="/WEB-INF/client/js/"/> 
        <mvc:resources mapping="/locales/**" location="/WEB-INF/client/locales/"/> 
        <mvc:resources mapping="/media/**" location="/WEB-INF/client/media/"/> 
        <mvc:resources mapping="/index.html" location="/WEB-INF/client/index.html"/> 
        <mvc:resources mapping="/test.html" location="/WEB-INF/client/test.html"/> 
        <mvc:resources mapping="/admin/**" location="/WEB-INF/client/admin/"/> 
        <mvc:resources mapping="/documentation/**" location="/WEB-INF/client/documentation/"/> 
    
    
        <bean id="clientViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
         <property name="prefix" value="/WEB-INF/client/"/> 
         <property name="suffix" value=".jsp"/> 
        </bean> 
    
    
    </beans> 
    

Es gibt auch einige andere nach demselben Konfigurationsmuster.

Ich lade die Ressourcen in der Application.java

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) 
    //@EnableWebMvc 
    @ComponentScan({"org.myCompany"}) 
    @ImportResource({"classpath*:/springContext/applicationContext-controllers.xml", 
      "classpath*:/springContext/applicationContext-rest.xml", 
      "classpath*:/springContext/applicationContext-login.xml", 
      "classpath*:/springContext/applicationContext-client.xml", 
      "classpath*:/springContext/applicationContext-admin.xml", 
      "classpath*:/springContext/applicationContext-logging.xml", 
      "classpath*:/springContext/applicationContext-web-common.xml" 
    }) 
    public class Application extends SpringBootServletInitializer { 

     public static void main(String[] args) throws UnknownHostException { 
      SpringApplication app = new SpringApplication(Application.class); 
      ApplicationContext ctx = app.run(args); 
      Environment env = ctx.getEnvironment(); 

      logger.info(String.format("\n----------------------------------------------------------\n\t" + 
          "Application '%s' is running! Access URLs:\n\t" + 
          "Local: \t\thttp://localhost:%s\n\t" + 
          "External: \thttp://%s:%s\n----------------------------------------------------------", 
        env.getProperty("spring.application.name"), 
        env.getProperty("server.port"), 
        InetAddress.getLocalHost().getHostAddress(), 
        env.getProperty("server.port"))); 
     } 

     @Bean 
     public ServletRegistrationBean restDispatcher() { 
      ServletRegistrationBean registration = new ServletRegistrationBean(new DispatcherServlet(), 
        "/rest/*", "/websocket/*"); 
      registration.setName("rest-dispatcher"); 
      registration.setLoadOnStartup(2); 
      Map<String, String> params = new HashMap<>(); 
      params.put("contextConfigLocation", "classpath*:springContext/applicationContext-rest.xml"); 
      registration.setInitParameters(params); 
      return registration; 
     } 

     @Bean 
     public ServletRegistrationBean authDispatcher() { 
      ServletRegistrationBean registration = new ServletRegistrationBean(new DispatcherServlet(), "/auth/*"); 
      registration.setName("auth-dispatcher"); 
      registration.setLoadOnStartup(2); 
      Map<String, String> params = new HashMap<>(); 
      params.put("contextConfigLocation", "classpath*:springContext/applicationContext-login.xml"); 
      registration.setInitParameters(params); 
      return registration; 
     } 

     @Bean 
     public ServletRegistrationBean clientDispatcher() { 
      ServletRegistrationBean registration = new ServletRegistrationBean(new DispatcherServlet(), "/client/*"); 
      registration.setName("client-dispatcher"); 
      registration.setLoadOnStartup(2); 
      Map<String, String> params = new HashMap<>(); 
      params.put("contextConfigLocation", "classpath*:springContext/applicationContext-client.xml"); 
      registration.setInitParameters(params); 
      return registration; 
     } 

    //... other servlets registration, filters registration 

    } 

Antwort

2

Du redirect:/client/home von Ihrem Login-Bildschirm zurückkehren, die von Ihrem loginViewResolver verarbeitet werden erhalten:

<bean id="loginViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
     <property name="prefix" value="/WEB-INF/auth/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

Die clientViewResolver nicht aufgerufen werden, da auf den View-Resolvern keine Reihenfolge angegeben ist. Sie können den Auftrag mit der Bestelleigenschaft festlegen.,

+0

Aber wie funktioniert alles gut auf der Federmvc-Anwendung? –

+0

Wenn Sie keine Reihenfolge angeben, wird die Standardreihenfolge für beide verwendet - was bedeutet, dass die erste Priorität nicht vorhersehbar ist und daher möglicherweise nicht funktioniert. – 6ton

+0

Auftrag 1 zum Kunden und 2 zum Login hinzugefügt. Jetzt erhalte ich nur Request URL: http: // localhost: 8000/client/home Anfrage Methode: GET Statuscode: 404 Nicht gefunden –

1

Ich kann annehmen, dass dies von Spring Security-Konfiguration verursacht wird und nicht von View Resolvers abhängt. Es sieht so aus, als ob der Login-Benutzer nach erfolgreicher Anmeldung auf die Seite umgeleitet wird, auf die er vorher zuzugreifen versuchte, und möglicherweise nicht . Versuchen Sie, Ihre Spring Security http-Konfiguration wie folgt zu bearbeiten:

<s:http use-expressions="true" entry-point-aref="delegatingAuthenticationEntryPoint"> 
    <s:form-login login-page="/auth/login" 
        login-processing-url="/auth/j_spring_security_check" 
        authentication-failure-url="/auth/login-secure?loginFailed=true" 
        default-target-url="/auth/defaultEntry" 
        always-use-default-target="true"/> 
    <s:logout logout-url="/auth/logout" logout-success-url="/auth/logout-success" delete-cookies="jsessionid"/> 
</s:http> 

Wenn es hilft - erhalten Sie einen Hinweis, wo Sie weiter suchen können.

Überprüfen Sie auch diese StackOverFlow answer.

1

Das Problem mit dem Dispatcher Servlet-Konfiguration Sie

definiert
<bean id="clientViewResolve" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
    <property name="prefix" value="/WEB-INF/client/"/> 
    <property name="suffix" value=".jsp"/> 

Mit dieser Konfiguration, seine wahrscheinlich

zu /WEB-INF/client/client/*.jsp entschlossen zu erhalten Es ist besser, einen einzelnen Viewresolver zu verwenden, als die Aufgabe, zwei View-Resolver zu haben, zu komplexieren.