Configuring Spring Security without the plugin web.xml problems
I'm currently building a Grails 1.3.5 app. It uses an existing model layer (shared code - no GORM) and I've successfully configured the tomcat jndi connections for it in conf/spring/resources.xml and all is working well. However I'm having problems when I try and configure Spring security. I'm NOT using the spring security plugin because I want to use the xml security config from another project we already have running. This uses Spring 3 security. I've followed the following steps on various blogs:
- Ran 'grails install-templates'
I added the following to the templates/war/web.xml:
<filter>
<filter-name>Spring Security Filter Chain Proxy</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>filterChainProxy</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring Security Filter Chain Proxy</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-cl开发者_StackOverflow中文版ass>
</listener>
- I added my security beans to the conf/spring directory and have validated them.
- I've copied the spring security jar files over to the grails lib directory. Version 3.0.2
When I run 'grails run-app' I get a very strange exception with the web.xml:
2010-11-26 12:16:02,512 INFO [startup.ContextConfig] No default web.xml
2010-11-26 12:16:02,518 ERROR [digester.Digester] End event threw exception
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.tomcat.util.IntrospectionUtils.callMethod1(IntrospectionUtils.java:925)
...
Caused by: java.lang.IllegalArgumentException: Filter mapping specifies an unknown filter name hiddenHttpMethod
at org.apache.catalina.core.StandardContext.addFilterMap(StandardContext.java:2251)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.tomcat.util.IntrospectionUtils.callMethod1(IntrospectionUtils.java:925)
at org.apache.tomcat.util.digester.SetNextRule.end(SetNextRule.java:193)
at org.apache.tomcat.util.digester.Rule.end(Rule.java:229)
at org.apache.tomcat.util.digester.Digester.endElement(Digester.java:1140)
... 437 more
I've also copied the filter code from other blogs that suggest:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
But I still get the same problem.
Does anyone have any advice about what I should try next? The annoying thing is that I had this all working on an older version of Grails (1.1.1) with version 2 of the security modules but now I'm getting problems.
I got this working a while back but it took a different setup in 1.3 than what worked in 1.2. I found I could only access external XML files by adding them to the contextConfigLocation context-param in web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/applicationContext-security.xml
</param-value>
</context-param>
Other than that the configuration was the same as for a non-Grails app. You probably also need the config jar.
Also, rather than copying jars to your lib directory you should use the IVy config in BuildConfig.groovy so you only have one copy of each jar on your machine. Here's one that should work:
grails.project.class.dir = 'target/classes'
grails.project.test.class.dir = 'target/test-classes'
grails.project.test.reports.dir = 'target/test-reports'
grails.project.dependency.resolution = {
inherits 'global'
log 'warn'
repositories {
grailsPlugins()
grailsHome()
grailsCentral()
ebr() // SpringSource http://www.springsource.com/repository
}
dependencies {
runtime('org.springframework.security:org.springframework.security.core:3.0.3.RELEASE') {
excludes 'com.springsource.org.aopalliance',
'com.springsource.org.apache.commons.logging',
'org.springframework.beans',
'org.springframework.context',
'org.springframework.core'
}
runtime('org.springframework.security:org.springframework.security.config:3.0.3.RELEASE')
runtime('org.springframework.security:org.springframework.security.web:3.0.3.RELEASE') {
excludes 'com.springsource.javax.servlet',
'com.springsource.org.aopalliance',
'com.springsource.org.apache.commons.logging',
'org.springframework.aop',
'org.springframework.beans',
'org.springframework.context',
'org.springframework.core',
'org.springframework.web'
}
}
}
精彩评论