SpringSecurity小结

131 阅读1分钟

为什么是Spring Security

答:企业级安全访问控制框架,提供了一组可以在Spring应用上下文配置的Bean

web.xml配置了什么

答:

1.  spring用contextConfigLocation加载xml文件,没有这个参数,默认加载WEB-INF下的applicationContext.xml
  	<context-param>
	     <param-name>contextConfigLocation</param-name>
    	    <param-value>classpath:spring-security.xml</param-value>
        </context-param>
2.  配置Spring监听器
	 <listener>
	    <listener-class>
		    org.springframework.web.context.ContextLoaderListener
	    </listener-class>
        </listener>
3.  Spring自带代理过滤器,请求都将进过DelegatingFilterProxy,这个bean是通过对xsd解析得到的,核心方法是createFilterChain(),可以添加自定义过滤器,默认过滤器
    <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.xml配置了什么

答:

1.  <!-- 页面拦截规则 -->
<http use-expressions="false">
        <!--/*表示该目录下资源,/**表示当前和所有级别子目录的资源-->
	<intercept-url pattern="/**" access="ROLE_USER" />
	<!--支持表单登陆-->	
	<form-login 
	    login-page="/login.html" 
	    default-target-url="/index.html" 
	    authentication-failure-url="/login_error.html"
	</form-login>
</http>

<!---关闭csrf跨域保护->
    <csrf disabled="true"/>
    
<!-- 以下页面不被拦截 -->
<http pattern="/login.html" security="none"></http>
<http pattern="/login_error.html" security="none"></http>
    
    PS:没有设置登录页security="none",首页会被反复重定向,无法打开
    
    
<!-- 认证管理器 -->
<authentication-manager>
	<authentication-provider>
		<user-service>
			<user name="admin" password="123456" authorities="ROLE_USER"/>
		</user-service>		
	</authentication-provider>	
</authentication-manager>