Установить защищенную страницу приветствия по умолчанию в проекте Spring MVC - PullRequest
0 голосов
/ 20 сентября 2018

У меня есть проект Spring MVC, и я хотел бы установить домашнюю страницу приветствия, защищенную логином.Результатом моей конфигурации является то, что обычно, если я спрашиваю URL-адрес вроде:

http://localhost:8080/angularjava/app/homepage.html

, сервер правильно отображает мне страницу входа.Если я задам этот URL-адрес:

http; // localhost: 8080 / angularjava

, я вижу страницу homepage.html (со ошибочными ссылками на css и т. Д.), И страница входа не отображается.

Это мой web.xml файл:

<web-app 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_3_0.xsd"
     version="3.0"
     metadata-complete="true">
  <display-name>AngularJS-Java8-SpringMVC-MongoDB</display-name>
  <welcome-file-list>
    <welcome-file>/app/homepage.html</welcome-file>  
  </welcome-file-list>

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<security-constraint>
   <web-resource-collection>
       <web-resource-name>Pagine applicazione</web-resource-name>
       <url-pattern>/app/*</url-pattern>
       <http-method>GET</http-method>
       <http-method>POST</http-method>  
   </web-resource-collection>
   <auth-constraint>
       <role-name>*</role-name>         
   </auth-constraint>
   <user-data-constraint>
       <transport-guarantee>NONE</transport-guarantee>
   </user-data-constraint>
   </security-constraint>

   <login-config>
   <auth-method>FORM</auth-method>
   <form-login-config>
       <form-login-page>/public/login.html</form-login-page>
       <form-error-page>/public/error.html</form-error-page>
   </form-login-config>
   </login-config>

   <security-role>
       <role-name>*</role-name>
   </security-role>
</web-app>

А это мой springmvc-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
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/data/mongo 
        http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:annotation-config />

<tx:annotation-driven />
<mvc:annotation-driven />
<context:property-placeholder location="classpath:mongodb.properties" />
<mongo:repositories base-package="sa.angularjava.repository"></mongo:repositories>
<!--Component scanning with <context:component-scan base-package="com.rocketAlan" 
    /> is telling spring that it should search the class path for all the classes 
    under "sa.angularjava" and look at each class to see if it has a @Controller, 
    or @Repository, or @Service, or @Component and if it does then Spring will 
    register the class with the bean factory as if you had typed <bean class="..." 
    /> in the xml configuration files. -->
<!-- Specify base package of the components DAO, Controller, etc -->
<context:component-scan base-package="sa.angularjava.config" />
<context:component-scan base-package="sa.angularjava.controller" />
<context:component-scan base-package="sa.angularjava.dao" />
<context:component-scan base-package="sa.angularjava.rest" />
<context:component-scan base-package="sa.angularjava.service" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<!-- Maps static resources like images, css, javascript files -->
<mvc:resources mapping="/app/**" location="/app/" />
<mvc:resources mapping="/public/**" location="/public/" />

<!-- Setting the connection with MONGODB -->

<context:property-placeholder location="classpath:mongodb.properties"/>

<mongo:mongo-client host="${mongo.host}" port="${mongo.port}" credentials="root:example@admin">

</mongo:mongo-client>
<mongo:db-factory dbname="Auth" mongo-ref="mongoClient"/>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
  <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>

1 Ответ

0 голосов
/ 20 сентября 2018

Если вам нужны только аутентифицированные пользователи, лучше иметь класс, который расширяет WebSecurityConfigurerAdapter и переопределяет protected void configure(HttpSecurity http), чтобы указать, к каким путям обращаться только зарегистрированный пользователь:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors().and().csrf().disable().authorizeRequests()
            .antMatchers(SIGN_UP_URL, "/login", "/getAll").permitAll()
            .anyRequest().authenticated()
            .and()
            .addFilter(new JWTAuthenticationFilter(authenticationManager()))
            .addFilter(new JWTAuthorizationFilter(authenticationManager()))
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
...