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

 

 

<!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具体需要添些什么内容。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值