在一个项目我正在开发的早期阶段,每当我添加web.xml
ContextLoaderListener
及其params,自定义登录,以及spring-security加载的默认登录时,它都会显示404错误。没有它,登录加载正常。
目前我正在使用Hibernate 5,Spring 5.0.7,并实现Spring Security。
帮助将不胜感激。谢谢。
这是扩展WebSecurityConfigurerAdapter
的类中的方法:
@Override
protected void configure(HttpSecurity http) throws Exception {
// TODO Auto-generated method stub
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/Login.xhtml").permitAll();
http.formLogin().failureUrl("/Login.xhtml?error=true");
http.formLogin().defaultSuccessUrl("/test.jsp",true);
}
.formLogin().loginPage("/Login.xhtml").permitAll();
也可以用.and().httpBasic();
代替
然后在Webcontent-Web-Inf里面我有一个web.xml
,我在其中添加spring的监听器来初始化它
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- PARAMETRO QUE INDICA NOMBRE Y RUTA DEL FICHERO DE CONFIGURACION DE
SPRING -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/spring/modelo.xml</param-value>
</context-param>`
modelo.xml
包含所有hibernate等映射...
<?xml version="1.0" encoding="iso-8859-15"?>
<beans default-autowire="byName" default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- COMPONENTE DE LECTURA DE PROPERTIES -->
<bean id="gestor_properties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- RUTA Y NOMBRE DE PROPERTIES A LEER -->
<property name="location"
value="classpath:com/spring/datos-conexion.properties" />
</bean>
<!-- ************ ZONA DE ACCESO A LA BASE DE DATOS ************ -->
<!-- CONEXION A LA BASE DE DATOS -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:com/atos/hibernate/hibernate.cfg.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager" />
<tx:annotation-driven
transaction-manager="transactionManager" />
<!-- **** DATASOURCE REQUERIDO POR TRANSACTION MANAGER **** -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${spring.datasource.driver}" />
<property name="url" value="${spring.datasource.url}" />
<property name="username" value="${spring.datasource.nombre}" />
<property name="password" value="${spring.datasource.clave}" />
</bean>
<!-- PAQUETES CON ANOTACIONES PARA SPRING PARA LA GESTION DEL MODELO -->
<context:component-scan
base-package="com.atos.hibernate.modelo" />
<context:component-scan
base-package="com.atos.hibernate.dao" />
<context:component-scan
base-package="com.atos.hibernate" />
<context:component-scan
base-package="com.atos.dao_ext" />
<context:component-scan
base-package="com.atos.util" />
当我专门测试项目的后端部分时,它与hibernate完美配合,我命令用hibernate等做的所有查询工作正常。
但是,当我实施春季安全时,悲伤到了......
问题解决了,我已经有一个springframework的上下文加载器干扰了新的上下文spring安全性的事实正在加载。这可以通过删除@configure注释并从`package com.atos.springSecurity中删除构造函数来解决;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
公共类SecurityWebApplicationInitializer扩展AbstractSecurityWebApplicationInitializer {
} `