Spring Security 学习系列(2) - Spring Security 配置

以下是一段巨简单的web.xml的配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Demo</display-name>
</web-app>

若是想在你的系统里面用上Spring, 你会在web.xml里面添上一个context loader listener, 从而使得Servlet容器启动时将Spring环境启动并绑定到了Servlet 上下文环境。添加了该listener之后的web.xml配置如下:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Demo</display-name>
	
<!-- - Loads the root application context of this web app at startup. -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

这样配置好后呢, 在Servlet下下文绑定一个空的Spring 环境。为什么是空的Spring环境呢, 因为这里面没有定义一个Spring Bean声明文件,这样绑定的Spring环境就只能是空的了。

所以还得再加一个contextConfigLocation的声明, 表示web应用启动时, Spring 环境绑定到了Servlet 上下文环境, 并且, Spring环境中的Bean声明, 来自classpath:core-config.xml

扫描二维码关注公众号,回复: 668446 查看本文章
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Demo</display-name>

	<!-- - Location of the XML file that defines the root application context 
		- Applied by ContextLoaderListener. -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:core-config.xml
		</param-value>
	</context-param>


	<!-- - Loads the root application context of this web app at startup. -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

其实以上的这些都只是把web应用的Spring环境配置了一把, 我们至今还没有看到Spring Security的影子。

Spring Security 的配置跟它的定义一样, 我们只需要web.xml里面配置一个Filter就可以了。 当然, 最好是把security的配置放置到另一个配置文件, 所以也请注意一下下面的web.xml里面已经配置了两个配置文件了。

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Demo</display-name>

	<!-- - Location of the XML file that defines the root application context 
		- Applied by ContextLoaderListener. -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:core-config.xml
			classpath:security-config.xml
		</param-value>
	</context-param>

	<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>


	<!-- - Loads the root application context of this web app at startup. -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>
 

整个Spring Security 的环境就配置完成, 接下来我们要讲的是core-config.xml和security-config.xml具体需要添些什么内容。

猜你喜欢

转载自startfromheart.iteye.com/blog/1678984
今日推荐