好的,我来为你分别介绍计算机网络和多媒体的基础知识。
一、计算机网络基础知识
(一)网络的定义
计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统、网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
(二)网络的分类
- 按覆盖范围分类
- 局域网(LAN):覆盖范围较小,通常在几公里以内,如一个办公室、学校或企业的内部网络。它具有高速数据传输率(通常在100Mbps - 10Gbps甚至更高),并且连接设备数量有限。例如,企业内部的办公网络,员工可以通过局域网共享打印机、文件服务器等资源。
- 城域网(MAN):覆盖范围比局域网大,一般是一个城市范围内的网络。它主要用于连接多个局域网,起到一个中间层次的作用。比如,城市的有线电视网络和一些城市的宽带接入网络可以看作是城域网。
- 广域网(WAN):覆盖范围最广,可以是一个国家、一个洲甚至全球范围。Internet就是典型的广域网。广域网连接距离远,通常需要借助公共通信线路(如电话线、光纤等)来实现通信。它的数据传输速率相对局域网较低,但传输距离远,能够连接大量的计算机和网络设备。
- 按拓扑结构分类
- 总线型拓扑:所有计算机都连接到一条公共的总线(通信线路)上。它的优点是结构简单、易于扩充,但总线带宽是共享的,当连接的设备过多时,性能会下降。一旦总线出现故障,整个网络就会瘫痪。
- 星型拓扑:所有计算机都通过单独的线路连接到一个中心节点(如交换机或集线器)。中心节点负责转发数据,这种拓扑结构便于管理和维护,当某个节点出现故障时,不会影响其他节点。但是,中心节点的可靠性要求很高,如果中心节点故障,整个网络就会中断。
- 环型拓扑:计算机通过通信线路首尾相连形成一个闭合的环。数据在环中单向或双向传输,每个节点都负责转发数据。环型拓扑的优点是传输延迟固定,适合实时性要求较高的应用。不过,如果环中的某个节点或线路出现故障,整个环路通信就会中断。
- 网状拓扑:网络中各个节点通过通信线路相互连接,形成复杂的网络结构。它具有很高的可靠性,因为每个节点有多个连接路径。但是,这种拓扑结构的布线复杂,成本较高,主要用于大型的广域网。
(三)网络协议
网络协议是计算机网络中通信双方必须共同遵守的规则和约定。它是网络能够正常运行的基础。
- OSI参考模型
- 它是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互联的标准体系。OSI模型分为七层,从下到上分别是物理层、数据链路层、网络层、传输层、会话层、表示层和应用层。
- 物理层主要负责在物理媒体上传输原始的比特流,定义了物理连接的电气、机械、功能和过程特性。
- 数据链路层将物理层提供的可能出错的物理连接改造成逻辑上无差错的数据链路,其主要功能包括链路管理、帧同步、差错控制等。
- 网络层负责为分组交换网上的不同主机提供通信服务,主要功能是路由选择和拥塞控制。
- 传输层主要负责端到端的可靠数据传输,TCP(传输控制协议)和UDP(用户数据报协议)是该层的两个主要协议。TCP提供可靠的、面向连接的传输服务,而UDP提供无连接的、不可靠的传输服务。
- 会话层负责建立、管理和终止会话,会话是指两个实体之间的通信连接。
- 表示层主要处理数据的表示、安全、压缩等问题,它将数据转换为适合网络传输的格式。
- 应用层是用户与网络的接口,它为用户提供各种网络服务,如HTTP(超文本传输协议)用于网页浏览,FTP(文件传输协议)用于文件传输等。
- TCP/IP协议族
- TCP/IP是Internet的基础协议,它分为四层,分别是链路层、网络层、传输层和应用层。
- 链路层相当于OSI模型的物理层和数据链路层,主要负责网络设备之间的通信。
- 网络层的核心协议是IP(Internet协议),它负责将数据包从源主机传输到目的主机。IP地址是网络层用于标识主机和网络的地址,分为IPv4和IPv6两种版本。IPv4地址是32位的,用点分十进制表示,如192.168.1.1;IPv6地址是128位的,采用冒号十六进制表示,解决了IPv4地址不足的问题。
- 传输层的协议主要是TCP和UDP,它们的功能与OSI模型中的传输层协议类似。
- 应用层包括许多协议,如HTTP、FTP、SMTP(简单邮件传输协议)等,这些协议为用户提供各种网络应用服务。
(四)网络设备
- 中继器
- 它工作在物理层,用于延长网络的传输距离。中继器的作用是对接收到的信号进行放大和整形,然后转发到下一个中继器或终端设备。它不能对信号进行过滤或处理,只是简单地复制信号。
- 交换机
- 工作在数据链路层,它可以根据数据帧中的MAC地址(媒体访问控制地址)来转发数据。交换机可以为每个端口提供独立的带宽,提高网络的性能。它比传统的集线器(也工作在数据链路层,但只是简单地将数据广播到所有端口)更加智能,能够减少网络冲突。
- 路由器
- 工作在网络层,主要功能是根据IP地址进行数据包的转发。它连接不同的网络(如局域网和广域网),根据路由表来选择数据包的传输路径。路由器还可以实现网络的隔离、访问控制等功能。
- 网关
- 网关是一种充当转换重任的计算机系统或设备。在使用不同的通信协议、数据格式或语言,甚至体系结构完全不同的两种系统之间,网关是一个翻译器。例如,企业内部网络(使用私有IP地址)与外部Internet(使用公有IP地址)之间的通信就需要通过网关来实现地址转换(NAT)等功能。
二、多媒体基础知识
(一)多媒体的定义
多媒体是指多种媒体信息的组合,包括文本、图形、图像、音频、动画和视频等多种形式的信息。它是一种能够同时采集、处理、编辑、存储和展示两种以上不同类型信息媒体的技术。
(二)多媒体数据的特点
- 数据量大
- 多媒体数据,尤其是音频、视频和动画等,通常包含大量的信息。例如,一张高分辨率的图像可能包含数百万个像素,每个像素都有颜色和亮度等信息。一段高清视频每秒可能包含30帧或更多的图像帧,每帧图像的数据量都很大,这就导致多媒体数据的存储和传输需要大量的空间和带宽。
- 数据类型多样
- 多媒体数据包括离散的数据(如文本和图形)和连续的数据(如音频和视频)。离散数据的处理相对简单,可以通过字符编码、图形绘制算法等方式来处理。而连续数据需要通过采样、量化等过程将其转换为数字信号,然后进行存储和处理。
- 数据的时空特性
- 图像和图形是静态的,它们在空间上具有一定的结构和布局。而音频和视频是动态的,它们在时间上具有连续性。例如,音频信号是随时间变化的声波信号,视频信号是随时间变化的图像序列。在处理多媒体数据时,需要考虑数据的时间和空间特性,如对音频进行时间上的压缩和编码,对视频进行帧率控制和空间分辨率调整等。
(三)多媒体的关键技术
- 数据压缩技术
- 由于多媒体数据量大,数据压缩技术是多媒体技术的核心。数据压缩分为无损压缩和有损压缩。
- 无损压缩是指压缩后的数据可以完全恢复到原始数据,不会丢失任何信息。例如,文本文件和某些简单的图形文件通常采用无损压缩。常用的无损压缩算法有霍夫曼编码、LZW编码等。霍夫曼编码是一种基于字符出现频率的编码方法,出现频率高的字符用较短的代码表示,出现频率低的字符用较长的代码表示,从而实现数据压缩。
- 有损压缩是指压缩后的数据不能完全恢复到原始数据,但可以通过控制压缩程度来平衡数据质量和压缩率。例如,音频和视频文件通常采用有损压缩。常用的有损压缩
以下是关于计算机网络与多媒体基础知识的详细介绍,帮助你快速建立知识框架:
一、计算机网络基础
1. 计算机网络的定义与功能
- 定义:通过通信设备和传输介质,将分布在不同地理位置的多台计算机及外围设备连接起来,实现资源共享和信息传递的系统。
- 核心功能:
- 资源共享:共享硬件(如打印机)、软件(如数据库)、数据(如文件)。
- 数据通信:支持电子邮件、即时消息、视频会议等通信方式。
- 分布式处理:将复杂任务分配给网络中的多台计算机协同完成。
2. 网络分类
分类标准 | 类型 | 特点 |
---|---|---|
覆盖范围 | 局域网(LAN) | 范围小(如办公室、校园),传输速度快,延迟低。 |
城域网(MAN) | 覆盖城市范围,用于连接多个局域网。 | |
广域网(WAN) | 覆盖国家或全球(如互联网),依赖公共通信线路(如光纤、卫星)。 | |
拓扑结构 | 总线型、星型、环型、网状型等 | 描述网络中设备的连接方式,影响网络稳定性、扩展性和故障排查难度。 |
3. 网络协议与体系结构
- 协议:网络中设备通信的规则,如:
- TCP/IP协议族:互联网的基础协议,包括TCP(传输控制协议)、IP(网际协议)、HTTP(超文本传输协议)等。
- HTTP/HTTPS:用于网页传输,HTTPS加密更安全。
- FTP:文件传输协议,用于上传/下载文件。
- OSI参考模型(七层):从下到上依次为物理层、数据链路层、网络层、传输层、会话层、表示层、应用层,每层负责不同功能(如物理层定义线缆标准,应用层提供用户接口)。
4. 网络设备
- 路由器:连接不同网络(如LAN与WAN),根据IP地址转发数据。
- 交换机:在局域网内转发数据帧,实现设备间高速通信。
- 集线器(Hub):早期设备,广播式传输数据,效率低,已逐渐被交换机取代。
- 调制解调器(Modem):实现数字信号与模拟信号的转换,用于连接互联网。
5. 互联网与IP地址
- IP地址:标识网络中的设备,如
192.168.1.1
(IPv4格式),新版本IPv6用128位地址解决地址枯竭问题。 - 域名系统(DNS):将域名(如www.baidu.com)解析为IP地址,方便用户记忆。
- 常见服务:
- 万维网(WWW):通过HTTP协议访问网页。
- 电子邮件(Email):使用SMTP(发送)和POP3/IMAP(接收)协议。
- 云计算:通过网络提供计算资源(如服务器、存储)的按需服务。
二、多媒体基础知识
1. 多媒体的定义与元素
- 定义:融合文本、图像、音频、视频、动画等多种形式信息的综合载体。
- 核心元素:
- 文本:最基础的信息载体,可通过字体、排版增强表现力。
- 图像:静态视觉元素,常见格式有JPG(压缩率高)、PNG(支持透明背景)、BMP(无损但文件大)。
- 音频:包括音乐、语音、音效,格式如MP3(有损压缩)、WAV(无损)、AAC(音质好)。
- 视频:动态图像序列,格式如MP4(通用)、AVI(无损但体积大)、MOV(苹果设备常用),需压缩技术(如H.264编码)减少存储空间。
- 动画:通过连续画面模拟运动,分为二维(Flash)和三维(Maya制作)。
2. 多媒体数据压缩技术
- 必要性:原始多媒体数据(如图像、视频)体积庞大,压缩可减少存储和传输成本。
- 分类:
- 无损压缩:还原后数据与原始一致,如ZIP压缩文本、PNG图像。
- 有损压缩:牺牲部分细节换取高压缩率,如JPG图像、MP3音频、H.264视频。
- 常用标准:
- JPEG:用于静态图像压缩。
- MPEG:用于视频和音频压缩(如MP3、MP4)。
- H.26x系列:高效视频编码标准,广泛用于网络视频流(如YouTube、抖音)。
3. 多媒体应用领域
- 娱乐:电影、游戏、音乐、虚拟现实(VR)、增强现实(AR)。
- 教育:多媒体课件、在线课程、虚拟实验室。
- 广告与营销:短视频、动画广告、互动宣传页。
- 通信:视频会议、直播、实时语音聊天。
- 工业与医疗:工业仿真、医学影像(如CT图像)分析、远程手术指导。
4. 多媒体工具软件
- 图像编辑:Photoshop(专业修图)、Canva(在线设计,适合新手)。
- 音频处理:Audacity(免费录音与剪辑)、Adobe Audition(专业音频编辑)。
- 视频制作:Premiere Pro(剪辑)、After Effects(特效)、剪映(手机端简易剪辑)。
- 动画设计:Flash(二维)、Blender(开源三维动画与建模)。
三、计算机网络与多媒体的结合
- 流媒体技术:通过网络实时传输音频/视频流(如Netflix、腾讯视频),边下载边播放,无需完全下载。
- 在线协作:通过网络共享多媒体文件(如Google Docs支持图文协作)、实时编辑(如Figma设计工具)。
- 虚拟现实(VR)与增强现实(AR):依赖高速网络传输3D模型、视频流,实现沉浸式体验(如VR远程会议、AR导航)。
- 云计算与多媒体:通过云存储(如阿里云盘)备份多媒体文件,通过云渲染服务加速视频/动画制作。
四、延伸学习建议
- 入门书籍:
- 《计算机网络(第7版)》(谢希仁):经典网络教材。
- 《多媒体技术基础及应用》(林福宗):系统讲解多媒体原理与应用。
- 在线课程:
- Coursera:《Computer Networking: A Top-Down Approach》(计算机网络入门)。
- 中国大学MOOC:《多媒体技术》(涵盖图像处理、视频编辑等实操内容)。
- 实践工具:
- 搭建小型局域网:使用路由器和交换机,练习IP配置和文件共享。
- 尝试多媒体创作:用剪映制作短视频,用Audacity剪辑一段音频,熟悉工具操作。
通过以上内容,你可以初步掌握计算机网络与多媒体的核心概念,后续可根据兴趣深入某个领域(如网络安全、视频特效制作等)进行专项学习。
Spring Social Service Provider Framework 是 Spring Social 的核心组件,它提供了一种灵活且可扩展的方式来管理应用程序与外部服务提供商(如 Facebook、Twitter 等)之间的连接。以下是对其的详细解析:
核心组件
- Connection 接口:表示与外部服务提供商的连接,是用户本地账户与服务提供商账户之间的绑定抽象。它提供了获取连接的元数据(如显示名称、用户头像等)、测试连接有效性、刷新连接、获取用户信息等功能。
- ConnectionFactory 抽象类:用于创建与特定服务提供商的连接。它封装了特定授权协议的细节,如 OAuth 1 或 OAuth 2。Spring Social 提供了
OAuth1ConnectionFactory
和OAuth2ConnectionFactory
两种实现。 - ServiceProvider 接口:定义了服务提供商的基本操作,包括获取授权操作接口和 API 绑定接口。Spring Social 提供了
AbstractOAuth1ServiceProvider
和AbstractOAuth2ServiceProvider
两个抽象类,开发者可以继承这些类来实现自定义的服务提供商。 - ConnectController:处理 Web 应用程序与服务提供商之间的 OAuth 授权流程。
- SignInController:允许用户通过服务提供商的账户登录应用程序。
工作流程
- 建立连接:用户通过应用程序发起与服务提供商的连接请求。
ConnectionFactory
负责引导用户完成授权流程,并最终创建一个Connection
实例。 - 使用连接:一旦连接建立,应用程序可以通过
Connection
的getApi()
方法获取服务提供商的 API 绑定,从而以用户的身份调用服务提供商的 API。 - 管理连接:应用程序可以对连接进行管理,如测试连接有效性、刷新连接、同步用户信息等。
特点
- 可扩展性:支持多种授权协议(如 OAuth 1 和 OAuth 2),并且可以通过扩展框架来支持其他协议。
- 模块化设计:每个服务提供商模块独立发展,便于快速响应服务提供商 API 的变化。
- 简化集成:提供了一套统一的 API,使得与不同服务提供商的集成变得更加简单。
应用场景
Spring Social Service Provider Framework 适用于需要与多个 SaaS 平台集成的 Web 应用程序,例如:
- 社交网络集成:允许用户通过社交媒体账户登录或分享内容。
- 用户身份验证:通过第三方服务提供商验证用户身份。
- 数据同步:同步用户在不同平台上的数据。
尽管 Spring Social 已不再由 VMware 积极维护,但其仍然是一个功能强大且灵活的工具。
Spring Social Service Provider Framework 是 Spring 框架的一个重要组件,旨在简化与各种社交网络和服务提供商(如 Facebook、Twitter、LinkedIn 等)的集成。以下是对该框架的详细解析:
核心概念与架构
Spring Social 的核心目标是提供统一的编程模型,帮助开发者与第三方服务提供商进行身份验证和数据交互,而无需关注每个平台的具体实现细节。
1. Service Provider(服务提供商)
- 定义:表示一个外部服务(如 Twitter、Facebook)的抽象接口。
- 核心组件:
- API Binding:与服务提供商 API 交互的接口(如
Twitter
、Facebook
)。 - Connection Factory:创建
Connection
对象的工厂,负责处理服务提供商的特定配置。 - OAuth 模板:封装 OAuth 1.0a 或 OAuth 2.0 流程的实现。
- API Binding:与服务提供商 API 交互的接口(如
2. Connection(连接)
- 定义:表示应用与服务提供商之间的连接,包含用户认证信息(如访问令牌)和用户在服务提供商处的身份标识。
- 核心接口:
Connection<T>
:泛型接口,T
为具体的 API 类型(如Twitter
)。ConnectionRepository
:管理用户与服务提供商的连接(增删改查)。UsersConnectionRepository
:管理所有用户的连接,通常与数据库交互。
3. API Binding(API 绑定)
- 定义:封装与服务提供商 API 交互的具体实现,提供访问服务功能的方法(如获取用户资料、发布内容)。
- 示例:
Twitter
接口:提供 tweet、user、search 等操作方法。Facebook
接口:提供 user、friend、feed 等操作方法。
工作流程(以 OAuth 2.0 为例)
- 用户请求授权:
- 应用引导用户访问服务提供商的授权页面(如 Facebook 的登录授权页)。
- 授权码获取:
- 用户在服务提供商处登录并授权后,服务提供商会重定向回应用,并携带授权码。
- 访问令牌交换:
- 应用使用授权码向服务提供商换取访问令牌(Access Token)。
- 建立连接:
- 通过
ConnectionFactory
创建Connection
对象,存储访问令牌和用户信息。
- 通过
- API 调用:
- 通过
Connection
获取 API 绑定实例(如Facebook
),调用具体方法获取用户数据。
- 通过
框架优势
- 简化 OAuth 流程:自动处理复杂的 OAuth 1.0a/2.0 流程,包括令牌管理和刷新。
- 统一 API 模型:提供一致的编程接口,减少学习成本。
- 可扩展性:支持自定义服务提供商,便于集成新的第三方平台。
- 安全机制:妥善管理用户凭证,避免直接暴露敏感信息。
- 与 Spring 生态集成:无缝集成 Spring Security、Spring MVC 等模块。
关键接口与类
-
ServiceProvider 接口:
public interface ServiceProvider<A> { ApiBindingFactory<A> getApiBindingFactory(); ConnectionFactory<A> getConnectionFactory(); }
-
ConnectionFactory 接口:
public interface ConnectionFactory<A> { String getProviderId(); Connection<A> createConnection(AccessToken accessToken); Connection<A> createConnection(ConnectionData data); }
-
ConnectionRepository 接口:
public interface ConnectionRepository { List<Connection<?>> findAllConnections(); Connection<?> getConnection(ServiceProviderIdentifier providerId); void addConnection(Connection<?> connection); void updateConnection(Connection<?> connection); void removeConnections(String providerId); }
自定义服务提供商
若要集成未被 Spring Social 官方支持的服务(如 GitHub、Instagram),需实现以下步骤:
- 创建 API 绑定接口:定义与服务交互的方法。
- 实现 API 客户端:具体实现接口方法,调用服务提供商的 REST API。
- 创建 ConnectionFactory:配置服务提供商的 OAuth 参数。
- 注册到 Spring 容器:将自定义组件注册为 Bean。
实际应用场景
- 社交登录:允许用户使用第三方账号(如 Google、微信)登录应用。
- 数据同步:从社交媒体平台获取用户内容(如朋友圈、推文)。
- 第三方分享:将应用内内容分享到社交网络。
- 用户认证:利用第三方身份验证替代传统注册流程。
与 Spring Security 的集成
Spring Social 通常与 Spring Security 配合使用,实现:
- 社交登录认证:通过
SocialAuthenticationFilter
拦截 OAuth 回调请求。 - 权限管理:结合 Spring Security 的权限模型,控制对受保护资源的访问。
总结
Spring Social Service Provider Framework 通过抽象化服务提供商的差异,为开发者提供了高效、安全的社交网络集成方案。其核心价值在于统一的 API 模型和自动化的 OAuth 流程管理,使开发者能够专注于业务逻辑而非底层实现。
In my previous post, I introduced you to Spring Social’s Java bindings to popular Software-as-a-Service (SaaS) APIs such as Twitter, Facebook, LinkedIn, and TripIt. In addition to providing simple, strongly-typed Java methods for common API operations, these bindings ensure each HTTP request includes the credentials required to authorize your application to invoke the API on behalf of a user.
What my first post did not address was: how do we manage the credentials required to invoke service APIs on behalf of users? I’m pleased to say that we now have answers to that question.
Earlier this week, we announced the release of the second milestone of the Spring Social project. The most significant new feature in Spring Social 1.0.0.M2 is the introduction of a Service Provider “Connect” framework. Today I want to introduce you to this framework and show you how to use it to manage “connections” to SaaS providers.
The examples in this article are from the Spring Social Showcase. To follow along, clone the repository and follow the README to build and deploy the sample app.
Getting Spring Social
With the M2 release, Spring Social has been split into several modules:
spring-social-core - The service provider framework, OAuth support, and core classes.
spring-social-web - The connect controller and supporting types.
spring-social-facebook - A service provider implementation for connecting with Facebook and support for signing into an application via Facebook.
spring-social-twitter - A service provider implementation for connecting with Twitter and support for signing into an application via Twitter.
spring-social-linkedin - A service provider implementation for connecting with LinkedIn.
spring-social-tripit - A service provider implementation for connecting with TripIt.
spring-social-github - A service provider implementation for connecting with GitHub.
spring-social-gowalla - A service provider implementation for connecting with Gowalla.
spring-social-test - Support for testing service provider implementations and API bindings.
Depending on your needs, you won’t necessarily need all of these modules. At a minimum, you’ll need the core module. You can add this to a Maven-built project with the following entry:
org.springframework.social spring-social-core 1.0.0.M2In the likely case that you’ll be using Spring Social in a web application, you’ll also need the web module:
org.springframework.social spring-social-web 1.0.0.M2Then, you’ll need to add one or more of the provider modules. In our examples, we’ll be talking about adding Twitter connectivity to an application, so we’ll need the twitter module:
org.springframework.social spring-social-twitter 1.0.0.M2Since we’re building off of a milestone release of Spring Social, we’re going to need to add Spring’s milestone repository to the pom.xml file:
org.springframework.maven.milestone Spring Maven Milestone Repository http://maven.springframework.org/milestone falseNote that although Spring Social 1.0.0.M2 has a compile-time dependency on Spring Framework 3.1, Spring Social also works fine with Spring Framework 3.0.x.
With the dependencies addressed in our build, we’re ready to add Twitter connectivity to the application.
Connecting with Twitter
To help understand what Spring Social’s Service Provider “Connect” Framework brings to the table, let’s take a quick tour through the showcase application.
After signing into the showcase sample, the first thing you’ll see are a list of the service providers that the showcase supports connectivity with: Twitter, Facebook, and TripIt. They should all show that your account is not yet connected to any of them.
If you click on the Twitter link, you’ll be redirected to a connection status page and be given the opportunity to “Connect with Twitter”. You’ll also see a checkbox that, if checked, will post a tweet about Spring Social Showcase after connecting to Twitter.
After clicking the “Connect with Twitter” button (I’ll let you decide whether or not to check the “Post tweet…” checkbox), you’ll be redirected to Twitter’s authorization page, which should look something like this:
The username and password fields won’t be shown if you’re already signed into Twitter. But in either case, you’ll be given an opportunity to deny or allow the showcase example the right to access and update your data on Twitter. When you click on the “Allow” button to grant permission, Twitter will redirect back to the showcase application and a connection will be created between the showcase account and your Twitter profile. You’ll be shown a connection status page, this time indicating that a connection exists.
At this point, you may choose to disconnect from Twitter if you’d like. But for now, return to the home page and you’ll see that you are now connected to Twitter.
If you click on the “Twitter” link, with a connection available, you’ll be taken to the Twitter Showcase page which displays, among other things, your profile image, Twitter display name, and Twitter screen name. You may also choose to connect another Twitter account from here (you may connect multiple provider accounts to a single application account), disconnect from Twitter, or post a tweet.
The Service Provider “Connect” Framework
When you first clicked on the “Twitter” link in the showcase sample, you were shown a status connection page for Twitter. This page is served by Spring Social’s ConnectController. ConnectController is a Spring MVC controller that handles the authorization process. In the case of Twitter, that’s an OAuth 1 authorization flow. For OAuth 1, ConnectController supports the following flow:
GET /connect/{provider ID} - Displays a page with the connection status.
POST /connect/{provider ID} - Initiates the connection flow, requests a request token from the provider, and redirects to the provider's authorization page.
GET /connect/{provider ID}?oauth_token={request token}&oauth_verifier={verifier} - Handles the callback after authorization, exchanges the request token and verifier for an access token, and creates the connection.
DELETE /connect/{provider ID} - Disconnects from the provider.
ConnectController also supports OAuth 2’s authorization flow. When authorizing against an OAuth 2 based provider like Facebook, ConnectController’s flow is slightly different:
GET /connect/{provider ID} - Displays a connection status page
POST /connect/{provider ID} - Initiates authorization with the provider, redirecting to the provider's signin and/or authorization page.
GET /connect/{provider ID}?code={code} - Handles the authorization callback from the provider, exchanges the code for an access token, and creates the connection.
DELETE /connect/{provider ID} - Disconnects from the provider
ConnectController can be configured in a Spring MVC application as simple as adding the following element:
If you look at how ConnectController is configured in the showcase sample, however, you’ll see that it has an interceptors property injected with a list of interceptor beans. Connect interceptors let you inject custom functionality into the connection flow. One of those interceptors, TweetAfterConnectInterceptor, is responsible for posting a tweet after you connect with Twitter. (See the reference documentation for more information on connection interceptors.)
When a request goes to ConnectController, it works with a service provider class to handle the behind-the-scenes interaction with the service. In the case of Twitter, that provider class is TwitterServiceProvider and is configured in Spring like this:
TwitterServiceProvider, like all of Spring’s service provider implementations, is constructed with the following arguments:
The application's ID or consumer key.
The application's secret or consumer secret.
A reference to a connection repository bean used to persist connection details.
You obtain the application’s consumer key and secret when you register the application with the service provider. To register an application with Twitter go to https://dev.twitter.com/apps/new and fill out the form. When you’re done, Twitter will respond with a page showing details about your application, including the consumer key and consumer secret.
In this case, the application’s Twitter application ID and secret are expressed as placeholder variables to be resolved by a property placeholder configurer. It’s recommended that you externalize these details, since you’ll likely have a different ID/secret pair for your production deployment than you would for testing and development environments.
Each of Spring’s service provider classes has a provider ID (returned from its getId() method and different than its Spring bean ID). ConnectController will use that ID, as specified in the URL path, to choose the service provider it will work with when creating connections. For TwitterServiceProvider, the provider ID is “twitter”. Mapping this ID to ConnectController’s request mappings, we get the following flow:
GET /connect/twitter - Displays the user's connection status with Twitter.
POST /connect/twitter - A request token is retrieved from Twitter and then redirects to the Twitter's authorization page. This is where the flow went to when you clicked the "Connect with Twitter" button.
GET /connect/twitter?oauth_token={request token}&oauth_verifier={verifier} - Handles the callback after authorization, exchanges the request token and verifier for an access token, and creates the connection.
DELETE /connect/twitter - Disconnects from Twitter. This is what happens when you click the "Disconnect" button.
Once a connection has been established, it’s important to store it away for future use so that you won’t have to repeatedly ask your users to reauthorize your application. Therefore, TwitterServiceProvider relies on a connection repository to persist the connection information. Spring Social provides JdbcConnectionRepository, which persists connection details in a relational database. The JdbcConnectionRepository bean is configured like this:
The JdbcConnectionRepository depends on a data source through which it will access the database as well as an instance of Spring Security 3.1’s TextEncryptor interface. It uses the text encryptor to encrypt the connection credentials (e.g., access token and secret) when storing them in the database. As a sample application, Spring Security’s encryption needs are rather light, so it’s wired with a no-op text encryptor:
(For production-level encryption, consider one of the other encryptors offered by Spring Security’s Encryptors static factory class.)
Working with connections
When you first signed into the Spring Social Showcase, it displayed a list of service providers and indicated that you were not connected with any of them. After going through the connection process with Twitter and returning to that page, it showed that a connection had been established.
The showcase application’s HomeController determines the connection status with each of the providers, by calling their isConnected() method:
for (ServiceProvider<?> serviceProvider : serviceProviders) {
boolean connected = serviceProvider.isConnected(currentUser.getName());
model.addAttribute(serviceProvider.getId() + “_status”, connected ? “Yes” : “No”);
}
The isConnected() method will return true if there is one or more connections between the given account and the service provider.
You may also ask the provider for a list of connections by calling its getConnections() method:
List<ServiceProviderConnection> connections = twitterProvider.getConnections(currentUser.getName());
From any one of those connections, you may retrieve the service API. For example, to get an instance of TwitterApi from the first connection:
TwitterApi twitter = connections.get(0).getServiceApi();
With that TwitterApi object you may interact with Twitter, fetching timeline entries, posting tweets, or any of the other operations defined in the TwitterApi interface.
A key thing to notice here is that at no point is it necessary for application code to deal directly with access tokens and secrets in order to interact with the service. The service provider framework deals with those details under the covers.
Summary
Spring Social 1.0.0.M2 brings a service provider framework to the table that greatly simplifies an application’s role in obtaining authorization to access a service on behalf of a user and connecting the user’s local application account to their account on the provider. Adding connection capabilities to a Spring application involves configuring a service provider as a bean in the Spring application context and Spring Social’s ConnectController to handle the authorization process. The reference documentation has more information on using Spring Social’s service provider framework.
Although Spring Social 1.0.0.M2 only offers service provider implementations for 6 providers (Facebook, Twitter, LinkedIn, TripIt, GitHub, and Gowalla), the framework is easily extensible. In a followup article, I’ll show you what goes into writing a custom service provider implementation, enabling you to use Spring Social’s connection support for any provider you need to connect to.
As always, we are very interested in hearing your feedback. We encourage you to collaborate with us in the forum or by submitting enhancement requests in issue tracking.
SpringSocial对流行的软件即服务(SaaS)API(如Twitter、Facebook、LinkedIn和TripIt)的Java绑定。除了为常见的API操作提供简单的强类型Java方法外,这些绑定还确保每个HTTP请求都包含授权应用程序代表用户调用API所需的凭据。
我的第一篇文章没有提到的是:如何管理代表用户调用服务api所需的凭据?我很高兴地说,我们现在有了这个问题的答案。
本周早些时候,我们宣布发布春季社会项目的第二个里程碑。SpringSocial1.0.0.M2中最重要的新特性是引入了一个服务提供商“连接”框架。今天我将向您介绍这个框架,并向您展示如何使用它来管理与SaaS提供商的“连接”。
本文中的示例来自Spring Social Showcase。接下来,克隆存储库并按照自述文件构建和部署示例应用程序。