Spring Security安全认证框架

一、Spring Security 简介

官网 https://spring.io/projects/spring-security

Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。( 前身是 Acegi Security )由于它是Spring生态系统中的一员,因此它伴随着整个Spring生态环境不断修正、升级,在Spring boot项目中加入Spring security更是十分简单,使用Spring security减少了企业系统安全控制编写大量重复代码的工作。

Spring Security的核心功能:

  • 认证:(authentication):认证是建立主体(principal)的过程。“主体”通常是指可以在你的应用程序中执行操作的用户,设备或者其他系统(比如两个服务之间的通信,也是需要认证的一个过程)
  • 授权:或者可以称为:访问控制(access-control),“授权”,指决定是否允许主体在应用程序中执行操作

Spring Security相关概念:

  • 权限(Permission) = 资源(Resource) + 操作(Privilege)
  • 角色(Role) = 权限的集合(a set of low-level permissions)
  • 用户(User) = 角色的集合(high-level roles)

Spring Security 目前支持认证一体化如下认证技术:

  • HTTP BASIC authentication headers (一个基于 IEFT RFC 的标准)
  • HTTP Digest authentication headers (一个基于 IEFT RFC 的标准)
  • HTTP X.509 client certificate exchange(一个基于 IEFT RFC 的标准)
  • LDAP (一个非常常见的跨平台认证需要做法,特别是在大环境)
  • Form-based authentication (提供简单用户接口的需求)
  • OpenID authentication
  • Computer Associates Siteminder JA-SIG Central Authentication Service
    (CAS,这是一个流行的开源单点登录系统)
  • Transparent authentication context propagation for Remote Method Invocation and HttpInvoker (一个 Spring 远程调用协议)

二、Spring Security 下载

jar包下载:

三、Spring Security快速入门(SSM)

1. 导入依赖
    <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
2. 配置 Spring Security

添加spring-security配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:security="http://www.springframework.org/schema/security"
       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.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security.xsd">

    <!--开启三种权限控制方式-->
    <security:global-method-security  
    		pre-post-annotations="enabled"   <!--支持表达式的注解-->     
   			jsr250-annotations="enabled"    <!--JSR-250注解-->        
    		secured-annotations="enabled">    <!--security框架自带的注解-->   
    </security:global-method-security>

    <!-- 配置不拦截的资源(静态资源及登录相关)-->
    <security:http pattern="/login.jsp" security="none"/>
    <security:http pattern="/failer.jsp" security="none"/>
    <security:http pattern="/css/**" security="none"/>
    <security:http pattern="/img/**" security="none"/>
    <security:http pattern="/plugins/**" security="none"/>
    <!--
        配置具体的规则
        auto-config="true"    不用自己编写登录的页面,框架提供默认登录页面
        use-expressions="false"    是否使用SPEL表达式(没学习过)
    -->
    <security:http auto-config="true" use-expressions="false">
        <!-- 配置具体的拦截的规则 pattern="请求路径的规则" access="访问系统的人,必须有ROLE_USER、Role_ADMIN的角色" -->
        <security:intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN"/>

        <!-- 定义跳转的具体的页面 -->
        <!--login-page  自定义登陆页面-->
        <!--login-processing-url  登录请求拦截的url,也就是form表单提交时指定的action-->
        <!--default-target-url  默认登录成功后跳转的页面-->
        <!--authentication-failure-url  用户权限校验失败之后才会跳转到这个页面-->
        <!--authentication-success-forward-url 用户权限校验成功之后会跳转的页面-->
        <security:form-login
                login-page="/login.jsp"
                login-processing-url="/login.do"
                default-target-url="/index.jsp"
                authentication-failure-url="/failer.jsp"
                authentication-success-forward-url="/pages/main.jsp"
        />

        <!-- 关闭跨域请求 -->
        <security:csrf disabled="true"/>
        <!-- 退出,只要路径中有/logout.do,就可以直接点击退出 -->
        <security:logout invalidate-session="true" logout-url="/logout.do" logout-success-url="/login.jsp" />

    </security:http>

     <!--切换成数据库中的用户名和密码-->
    <security:authentication-manager>
        <security:authentication-provider user-service-ref="userService">  <!--调用指定的service-->
            <!-- 配置加密的方式-->
            <!--<security:password-encoder ref="passwordEncoder"/>-->
        </security:authentication-provider>
    </security:authentication-manager>

    <!-- 配置加密类 -->
    <bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

</beans>

若使用JSR-250注解,则还需导入一个依赖:

<dependency>
     <groupId>javax.annotation</groupId>
     <artifactId>jsr250-api</artifactId>
     <version>1.0</version>
</dependency>

注解:

在xml中的<security:password-encoder ref=“passwordEncoder”/>,其作用是当注册用户时密码采用了security自带的加密类进行加密存入数据库中后,当我们需要登录时,则自动会将输入的密码转换成密文,便于和数据库中的密码密文进行比对。这里对该句进行注释,是因为数据库中存的密码没有加密,登陆输入的密码可直接和数据库的密码进行核对。

3. 配置过滤器

在 web.xml 中配置 Spring Security 的过滤器及 spring-security.xml 的加载信息。

<!-- 配置加载类路径的配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
        classpath:spring-security.xml
    </param-value>
  </context-param>

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

注意: service层接口要实现UserDetailsService 类

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值