step1: 在web.xml中添加spring security的代理filter。
<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>
<beans:beans xmlns="http://www.springframework.org/schema/security" 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-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> <http> <intercept-url pattern="/secure/extreme/**" access="ROLE_SUPERVISOR" /> <intercept-url pattern="/secure/**" access="IS_AUTHENTICATED_FULLY" /> <intercept-url pattern="/login.htm" access="IS_AUTHENTICATED_ANONYMOUSLY" /> <intercept-url pattern="/images/*" filters="none" /> <intercept-url pattern="/**" access="ROLE_USER" /> <form-login login-page="/login.htm" default-target-url="/home.htm" /> <logout logout-success-url="/logged_out.htm" /> </http> <authentication-manager> <authentication-provider> <password-encoder hash="md5"/> <user-service> <user name="bob" password="12b141f35d58b8b3a46eea65e6ac179e" authorities="ROLE_SUPERVISOR, ROLE_USER" /> <user name="sam" password="d1a5e26d0558c455d386085fad77d427" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> </beans:beans>好了,这是一个来自spring官网的例子,我们居然没写代码!好吧,我们看看这些配置都干了些什么.
好吧,我们开始抽丝剥茧,一点一点的讲明白这件事情。
<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>spring security 对web的保护是通过过滤器完成的,没错,就是你想到的那个“javax.servlet.Filter”。
<beans:beans xmlns="http://www.springframework.org/schema/security" 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-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> <http> <intercept-url pattern="/secure/extreme/**" access="ROLE_SUPERVISOR" /> <intercept-url pattern="/secure/**" access="IS_AUTHENTICATED_FULLY" /> <intercept-url pattern="/login.htm" access="IS_AUTHENTICATED_ANONYMOUSLY" /> <intercept-url pattern="/images/*" filters="none" /> <intercept-url pattern="/**" access="ROLE_USER" /> <form-login login-page="/login.htm" default-target-url="/home.htm" /> <logout logout-success-url="/logged_out.htm" /> </http> <authentication-manager> <authentication-provider> <password-encoder hash="md5"/> <user-service> <user name="bob" password="12b141f35d58b8b3a46eea65e6ac179e" authorities="ROLE_SUPERVISOR, ROLE_USER" /> <user name="sam" password="d1a5e26d0558c455d386085fad77d427" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager>配置很简单,因为在spring security3.0中引入了命名空间,也就是说使用了命名空间框架就会帮你做大量的默认工作,好处就是配置简单,坏处就是细节
<alias name="filterChainProxy" alias="springSecurityFilterChain"/> <bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy"> <sec:filter-chain-map path-type="ant"> <sec:filter-chain pattern="/images/*" filters="none"/> <sec:filter-chain pattern="/**" filters="securityContextFilter, logoutFilter, formLoginFilter, requestCacheFilter,servletApiFilter, anonFilter, sessionMgmtFilter, exceptionTranslator, filterSecurityInterceptor" /> </sec:filter-chain-map> </bean><http>首先建立了"filterChainProxy"的bean并且为每一个url配上了默认的filter链,然后将"filterChainProxy"起了个"springSecurityFilterChain"的别名,很眼熟吧,就是在web.xml中配置的那个filter名。有一个来自spring官方网站的图可以清楚的看到<http>都内置了哪些filter:这些filter有的是可选的,有的是必须的。
讲到这基本上都差不多了,最后再说一点,spring security提供了一个JSP标签库来方便在JSP页面中最安全信息进行获取。感兴趣可以自己看看,这里不详细说明了。
写的比较乱,错误之处请不吝指出,谢谢。