Spring Security构建Rest服务-1001-spring social开发第三方登录之spring social基本原理
OAuth协议是一个授权协议,目的是让用户在不将服务提供商的用户名密码交给第三方应用的条件下,让第三方应用可以有权限访问用户存在服务提供商上的资源。
接着上一篇说的,在第三方应用获取到用户资源后,如果过去的不是用户的自拍数据,而是用户的昵称头像等基本信息,根据这些基本信息,构建经过认证的Authentication对象,放进SecurityContext,这对于spring security来说,就算认证成功了。第三方应用引导用户走完这个流程,就是用户使用用户在服务提供商的上的用户基本信息登录了第三方应用。

图一
SpringSocial:就是把这个流程封装成了一个SocialAuthenticationFilter过滤器,然后吧这个过滤器加到springsecurity的过滤器链上。
官方定义:将您的Spring应用程序与软件即服务(SaaS)API提供程序(如Facebook,Twitter和LinkedIn)连接起来。
这样当你访问某一个请求的时候,SocialAuthenticationFilter会把这个请求拦截下来,然后走完上图的流程。这样就是实现了微信登录。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
附 :Spring Security对登录成功的定义
在处理登录过滤器的抽象类AbstractAuthenticationProcessingFilter定义了的doFilter方法定义了登录操作:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res; if (!requiresAuthentication(request, response)) {
chain.doFilter(request, response); return;
} if (logger.isDebugEnabled()) {
logger.debug("Request is to process authentication");
} Authentication authResult; try {
authResult = attemptAuthentication(request, response);
if (authResult == null) {
// return immediately as subclass has indicated that it hasn't completed
// authentication
return;
}
sessionStrategy.onAuthentication(authResult, request, response);
}
catch (InternalAuthenticationServiceException failed) {
logger.error(
"An internal error occurred while trying to authenticate the user.",
failed);
unsuccessfulAuthentication(request, response, failed); return;
}
catch (AuthenticationException failed) {
// Authentication failed
unsuccessfulAuthentication(request, response, failed); return;
} // Authentication success
if (continueChainBeforeSuccessfulAuthentication) {
chain.doFilter(request, response);
} successfulAuthentication(request, response, chain, authResult);
}
其中的successfulAuthentication()方法定义了认证成功后的操作:
SecurityContextHolder.getContext().setAuthentication(authResult)这句话就是说,认证成功后,用SecurityContextHolder.getContext()获取到spring安全的上下文SecurityContext,往里边放一个认证类Authentication,这就算是登录成功了。
protected void successfulAuthentication(HttpServletRequest request,
HttpServletResponse response, FilterChain chain, Authentication authResult)
throws IOException, ServletException { if (logger.isDebugEnabled()) {
logger.debug("Authentication success. Updating SecurityContextHolder to contain: "
+ authResult);
} SecurityContextHolder.getContext().setAuthentication(authResult); rememberMeServices.loginSuccess(request, response, authResult); // Fire event
if (this.eventPublisher != null) {
eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(
authResult, this.getClass()));
} successHandler.onAuthenticationSuccess(request, response, authResult);
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Spring Social:
相关接口

