名词定义:
(1)Third-party application:第三方应用程序,本文中又称"客户端"(client),即上一节例子中的"云冲印"。
(2)HTTP service:HTTP服务提供商,本文中简称"服务提供商",即上一节例子中的Google。
(3)Resource Owner:资源所有者,本文中又称"用户"(user)。
(4)User Agent:用户代理,本文中就是指浏览器。
(5)Authorization server:认证服务器,即服务提供商专门用来处理认证的服务器。
(6)Resource server:资源服务器,即服务提供商存放用户生成的资源的服务器。它与认证服务器,可以是同一台服务器,也可以是不同的服务器。
运行流程:
(1)用户打开客户端以后,客户端要求用户授权。
(3)客户端根据获得的授权,向认证服务器申请令牌。
(4)认证服务器对客户端进行认证,确认无误后发放令牌。
(5)客户端使用令牌,向资源服务器申请获取资源。
(6)资源服务器确认令牌无误后,向客户端开放资源。
授权码模式(authorization code):功能最完整、流程最严密的授权模式。它的特点就是通过客户端的后台服务器,与"服务提供商"的认证服务器进行互动。
(1)用户访问客户端,客户端将用户导向认证服务器
客户端申请认证的URI包含以下参数:
response_type:表示授权类型,必选项,此处的值固定为"code"
client_id:表示客户端的ID,必选项
redirect_uri:表示重定向URI,可选项
scope:表示申请的权限范围,可选项
state:表示客户端的当前状态,可以指定任意值,认证服务器会原封不动地返回这个值。
例如:/authorize?response_type=code&client_id=s6BhdRkqt3&state=xyz&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
(3)假设用户给予授权,认证服务器会将用户导向客户端原先指定的重定向URI,并加上一个授权码
服务器回应客户端的URI包含以下参数:
code:表示授权码,必选项。该码的有效期应该很短,通常设为10分钟,客户端只能使用该码一次,否则会被授权服务器拒绝。
该码与客户端ID和重定向URI,是一一对应关系。
state:如果客户端的请求中包含这个参数,认证服务器的回应也必须一模一样包含这个参数。
例如:https://client.example.com/cb?code=SplxlOBeZQQYbYS6WxSbIA&state=xyz
(4)客户端接收到授权码和原先的重定向URI,向认证服务器申请令牌
客户端向认证服务器申请令牌的HTTP请求包含以下参数:
grant_type:表示使用的授权模式,必选项,此处的值固定为"authorization_code"。
code:表示上一步获得的授权码,必选项。
redirect_uri:表示重定向URI,必选项,且必须与A步骤中的该参数值保持一致。
client_id:表示客户端ID,必选项。
例如:grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
(5)认证服务器核对令牌和重定向URI,确认无误后向客户端发送访问令牌和更新令牌
认证服务器发送的HTTP回复,包含以下参数:
access_token:表示访问令牌,必选项。
token_type:表示令牌类型,该值大小写不敏感,必选项,可以是bearer类型或mac类型。
expires_in:表示过期时间,单位为秒。如果省略该参数,必须其他方式设置过期时间。
refresh_token:表示更新令牌,用来获取下一次的访问令牌,可选项。
scope:表示权限范围,如果与客户端申请的范围一致,此项可省略。
例如:
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache
{
"access_token":"2YotnFZFEjr1zCsicMWpAA",
"token_type":"example",
"expires_in":3600,
"refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
"example_parameter":"example_value"
}
从上面回复代码可以看出,相关参数使用JSON格式发送(Content-Type: application/json)。此外,HTTP头信息中明确指定不得缓存。
简化模式(implicit grant type):
spring security与OAuth2.0集成
1)修改web.xml文件:
2)配置applicationContext-security.xml:
authentication-manager-ref="oauth2AuthenticationManager" entry-point-ref="oauth2AuthenticationEntryPoint">
class="org.springframework.security.oauth2.provider.token.store.JdbcTokenStore">
class="org.springframework.security.oauth2.provider.client.JdbcClientDetailsService">
class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler">
class="org.springframework.security.oauth2.provider.approval.ApprovalStoreUserApprovalHandler">
class="org.springframework.security.oauth2.provider.approval.JdbcApprovalStore">
class="org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory">
class="org.springframework.security.access.vote.AffirmativeBased">
client-details-service-ref="clientDetailsService" token-services-ref="tokenServices" user-approval-handler-ref="oauthUserApprovalHandler">
class="org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices">
class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
resource-id="rest-resource" token-services-ref="tokenServices" />
entry-point-ref="oauth2AuthenticationEntryPoint" access-decision-manager-ref="oauth2AccessDecisionManager" use-expressions="false">
authentication-failure-url="/login.jsp?login_error=1" default-target-url="/index.jsp" login-page="/login.jsp" login-processing-url="/tms_security_check"/>
閱讀更多 熱血萌寵 的文章