spring security例子

场景:一个容易的Spring Security例子

一个简单的Spring Security例子

开始参考family168的Spring Security权限管理手册学习Spring Security,作为自己学习权限管理的一个入门。

文档地址为:http://www.family168.com/oa/springsecurity/html/index.html

1.首先需要在web.xml中配置一个fitter,名为springSecurityFilterChain

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

 

这里的fitter名字是不能更改的

2.ApplicationContext-security.xml

	<!-- http部分配置如何拦截用户请求,auto-config=true将自动配置几种常用的权限控制,包括form,anonymous, remberMe -->
	<http auto-config="true">
		<!-- 利用intercept-url判断用户需要具有何种全新才能访问对应的url资源
			可以在pattern中指定一个特定的url资源,也可以使用通配符指定一组类似的url资源,
			在实际使用中,Spring Security采用的是一种就近原则,就是说当用户访问的url资源
			满足多个intercepter-url时,系统将使用第一个符合条件的intercept-url进行权限控制。
			access指定的权限部分比较有趣,大家可以注意到这些权限标示符都是以ROLE_开头的,实
			际上这与Spring Security中的Voter机制有着千丝万缕的联系,只有包含了特定前缀的字符
			串才会被Spring Security处理。
		 -->
		<intercept-url pattern="/admin.jsp" access="ROLE_ADMIN" />
		<intercept-url pattern="/**" access="ROLE_USER"/>
	</http>
	
	<authentication-provider>
		<user-service>
			<user password="admin" name="admin" authorities="ROLE_USER, ROLE_ADMIN"/>
			<user password="user" name="user" authorities="ROLE_USER"/>
		</user-service>
	</authentication-provider>