Note that there are some explanatory texts on larger screens.

plurals
  1. POspring web, security + web.xml + mvc dispatcher + Bean is created twice
    primarykey
    data
    text
    <p>I have the Web.xml as below:</p> <pre><code> &lt;servlet&gt; &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;filter&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;url-pattern&gt;/api/secure/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; </code></pre> <p>[Edit]</p> <p>After I added the spring security, then I get the error! </p> <blockquote> <p>java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?</p> </blockquote> <p>then I added</p> <pre><code> &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/mvc-dispatcher-servlet.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; </code></pre> <p>then it seems working fine, but then 1) The problem is the bean are created twice!</p> <p>if I only remove that: </p> <pre><code>&lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/mvc-dispatcher-servlet.xml &lt;/param-value&gt; &lt;/context-param&gt; </code></pre> <p>but leave the <code>&lt;listener&gt;</code> then the web application doesn't run at all</p> <p>[Extra]</p> <p>The full Web.xml is below: </p> <pre><code>&lt;web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt; &lt;display-name&gt;Spring MVC Application&lt;/display-name&gt; &lt;servlet&gt; &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;filter&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;url-pattern&gt;/api/secure/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/mvc-dispatcher-servlet.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;/web-app&gt; </code></pre> <p>here is my mvc-dispatcher-servlet.xml</p> <pre><code>&lt;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:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:security="http://www.springframework.org/schema/security" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"&gt; &lt;mvc:annotation-driven/&gt; &lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.ge.wtracker"/&gt; &lt;context:property-placeholder location="classpath*:META-INF/spring/*.properties"/&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/pages/"/&gt; &lt;property name="suffix" value=".jsp"/&gt; &lt;/bean&gt; &lt;!--Java Persistence API config--&gt; &lt;jpa:repositories base-package="com.ge.wtracker.repository"/&gt; &lt;!--JPA and Database Config--&gt; &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="persistenceUnitName" value="defaultPersistenceUnit"/&gt; &lt;property name="dataSource" ref="dataSource"/&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt; &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt; &lt;/bean&gt; &lt;bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource"&gt; &lt;property name="driverClassName" value="${database.driverClassName}"/&gt; &lt;property name="url" value="${database.url}"/&gt; &lt;property name="username" value="${database.username}"/&gt; &lt;property name="password" value="${database.password}"/&gt; &lt;property name="testOnBorrow" value="true"/&gt; &lt;property name="testOnReturn" value="true"/&gt; &lt;property name="testWhileIdle" value="true"/&gt; &lt;property name="timeBetweenEvictionRunsMillis" value="1800000"/&gt; &lt;property name="numTestsPerEvictionRun" value="3"/&gt; &lt;property name="minEvictableIdleTimeMillis" value="1800000"/&gt; &lt;property name="validationQuery" value="SELECT 1"/&gt; &lt;/bean&gt; &lt;!--Spring Security--&gt; &lt;security:http create-session="stateless" entry-point-ref="restAuthenticationEntryPoint" authentication-manager-ref="authenticationManager"&gt; &lt;security:intercept-url pattern="/api/secure/**" access="ROLE_USER" /&gt; &lt;security:custom-filter ref="customRestFilter" position="BASIC_AUTH_FILTER" /&gt; &lt;/security:http&gt; &lt;!-- Configures the authentication entry point that returns HTTP status code 401 --&gt; &lt;bean id="restAuthenticationEntryPoint" class="com.ge.wtracker.web.security.RestAuthenticationEntryPoint"&gt; &lt;property name="realmName" value="Not Authorized" /&gt; &lt;/bean&gt; &lt;security:authentication-manager alias="authenticationManager"&gt; &lt;security:authentication-provider ref="restAuthenticationProvider" /&gt; &lt;/security:authentication-manager&gt; &lt;!--The customRestFilter responsibles for retrieving and manipulating any request data to pass to the authentication provider to authenticate.--&gt; &lt;bean id="customRestFilter" class="com.ge.wtracker.web.security.RestSecurityFilter"&gt; &lt;constructor-arg name="authenticationManager" ref="authenticationManager" /&gt; &lt;constructor-arg name="authenticationEntryPoint" ref="restAuthenticationEntryPoint" /&gt; &lt;/bean&gt; &lt;bean id="restAuthenticationProvider" class="com.ge.wtracker.web.security.RestAuthenticationProvider" /&gt; &lt;/beans&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload