How to Set “exclude-mapping” in Spring MVC Interceptor - spring

Here is my XML configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
...
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
... ">
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/style/**" location="/resources/style/" />
<resources mapping="/script/**" location="/resources/script/" />
<resources mapping="/images/**" location="/resources/images/" />
<resources mapping="/uploaded/**" location="/resources/uploaded/" />
<interceptors>
<interceptor>
<mapping path="/**" />
<exclude-mapping path="/style/**" />
<exclude-mapping path="/script/**" />
<exclude-mapping path="/images/**" />
<exclude-mapping path="/uploaded/**" />
<exclude-mapping path="/resources/**" />
<beans:bean class="com.example.MyInterceptor" />
</interceptor>
</interceptors>
</beans:beans>
My issue is when I browse a file in /style or other path in exclude mappins, such as /style/style.css, the interceptor code still run. Even if I change the config to <exclude-mapping path="/style/*" />, the issue still happen.
How do I set the config file to let <exclude-mapping> work?

I found the solution.
As what gamerkore said in About mvc:intercepter,how to set excluded path said, spring add this feature in version 3.2 (my project had used ver. 3.1.1.RELEASE). It can work after updating the version of spring (I updated it to ver. 4.2.0.RELEASE)

Related

My bootstrap style is not rendering prperly in my spring mvc application

Iam developing a E-musicstore application using spring mvc and hibernate.When I add header.jsp file to my ViewproductList.Jsp file The page is not rendering my bootstrap style properly.
my dispatcher servlet configuration
this is how its displayed in browser
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Interceptor -->
<context:component-scan base-package="com.mymusic" />
</beans:beans>
Try this... if you face same issue, please attatch your brower's NETWORK Tab which i can see HTTP information.

SpringData and MongoDB configuration error : MongoTemplate

I am currently attempting an integration of mongodb to my spring application.
I have the initial mongodb environment setup on my machine, service startup, etc.
However on the application configuration in spring there is an error related to the mongoTemplate construction-arg
"wildcard is strict but no declaration found for element"
I've actually used the same xml outlay as described in the documents here, the same as some recent tutorials online, and as far as I can tell I have addded the correct namespace urls. So i am a bit lost at the moment as to what I am missing. Any help on this is most appreciated.
Here is a look at the xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- scanning comment root context of all components package directory-->
<context:component-scan base-package="com.demo" />
<!--Dispatcher servlet-->
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
<!-- mongo db config -->
<mongo:mongo host="localhost" port="27017" id="mongo" />
<mongo:repositories base-package="com.demo.football.repository" />
<beans:bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="FootballManager"/>
</beans:bean>
</beans:beans>
Qualify the xsd names to match the spring version you're using.
Considering you're using Spring 4, the xshema name space will change to following.
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd">

How to display images in a jsp file in Spring MVC

I am very new to spring. I am using spring 3.29 version, tomcat 7. I need to display an image in .jsp file. I searched a lot. There are plenty of post about this problem. But still i am unable to solve this problem. Please help.
The following is my application structure
The following is my spring-servlet.xml file code
<beans 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: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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.wipro.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
I have a page footer.jsp. I need to add an image in this file
The following is code
<!-- Here I need to add an image -->
<hr/>
<p>Copyright 2010-2014 javatpoint.com.</p>
I got the image in my page. #Pankaj Saboo comments helped a lot. Thanks for all of you.
I have added the below code in spring-servlet.xml
<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="WEB-INF/resources/images/" />
And in footer.jsp I have added the below code
<img src="<c:url value="/images/wipLogo.png" />"/>
Add following line in your spring-servlet.xml
<resources mapping="/images/**" location="/images/" />
and on jsp
<link rel="icon"
href="http://<hostname>/projectname/images/imageName"/>
Try adding the following resources declaration to your Spring configuration:
<!-- Handles HTTP GET requests for /images/** by efficiently serving up static resources in the ${webappRoot}/images directory -->
<resources mapping="/images/**" location="/images/" />
Alternatively, and more common, is to have a resources folder which contains all your resources (images, css, js, etc...), broken out by sub-directories.
Your configuration would then look like:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
And your resources would be referenced as follows:
<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/screen.css" />" />
<script type="text/javascript" src="<c:url value="/resources/js/jquery-1.6.4.min.js" />"></script>
<img src="<c:url value="/resources/images/left_arrow.png" />"

Spring Security User-Roles are not loaded from external context file under JBOSS 7

I'm facing a problem on a pretty simple task and i don't know why.
I have a web application deployed under Jboss 7 that use Spring Security to allow access to different users and profiles.
I have 2 different security files one that is a global configuration file which configure the access to public/static stuff and another one that define specifically the users , roles and the paths that are allowed for each role.
Below both configurations:
General configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<context:component-scan base-package="mypackage.*" />
<!-- STATIC RESOURCES For which security context is not enabled -->
<security:http pattern="/css/**" security="none" />
<security:http pattern="/images/**" security="none" />
<security:http pattern="/js/**" security="none" />
<beans:import resource="classpath*:instance-security.xml" />
<bean id="myFilter" class="mypackage.filters.SessionFilter" />
<!-- setting timeout on success login -->
<bean id="loginSucessHandler"
class="mypackage.login.handlers.LoginSucessHandler">
<property name="defaultTargetUrl" value="/list/all" />
<property name="timeout" value="3600" />
</bean>
<!-- handler for failing login attempts -->
<bean id="loginFailureHandler"
class="mypackage.login.handlers.LoginFailureHandler">
</bean>
</beans>
Specific Configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<context:component-scan base-package="mypackage.*" />
<security:http auto-config="true" use-expressions="true">
<security:access-denied-handler error-page="/home?err=403"/>
<security:headers>
<security:cache-control />
<security:content-type-options />
<security:xss-protection />
</security:headers>
<security:intercept-url pattern="/home" access="permitAll()" />
<security:intercept-url pattern="/dashboard**" access="hasAnyRole('ROLE_ADMIN','ROLE_USER')" />
<security:intercept-url pattern="/dashboard/**" access="hasAnyRole('ROLE_ADMIN','ROLE_USER')" />
<security:intercept-url pattern="/configurations/**" access="hasRole('ROLE_ADMIN')" />
<security:intercept-url pattern="/configurations**" access="hasRole('ROLE_ADMIN')" />
<!-- form login definition -->
<security:form-login login-page="/home"
login-processing-url="/login.do"
username-parameter="username"
password-parameter="password"
authentication-failure-url="/home?err=1"
authentication-success-handler-ref="loginSucessHandler"
/>
<security:logout logout-url="/logout" invalidate-session="true" delete-cookies="JSESSIONID" logout-success-url="/home" />
<security:csrf />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="admin" password="test" authorities="ROLE_ADMIN,ROLE_USER" />
<security:user name="user" password="test" authorities="ROLE_USER" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
The above configurations works perfectly if i put both of them in my web application, the import is processed correctly and the users and roles are added correctly.
For my project i need to move the specific configuration file under JBoss classpath.
To allow Spring security to import the file from the Jboss classpath i used the new "module" concept they have in Jboss creating a custom module which basically has only my file in it.
The module folder structure is like this:
${JBOSS_HOME}>modules>mycustom>conf>main
and in the main folder i have the module.xml and the instance-security.xml. (which is the specifi configuration posted above)
The module.xml is like this:
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ JBoss, Home of Professional Open Source.
~ Copyright 2010, Red Hat, Inc., and individual contributors
~ as indicated by the #author tags. See the copyright.txt file in the
~ distribution for a full listing of individual contributors.
~
~ This is free software; you can redistribute it and/or modify it
~ under the terms of the GNU Lesser General Public License as
~ published by the Free Software Foundation; either version 2.1 of
~ the License, or (at your option) any later version.
~
~ This software is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
~ Lesser General Public License for more details.
~
~ You should have received a copy of the GNU Lesser General Public
~ License along with this software; if not, write to the Free
~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->
<module xmlns="urn:jboss:module:1.1" name="mycustom.conf">
<resources>
<resource-root path="."/>
</resources>
</module>
If i use this approach the file seems still loaded , because the "import" from the general security file does not throw any exception but the security is not applied. So i can access any path without logging in.
Does anyone knows which could be the problem ?
Any tip could be useful.
Seems i solved the problem.
My web app is included in an .ear with more others application/libraries and in this ear i had the following deployment configuration:
<deployment>
<ear-subdeployments-isolated>true</ear-subdeployments-isolated>
<dependencies>
<module name="mycustom.conf" />
</dependencies>
<sub-deployment name="somelibrary.jar">
.....
</sub-deployment>
</deployment>
While the module was still correctly loaded and dependency satisfied i had the problem described above (with the security roles and user not being applied).
After i changed the deployment configuration to this:
<jboss-deployment-structure>
<ear-subdeployments-isolated>true</ear-subdeployments-isolated>
<sub-deployment name="somelibrary.jar">
.....
</sub-deployment>
<sub-deployment name="myapp.war">
<dependencies>
<module name="mycustom.conf" />
</dependencies>
</sub-deployment>
</jboss-deployment-structure>
the security roles are correctly applied.
I do not really understand why moving the dependency to the specific "sub-deployment" solved the problem.
But that is what worked for me.
Regards.

xml spring social xmlns:facebook

I want to build a login with spring social, and I have implemented a *xml whit the next configuration, but the url http://www.springframework.org/schema/social/facebook is wrong?
<?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:facebook="http://www.springframework.org/schema/social/facebook"
xmlns:twitter="http://www.springframework.org/schema/social/twitter"
xmlns:social="http://www.springframework.org/schema/social"
xmlns:linkedin="http://www.springframework.org/schema/social/linkedin"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/social/facebook http://www.springframework.org/schema/social/spring-social-facebook.xsd
http://www.springframework.org/schema/social/linkedin http://www.springframework.org/schema/social/spring-social-linkedin.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/social/twitter http://www.springframework.org/schema/social/spring-social-twitter.xsd
http://www.springframework.org/schema/social http://www.springframework.org/schema/social/spring-social.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:/itrippingWeb/src/main/resources/application.properties" />
<facebook:config app-id="${facebook.clientId}" app-secret="${facebook.clientSecret}" app-namespace="socialshowcase" />
<social:jdbc-connection-repository/>
<bean id="userIdSource" class="org.springframework.social.security.AuthenticationNameUserIdSource" />
<bean id="connectController" class="org.springframework.social.connect.web.ConnectController" autowire="constructor">
<property name="connectInterceptors">
<list>
<bean class="org.springframework.social.showcase.facebook.PostToWallAfterConnectInterceptor" />
<bean class="org.springframework.social.showcase.twitter.TweetAfterConnectInterceptor" />
</list>
</property>
</bean>
<bean id="psc" class="org.springframework.social.connect.web.ProviderSignInController" autowire="constructor" />
<bean id="signInAdapter" class="org.springframework.social.showcase.signin.SimpleSignInAdapter" autowire="constructor" />
<bean id="disconnectController" class="org.springframework.social.facebook.web.DisconnectController"
c:_0-ref="usersConnectionRepository" c:_1="${facebook.clientSecret}" />
</beans>
And this xml retrieve me the next exception:
Multiple annotations found at this line:
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element
'facebook:config'.
- schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/social/spring-
social-facebook.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element
of the document is not <xsd:schema>.
Anybody knows the problem?
thank you!!!!
I think this has to do with your pom missing some of the dependencies.
Is spring-social-security in your dependency?
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-security</artifactId>
<version>1.1.0.RELEASE</version>
Here is a similar application with the same config as your spring xml above. Here is the pom it uses https://github.com/spring-projects/spring-social-samples/blob/master/attic/spring-social-showcase-xml/pom.xml
Here is a reference to a similar error resolved by fixing the pom dependency https://github.com/spring-projects/spring-social-facebook/issues/79
Actually the url "http://www.springframework.org/schema/social/facebook" links to nowhere.
I found another schema files location: http://docs.spring.io/autorepo/schema/
I used "http://docs.spring.io/autorepo/schema/spring-social/current/social/spring-social.xsd" and it works for me.

Resources