Spring Security 从配置入门 学习讲解

首先,Spring Security 到底是用来干嘛的?

再次,Security 是怎么工作的?

 

最后,我们为什么要用Security。

--------------------------------------------------------------------------

我先大体上说下security的工作流程:

   Spring Security对Web安全性的支持大量地依赖于Servlet过滤器。这些过滤器拦截进入请求,并且在应用程序处理该请求之前进行某些安全处理。 Spring Security提供有若干个过滤器,它们能够拦截Servlet请求,并将这些请求转给认证和访问决策管理器处理,从而增强安全性。根据自己的需要,可以使用表7.4中所列的几个过滤器来保护自己的应用程序。 http://baike.baidu.com/link?url=LhguUpz1g7MnakDzFDFRK9D7n6u6wFffzSbJ7Zkcq3QMDNy741SpXMVGAb4jfz_GAa5J0ORkYvKEGYOD2bIQsa
对于概念,百度百科上的说很明了。
  在我们访问一个资源的之前,会被AuthenticationProcessingFilter(这个过滤器我们会重写)拦截,然后它会调用securityMetadataSource来获取被访问资源所对应的权限集合,然后调用accessDecisionManager 来确认,我们用户是否有权限访问这个资源。
 
---------------------------说了这么多 ,还没见代码。-------------------------------
每个项目的访问入口都是 web.xml,那我们就看他的代码:
 
复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringSecurityDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 加载Spring security XML 配置文件
      为什么要配置这个文件?!!!
      因为百度! security是什么?  就是依靠一个特殊的过滤器(DelegatingFilterProxy,他是干嘛的?!),
      依靠他来委托一个在spring上下文的一个bean完成工作。
      而这个Bean,说白了就是一个过滤器。
   -->
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/securityConfig.xml
        </param-value>
    </context-param>
    
    
  <!-- Spring 容器监听,这尼玛又是个什么东东? 
      listener. 哦 ,是个监听器啊。
      看看他的源码
      public void contextInitialized(ServletContextEvent event)
    {
        contextLoader = createContextLoader();
        if(contextLoader == null)
            contextLoader = this;
        contextLoader.initWebApplicationContext(event.getServletContext());
    }
        瞅见了么,就特么是来加载上面配置的securityConfig.xml文件
   -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 看吧,这就是看个特殊的过滤器,撒手掌柜了,他到底干什么了,为什么我们要配置他?
      我们来扒开他的皮,瞅一瞅吧,
      //这是他的doFilter方法
       public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws ServletException, IOException
    {
        Filter delegateToUse = null;
        synchronized(delegateMonitor)
        {
            if(_flddelegate == null)   //如果bean为空
            {
                WebApplicationContext wac = findWebApplicationContext();  //给我拿来配置文件
                if(wac == null)            //虾米?配置文件为空?!  妈的,给老子抛异常! 你给老子监听的配置吃啦?!
                    throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
                _flddelegate = initDelegate(wac);   //这是什么?猜都出来了,在配置文件里召唤bean(感情配置文件就是人才市场,掌柜的招小工了..)
            }
            delegateToUse = _flddelegate; //小工招用完了,就你啦...可怜,三方呢? 工资了?  你他妈的刚毕业吧....
        }
        invokeDelegate(delegateToUse, request, response, filterChain); //照完小工干嘛去?  干活呗!,给老子干! 干!
    }
    //这是initDeletegate
    protected Filter initDelegate(WebApplicationContext wac)
        throws ServletException
    {
        Filter delegate = (Filter)wac.getBean(getTargetBeanName(), javax/servlet/Filter);
        if(isTargetFilterLifecycle())
            delegate.init(getFilterConfig());
        return delegate;
    }
  
    //这是invokeDelegate
  protected void invokeDelegate(Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws ServletException, IOException
    {
        delegate.doFilter(request, response, filterChain);
    }
    
    咦? 不对呀,小工要造反了怎么办?  你无良老板就啥都不干? 
    对,我就是啥都不干? 老子就是这么任性。 老子把你们召唤来,就是给了你们生存的价值
 (DelegatingFilterProxy做的事情是代理Filter的方法,从application context里获得bean。
       这让bean可以获得spring web application context的生命周期支持,使配置较为轻便。)
你们就知足吧!  唉...初来乍到,谁没有被老板坑过....
   -->
      <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>
</web-app>
复制代码

这篇我们要讲刽子手  securityConfig。 为什么要说他是刽子手呢?  因为他是无良掌柜的小工,直接的操盘手......

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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 access-denied-page = "/accessDenied.jsp">        <!-- 访问拒绝页面 -->
        <form-login login-page="/login.jsp"/>   <!-- 定义登陆界面 -->
        <intercept-url pattern="/login.jsp" filters="none"/>
        <session-management>
            <concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/>  <!-- 用户最大登录数设置为1 ,超过则引发异常 -->
        </session-management>                  
        <custom-filter ref="myFilter" before="FILTER_SECURITY_INTERCEPTOR"/>  <!-- 自定义FILTER ,FilterSecurityInterceptor 负责授权-->
    </http>
    <!-- myFilter -->
    <beans:bean id = "myFilter" class = "com.qbt.spring.security.MyFilterSecurityInterceptor">
            <beans:property name="authenticationManager" ref ="authenticationManager"></beans:property>  <!-- 登陆验证 ,验证你的用户名密码噼里啪啦-->
            <beans:property name="securityMetadataSource" ref = "securityMetadataSource"></beans:property>  <!-- 资源数据源的定义 ,神马权限对应神马资源 噼里啪啦-->
             <beans:property name="accessDecisionManager" ref="myAccessDecisionManagerBean"></beans:property>  <!-- 访问决策 有没有权限访问资源 噼里啪啦-->
    </beans:bean>                                                 
    
    <!-- 验证配置,认证管理器,实现UserDetailService接口 -->
    <!-- authenticationManager 可以有多个provider提供信息,我们用myUserDetailService获取信息 -->
    <!-- Spring Security中进行身份验证的是AuthenticationManager接口,ProviderManager是它的一个默认实现,
        但它并不用来处理身份认证,而是委托给配置好的AuthenticationProvider,每个AuthenticationProvider会轮流检查身份认证。
        检查后或者返回Authentication对象或者抛出异常 -->
    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="myUserDetailService"></authentication-provider>    
    </authentication-manager>
    
    <!-- 获取user数据,可以从数据库中获取用户密码,角色等! -->
    <beans:bean id = "myUserDetailService" class = "com.qbt.spring.security.MyUserDetailService"></beans:bean>
    
    <!-- 访问决策器,决定用户的角色,访问的权限 -->
    <beans:bean id = "myAccessDecisionManagerBean" class = "com.qbt.spring.security.MyAccessDecisionManager"></beans:bean>
    
    <!-- 资源数据源的定义 什么资源对应什么权限,或者什么资源能被什么角色访问-->
    <beans:bean id = "securityMetadataSource" class = "com.qbt.spring.security.MyInvocationSecurityMetadataSource"></beans:bean>
    
</beans:beans>
复制代码

 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值