图一中的步骤1~步骤6,都是在和服务提供商打交道
1,ServiceProvider:服务提供商的抽象,针对不同的服务提供商,如新浪、qq、微信,都需要一个ServiceProvider具体实现。
SpringSocial提供了AbstractOAuth2ServiceProvider抽象类,实现了共有的东西。我们使用时候继承这个类即可。
图一步中服务提供商中骤1~步骤5是OAuth标准的流程,第6步是个性化的。因为每一个服务提供商对的用户基本信息结构的定义都是不一样的。如字段的个数,名字可能都不一样。
OAuth2Operations:封装了第1步到第5步标准的OAuth协议的处理流程,Oauth2协议的相关的操作。这个接口Spring Social提供了一个默认的实现OAuth2Template。
Api:处理第6步需要的接口的实现,没有明确的接口,需要自定义,因为每个服务提供商对用户信息的调用都是有区别的。SpringSocial的默认实现是AbstractOAuth2Binding
2,Connection:用户信息的封装,命名是固定的,需要一个ApiAdapter适配器来转换api和connection。用来获取前6步获取到的用户信息,Spring Social的实现是OAuth2Connection,由ConnectionFactory工厂接口创建的(该工厂接口的默认实现是OAuth2ConnectionFactory,需要ServiceProvider),第7步是再第三方应用服务内部完成的。
3,UsersConnectionRepository:服务提供商里的用户和业务系统里的用户之间有对应关系,在spring social中是存在数据库中UserConnection表里,存储了业务中系统中User表的userid和服务提供商的用户(Connection)之间的对应关系。UsersConnectionRepository(默认实现是JdbcUsersConnectionRepository)来操作这个表
Spring Security构建Rest服务-1001-spring social开发第三方登录之spring social基本原理的更多相关文章
- Spring Security构建Rest服务-1000-使用SpringSocial开发第三方登录之大白话OAuth协议
OAuth协议简介 OAuth协议要解决的问题 OAuth协议中的各种角色 OAuth协议运行流程 OAuth协议,在网上也看了一些资料,意思就是给你颁发一个临时的通行证,你拿着这个通行证可以访 ...
- Spring Security构建Rest服务-1200-SpringSecurity OAuth开发APP认证框架
基于服务器Session的认证方式: 前边说的用户名密码登录.短信登录.第三方登录,都是普通的登录,是基于服务器Session保存用户信息的登录方式.登录信息都是存在服务器的session(服务器的一 ...
- Spring Security构建Rest服务-0102-Spring Social开发第三方登录之qq登录
图一 基于SpringSocial实现qq登录,要走一个OAuth流程,拿到服务提供商qq返回的用户信息. 由上篇介绍的可知,用户信息被封装在了Connection里,所以最终要拿到Connectio ...
- Spring Security构建Rest服务-1300-Spring Security OAuth开发APP认证框架之JWT实现单点登录
基于JWT实现SSO 在淘宝( https://www.taobao.com )上点击登录,已经跳到了 https://login.taobao.com,这是又一个服务器.只要在淘宝登录了,就能直接访 ...
- Spring Security构建Rest服务-1202-Spring Security OAuth开发APP认证框架之重构3种登录方式
SpringSecurityOAuth核心源码解析 蓝色表示接口,绿色表示类 1,TokenEndpoint 整个入口点,相当于一个controller,不同的授权模式获取token的地址都是 /oa ...
- Spring Security构建Rest服务-1203-Spring Security OAuth开发APP认证框架之短信验证码登录
浏览器模式下验证码存储策略 浏览器模式下,生成的短信验证码或者图形验证码是存在session里的,用户接收到验证码后携带过来做校验. APP模式下验证码存储策略 在app场景下里是没有cookie信息 ...
- Spring Security构建Rest服务-1201-Spring Security OAuth开发APP认证框架之实现服务提供商
实现服务提供商,就是要实现认证服务器.资源服务器. 现在做的都是app的东西,所以在app项目写代码 认证服务器: 新建 ImoocAuthenticationServerConfig 类,@Ena ...
- Spring Security构建Rest服务-0702-短信验证码登录
先来看下 Spring Security密码登录大概流程,模拟这个流程,开发短信登录流程 1,密码登录请求发送给过滤器 UsernamePasswordAuthenticationFilter 2,过 ...
- Spring Security构建Rest服务-0900-rememberMe记住我
Spring security记住我基本原理: 登录的时候,请求发送给过滤器UsernamePasswordAuthenticationFilter,当该过滤器认证成功后,会调用RememberMeS ...
随机推荐
- HDU 1846 Brave Game (博弈水题)
题意:中文...你们懂得. 析:这个就是一个水题博弈,就是一个巴什博弈定理,直接就没有变,如果你们看过我写的那个,这个题绝对水过. 附地址:http://www.cnblogs.com/dwtfukg ...
- UVa 1572 Self-Assembly (构造+拓扑排序。。。。。)
题意:给定n个带标号的正方形,标号要么是一个大写字母加一个+或-,要么是00, 当且仅当大写字母相同并且符号相反时可以连接,问你给定的能不能拼成一个无限大的的东西. 析:说实话,真心没有看出来是拓扑排 ...
- PAT甲 1006. Sign In and Sign Out (25) 2016-09-09 22:55 43人阅读 评论(0) 收藏
1006. Sign In and Sign Out (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- Android-卖票案例static-不推荐此方式
需求描述:四个窗口一起卖票,把10张票卖完,不许多卖 先看一个错误的案例: package android.java.thread06; /** * 售票线程 */ class Booking ext ...
- unusedjs
查看js的有效使用情况: https://github.com/gmetais/unusedjs Installation You need to open your console and writ ...
- NETSDK1061错误解决
NETSDK1061错误解决 在vs生成和运行都正常,发布的时候报错 .netcore控制台项目引用另一个类库 错误信息 NETSDK1061: 项目是使用 Microsoft.NETCore.App ...
- Microsoft.Web.Administration操作IIS7时的权限设置
在用Microsoft.Web.Administration操作IIS7时,你可能会遇到如下权限错误: 文件名: redirection.config错误: 由于权限不足而无法读取配置文件 如下图: ...
- php CI框架log写入
1.首先,打开application下的config.php文件,将log配置打开如下 /* |---------------------------------------------------- ...
- wpf使用DynamicDataDisplay插件,修改x轴的样式,改成透明的。
时光偷走的,永远都是我们眼皮底下看不见的珍贵. 问题:X轴会显示灰色拖动条. 解决:将X轴颜色改为透明. DDD插件是开源的,但是网上的参考资料却很少,所以,很多问题在网上搜索不到,因为没有找到该插件 ...
- log Log4NET配置
Log4Net是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件.数据库.EventLog等),日志就是程序的黑匣子,可以通过 日志查看系统的运行过程,从而发现系统的问题.日志的作用:将 ...