0

我有一个 Spring MVC 项目,我想设置一个受登录保护的欢迎主页。我的配置结果通常是如果我询问如下 URL:

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

服务器正确地向我显示了一个登录页面。如果我问这个 URL:

http;//本地主机: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>
4

1 回答 1

0

如果您只想要经过身份验证的用户,最好使用扩展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);
}
于 2018-09-20T16:04:57.427 回答