SpringSecurity---简介以及HelloWorld环境搭建(所需资料文末免费下载)

第一章 SpringSecurity-简介

1.简介

https://docs.spring.io/spring-security/site/docs/4.2.10.RELEASE/guides/html5/helloworld-xml.html

SpringSecurity融合Spring技术栈,提供JavaEE应     用的整体安全解决方案

Spring Security为基于Java EE的企业软件应用提供全面的安全服务

Spring Security只需要少量配置,就能构建一个强大的安全的应用系统。 

目前市面上受欢迎的两个安全框架:Apache Shiro、SpringSecurity;

SpringSecurity可以无缝整合Spring应用,具有强大的自动化web安全管控功能。而Shiro是一个轻量级强大的安全框架,可以脱离web应用来提供安全管控,但是对于web的一些定制安全需要手动编写;SpringBoot底层默认整合SpringSecurity作为安全框架,所以我们推荐web应用使用SpringSecurity来控制安全;

2.概念

 authentication:身份验证

“身份验证”是指建立主体(principal)的过程,主体就是他们声称是谁(“主体”通常指用户设备或在应用程序中可以执行动作的其他系统)。也就是“证明你是谁

authorization:授权

“授权”是指确定主体(principal是否被允许执行系统中某个动作的过程。 也就是“你能做什么!”

为了达到“授权”决策(安全框架决定你是否有权限做此事),“身份验证”(authentication)过程已经建立了主体的身份(Principal)

3.文档

Hello Spring Security <https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/javaconfig/helloworld > 基于Java配置整合示例

Hello Spring Security Boot <https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/boot/helloworld> 与SpringBoot整合案例

Hello Spring Security XML <https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/xml/helloworld> 基于XML方式整合示例

Hello Spring MVC Security <https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/javaconfig/hellomvc> SpringMVC集成示例

Custom Login Form <https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/javaconfig/form> 自定义登录表单示例

4.支持的身份认证模式

在身份验证级别,Spring Security支持广泛的认证模型。这些认证模型中的大部分要么由第三方提供,要么由相关标准机构(如互联网工程任务组)开发。此外,Spring Security提供了自己的一套身份验证功能。具体而言,Spring Security当前支持与所有这些技术的身份验证集成;

1.HTTP BASIC身份验证标头

HTTP BASIC authentication headers (an IETF RFC-based standard)

参考:https://blog.****.net/lvxinzhi/article/details/49000003

2.HTTP Digest身份验证标头

HTTP Digest authentication headers (an IETF RFC-based standard)

3.HTTP X.509客户端证书交换

HTTP X.509 client certificate exchange (an IETF RFC-based standard)

4.LDAP(一种非常常见的跨平台身份验证方法,特别是在大型环境中)

LDAP (a very common approach to cross-platform authentication needs, especially in large environments)

5.基于表单的身份验证(用于简单的用户界面需求)

Form-based authentication (for simple user interface needs)

6.OpenID身份验证

OpenID authentication   第三方登陆如:微信,qq。

7.基于预先建立的请求标头的身份验证

Authentication based on pre-established request headers (such as Computer Associates Siteminder)

8.Jasig中央认证服务(也称为CAS,是一种流行的开源单点登录系统)

Jasig Central Authentication Service (otherwise known as CAS, which is a popular open source single sign-on system)

9.远程方法调用(RMI)和HttpInvoker(Spring远程协议)的透明身份验证上下文传播

Transparent authentication context propagation for Remote Method Invocation (RMI) and HttpInvoker (a Spring remoting protocol)

10.自动“记住我”身份验证

Automatic "remember-me" authentication (so you can tick a box to avoid re-authentication for a predetermined period of time)

11.匿名身份验证(允许每个未经身份验证自动承担特定的安全身份)

Anonymous authentication (allowing every unauthenticated call to automatically assume a particular security identity)

12.Runas身份验证(如果一个调用应继续使用不同的安全标识,则非常有用)

Run-as authentication (which is useful if one call should proceed with a different security identity)

13.Java身份验证和授权服务(JAAS)

Java Authentication and Authorization Service (JAAS)

14.JavaEE容器身份验证(如果需要,您仍然可以使用容器管理身份验证)

Java EE container authentication (so you can still use Container Managed Authentication if desired)

15.Java开源单点登录(JOSSO

Java Open Source Single Sign-On (JOSSO) *

16.OpenNMS网络管理平台*

OpenNMS Network Management Platform *

17.您自己的身份验证系统

Your own authentication systems (see below)

18.其他

Kerberos

AppFuse *

AndroMDA *

Mule ESB *

Direct Web Request (DWR) *

Grails *

Tapestry *

JTrac *

Jasypt *

Roller *

Elastic Path *

Atlassian Crowd

5.模块划分

Core - spring-security-core.jar

核心模块

核心认证、授权功能、支持jdbc-user功能、支持独立的Spring应用

Remoting - spring-security-remoting.jar

远程交互模块

一般不需要,可以使用Spring Remoting功能简化远程客户端交互

Web - spring-security-web.jar

web安全模块

web项目使用,基于URL的访问控制(access-control)

Config - spring-security-config.jar

java配置模块

必须依赖包,包含解析xml方式和java 注解方式来使用SpringSecurity功能

LDAP - spring-security-ldap.jar

ldap(轻量目录访问协议)支持模块

可选依赖包,LDAP功能支持

ACL - spring-security-acl.jar

ACL支持

ACLAccess-Control-List访问控制列表

细粒度的资源访问控制(RBAC+ACL)

CAS - spring-security-cas.jar

CAS整合支持

CASCentral Authentication Service)中央认证服务。开源ApereoCAS整合

OpenID - spring-security-openid.jar

OpenID认证方式支持

OpenID Web身份验证支持。 用于针对外部OpenID服务器对用户进行身份验证

(微信,qq,新浪微博等第三方登录)

Test - spring-security-test.jar

测试模块

快速的测试SpringSecurity应用

 

6.4种使用方式

  1. 一种是全部利用配置文件,将用户、权限、资源(url)硬编码在xml文件中
  2. 二种是用户和权限用数据库存储,而资源(url)和权限的对应采用硬编码配置
  3. 三种是细分角色和权限,并将用户、角色、权限和资源均采用数据库存储,并且自定义过滤器,代替原有的FilterSecurityInterceptor过滤器, 并分别实现AccessDecisionManagerInvocationSecurityMetadataSourceServiceUserDetailsService,并在配置文件中进行相应配置。
  4. 四是修改springsecurity的源代码,主要是修改InvocationSecurityMetadataSourceService和UserDetailsService两个类。
    1. InvocationSecurityMetadataSourceService
      1. 将配置文件或数据库中存储的资源(url)提取出来加工成为url和权限列表的Map供Security使用
    2. UserDetailsService
      1. 提取用户名和权限组成一个完整的(UserDetails)User对象,该对象可以提供用户的详细信息供AuthentationManager进行认证与授权使用

第二章 SpringSecurity-HelloWorld

2.1 测试环境搭建

2.1.1 创建普通maven-war工程:spring-security-helloworld

  1. pom文件增加依赖

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-webmvc</artifactId>

                     <version>4.3.20.RELEASE</version>

              </dependency>

              <dependency>

                     <groupId>javax.servlet.jsp</groupId>

                     <artifactId>jsp-api</artifactId>

                     <version>2.2</version>

                     <scope>provided</scope>

              </dependency>

              <dependency>

                     <groupId>javax.servlet</groupId>

                     <artifactId>servlet-api</artifactId>

                     <version>2.5</version>

                     <scope>provided</scope>

              </dependency>

              <dependency>

                     <groupId>javax.servlet</groupId>

                     <artifactId>jstl</artifactId>

                     <version>1.2</version>

              </dependency>

2.1.2 web.xml配置

<servlet>

<servlet-name>springDispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springDispatcherServlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

2..1.3 spring配置:spring.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

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-4.3.xsd">

<context:component-scan base-package="com.atguigu.security"></context:component-scan>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/views/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

<mvc:annotation-driven />

<mvc:default-servlet-handler />

</beans>

2.1.4 导入实验资源

  1. 导入页面

SpringSecurity---简介以及HelloWorld环境搭建(所需资料文末免费下载)

  1. 导入controller

SpringSecurity---简介以及HelloWorld环境搭建(所需资料文末免费下载)

2.1.5 运行测试

SpringSecurity---简介以及HelloWorld环境搭建(所需资料文末免费下载)2.2 引入SpringSecurity框架

2.2.1 添加security-pom依赖

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-web</artifactId>

<version>4.2.10.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-config</artifactId>

<version>4.2.10.RELEASE</version>

</dependency>

<!-- 标签库 -->

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-taglibs</artifactId>

<version>4.2.10.RELEASE</version>

</dependency>

2.2.2 web.xml中添加SpringSecurity的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>

2.2.3 加入SpringSecurity配置类

  1. @Configuration@Bean 注解作用

@Configuration

@EnableWebSecurity

public class AppWebSecurityConfig extends WebSecurityConfigurerAdapter {

}

 

2.2.4 启动测试效果

  1. 所有资源访问受限(包括静态资源)
  2. 需要一个默认的登录页面(框架自带的)
  3. 账号密码错误会有提示
  4. 查看登录页面的源码,发现有个hidden-input;name="_csrf" 这是SpringSecurity帮我们防止“跨站请求伪造”攻击;还可以防止表单重复提交
  5. 登陆失败后跳转到框架自带的登陆界面,并有失败信息报出。

http://localhost:8080/spring-security-helloworld/login?error

SpringSecurity---简介以及HelloWorld环境搭建(所需资料文末免费下载)

 

SpringSecurity---简介以及HelloWorld环境搭建(所需资料文末免费下载)

 

导入的模板已经上传到****,供大家学习时免费下载,下载后导入即可。

https://download.****.net/download/m2606707610/12122449