(一)关于spring security的简要介绍以及相关配置和jar包认识

               Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

               安全主要包括两个操作,"认证和验证(权限控制)",现在是spring security面向的两个主要方向,认证” 是为用户建立一个他所声明的主体的过程, (主体”一般是指用户,设备或可以在你系统中执行行动的其他系统)。 验证”指的一个用户能否在你的应用中执行某个操作。              

              在身份验证层面,Spring Security广泛支持各种身份验证模式。 这些验证模型绝大多数都由第三方提供,或正在开发的有关标准机构提供的,例如Internet Engineering Task Force。 作为补充,Spring Security也提供了自己的一套验证功能。 Spring Security目前支持认证一体化和如下认证技术:

  • HTTP BASIC authentication headers (一个基于IETF RFC的标准)

  • HTTP Digest authentication headers (一个基于IETF RFC的标准)

  • HTTP X.509 client certificate exchange (一个基于IETF RFC的标准)

  • LDAP (一个非常常见的跨平台认证需要做法,特别是在大环境)

  • Form-based authentication (提供简单用户接口的需求)

  • OpenID authentication

  • 基于预先建立的请求头进行认证 (比如Computer Associates Siteminder)

  • JA-SIG Central Authentication Service (也被称为CAS,这是一个流行的开源单点登录系统)

  • Transparent authentication context propagation for Remote Method Invocation (RMI) and HttpInvoker (一个Spring远程调用协议)

  • Automatic "remember-me" authentication (这样你可以设置一段时间,避免在一段时间内还需要重新验证)

  • Anonymous authentication (允许未认证的任何调用,自动假设一个特定的安全主体)

  • Run-as authentication (这在一个会话内使用不同安全身份的时候是非常有用的)

  • Java Authentication and Authorization Service (JAAS)

  • JEE Container autentication (这样,你可以继续使用容器管理认证,如果想的话)

  • Kerberos

  • Java Open Source Single Sign On (JOSSO) *

  • OpenNMS Network Management Platform *

  • AppFuse *

  • AndroMDA *

  • Mule ESB *

  • Direct Web Request (DWR) *

  • Grails *

  • Tapestry *

  • JTrac *

  • Jasypt *

  • Roller *

  • Elastic Plath *

  • Atlassian Crowd *

  • 你自己的认证系统(向下看)

(* 是指由第三方提供。)

许多独立软件供应商(ISVs, independent software vendors)采用Spring Security,是因为它拥有丰富灵活的验证模型。 这样,无论终端用户需要什么,他们都可以快速集成到系统中,不用花很多功夫,也不用让用户改变运行环境。 如果上述的验证机制都没有满足你的需要,Spring Security是一个开放的平台,编写自己的验证机制是十分简单的。 Spring Security的许多企业用户需要整合不遵循任何特定安全标准的“遗留”系统,Spring Security在这类系统上也表现的很好。

Spring Security不仅提供认证功能,也提供了完备的授权功能。 在授权方面主要有三个领域,授权web请求,授权被调用方法,授权访问单个对象的实例。 为了帮你了解它们之间的区别,对照考虑授在Servlet规范web模式安全,EJB容器管理安全,和文件系统安全方面的授权方式。 Spring Security在所有这些重要领域都提供了完备的能力

           好了,简述到这里下面主要介绍一下spring security相关的一些jar包: 

1.4.1.1. Core - spring-security-core.jar

包含了核心认证和权限控制类和接口, 运程支持和基本供应API。使用Spring Security所必须的。支持单独运行的应用, 远程客户端,方法(服务层)安全和JDBC用户供应。包含顶级包:

  • org.springframework.security.core

  • org.springframework.security.access

  • org.springframework.security.authentication

  • org.springframework.security.provisioning

1.4.1.2. 远程调用 - spring-security-remoting.jar

提供与Spring远程调用的集成。你不需要它,除非你写了一个远程客户端 使用Spring Remoting。 主包是 org.springframework.security.remoting

1.4.1.3. Web - spring-security-web.jar

包含过滤器和对应的web安全架构代码。任何需要依赖servlet API的。 你将需要它,如果你需要Spring Security Web认证服务和基于URL的权限控制。 主包是org.springframework.security.web

1.4.1.4. Config - spring-security-config.jar

包含安全命名控制解析代码,你需要它, 如果使用了Spring Security XML命名控制来进行配置。主包是 org.springframework.security.config。这些类都不应该在应用中直接使用。

1.4.1.5. LDAP - spring-security-ldap.jar

LDAP认证和实现代码,如果你需要使用LDAP认证或管理LDAP用户实体就是必须的。 顶级包是org.springframework.security.ldap

1.4.1.6. ACL - spring-security-acl.jar

处理领域对象ACL实现。用来提供安全给特定的领域对象实例,在你的应用中。 顶级包是org.springframework.security.acls

1.4.1.7. CAS - spring-security-cas.jar

Spring Security的CAs客户端集成。如果你希望使用Spring Security web认证 整合一个CAS单点登录服务器。顶级包是 org.springframework.security.cas

1.4.1.8. OpenID - spring-security-openid.jar

OpenID web认证支持。用来认证用户,通过一个外部的OpenID服务。 org.springframework.security.openid。需要OpenID4Java。

           在pom.xml中的配置如下请看:          

 1 <dependency>
 2 <groupId>org.springframework.security</groupId>
 3 <artifactId>spring-security-web</artifactId>
 4 <version>4.1.2.RELEASE</version>
 5 </dependency>
 6 <dependency>
 7 <groupId>org.springframework.security</groupId>
 8 <artifactId>spring-security-config</artifactId>
 9 <version>4.1.2.RELEASE</version>
10 </dependency>
11 <dependency>
12 <groupId>org.springframework.security</groupId>
13 <artifactId>spring-security-taglibs</artifactId>
14 <version>4.1.2.RELEASE</version>
15 </dependency>

         spring security在web.xml中的配置:

1 <!-- Spring Secutiry4.1的过滤器链配置 -->
2 <filter>
3 <filter-name>springSecurityFilterChain</filter-name>
4 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
5 </filter>
6 <filter-mapping>
7 <filter-name>springSecurityFilterChain</filter-name>
8 <url-pattern>/*</url-pattern>
9 </filter-mapping>

   

         spring security.xml配置如下:

<?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:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
          http://www.springframework.org/schema/beans/spring-beans.xsd  
          http://www.springframework.org/schema/context  
          http://www.springframework.org/schema/context/spring-context.xsd  
          http://www.springframework.org/schema/security  
          http://www.springframework.org/schema/security/spring-security.xsd">

           
         <http pattern="/**/js/**/*" security="none"></http>
    <http pattern="/**/css/**/*" security="none"></http>
    <http pattern="/**/img/**/*" security="none"></http>
    <http pattern="/plugins/**/*" security="none"></http>
    <http pattern="/bootstrap/**/*" security="none"></http>
    <http pattern="/images/**/*" security="none"></http>
    <http pattern="/upload/**/*.*" security="none"></http>


        <http auto-config="false" use-expressions="true" entry-point-ref="myAuthenticationEntryPoint" >  


        <!--配置自定义的过滤器-->
        <custom-filter before="FILTER_SECURITY_INTERCEPTOR" ref="securityInterceptor"/>
 
         <!-- 退出处理 -->  
         <logout logout-url="/j_spring_security_logout"   
                logout-success-url="/loginlog/logout.do"   
                invalidate-session="true"  />  
        <custom-filter ref="loginFilter" position="FORM_LOGIN_FILTER" />
        <!-- 登录成功后拒绝访问跳转的页面 -->         
        <access-denied-handler ref="accessDeniedHandler" />  
        <csrf disabled="true" />  
    </http>
         

        <!-- 自定义权限不足处理程序 -->
    <beans:bean id="accessDeniedHandler"
        class="com.seawin.webapp.base.security.MyAccessDeniedHandler">
        <beans:property name="errorPage" value="/sysadmin/error.jsp?errorCode=1"></beans:property>
    </beans:bean>
  
    <!-- 使用自定义类myUserDetailsService从数据库获取用户信息 -->  
    <authentication-manager alias="myAuthenticationManager">    
        <authentication-provider user-service-ref="myUserDetailsService"></authentication-provider>  
    </authentication-manager>  
    
    <!-- 自定义用户资源信息获取 -->
    <beans:bean id="myUserDetailsService"
        class="com.seawin.webapp.base.security.MyUserDetailService">
    </beans:bean>
      
    <!-- 被认证请求根据所需权限跳转到不同的登录界面 -->  
    <beans:bean id="myAuthenticationEntryPoint"   
        class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">  
        <beans:constructor-arg name="loginFormUrl" value="/login.html"></beans:constructor-arg>  
    </beans:bean>  
    
    <!-- 验证成功后操作 -->
    <beans:bean id="authenticationSuccessHandler" class="com.seawin.webapp.base.security.MySimpleUrlAuthenticationSuccessHandler" />

    <!-- 验证失败后操作 -->
    <beans:bean id="authenticationFailureHandler"
        class="com.seawin.webapp.base.security.MySimpleUrlAuthenticationFailureHandler">
        <beans:property name="defaultFailureUrl" value="/login.html" />
    </beans:bean>
    
    <!-- 自定义登陆拦截器 -->
    <beans:bean id="loginFilter"
        class="com.seawin.webapp.base.security.MyAuthenticationFilter">
        <beans:property name="authenticationManager" ref="myAuthenticationManager" />
        <beans:property name="filterProcessesUrl" value="/j_spring_security_check" />
         <!-- 认证成功用自定义类authenticationSuccessHandler处理 -->  
        <beans:property name="authenticationSuccessHandler" ref="authenticationSuccessHandler"/>
        <beans:property name="authenticationFailureHandler" ref="authenticationFailureHandler"/>
    </beans:bean>
    
    <!-- 自定义拦截器 -->
    <beans:bean id="securityInterceptor" class="com.seawin.webapp.base.security.MyAuthorizationFilter">  
        <beans:property name="authenticationManager" ref="myAuthenticationManager"/>  
        <beans:property name="accessDecisionManager" ref="mysecurityAccessDecisionManager"/>  
        <beans:property name="securityMetadataSource" ref="secureResourceFilterInvocationDefinitionSource" />  
    </beans:bean> 
    
    <beans:bean id="mysecurityAccessDecisionManager" class="com.seawin.webapp.base.security.MyAccessDecisionManager" /> 

    <beans:bean id="secureResourceFilterInvocationDefinitionSource" class="com.seawin.webapp.base.security.MySecurityMetadataSource" />

</beans:beans> 
View Code

     

原文地址:https://www.cnblogs.com/jbml-154312/p/7298412.html