javascript
java B2B2C springmvc mybatis电子商务平台源码-Spring Cloud Security
一、SpringCloud Security簡介
??????????Spring Cloud Security提供了一組原語,用于構建安全的應用程序和服務,而且操作簡便。可以在外部(或集中)進行大量配置的聲明性模型有助于實現大型協作的遠程組件系統,通常具有中央身份管理服務。它也非常易于在Cloud Foundry等服務平臺中使用。在Spring Boot和Spring Security OAuth2的基礎上,可以快速創建實現常見模式的系統,如單點登錄,令牌中繼和令牌交換。愿意了解源碼的朋友直接求求交流分享技術:二一四七七七五六三三
功能:
從Zuul代理中的前端到后端服務中繼SSO令牌
資源服務器之間的中繼令牌
使Feign客戶端表現得像OAuth2RestTemplate(獲取令牌等)的攔截器
在Zuul代理中配置下游身份驗證
二、SpringCloud Security知識點
封裝順序是這樣的:spring security及其各個模塊=》spring cloud security=》spring boot autoconfigure的security部分,比如autoconfigure模塊有個spring security的sso,是對spring security在oath2下的封裝
spring oauth2 authorizeserver,resourceserver,認證服務器和資源服務器是怎么交互的,token_key,/oauth/token/ refresh/token
resourceserver就是業務系統,oauth client 就是邊緣業務系統,比如直接面向用戶的UI系統,或者UI系統直接調用的API接口這一層;
JWT和OAuth2的區別?看到好多人在比較這兩個東西,現在終結這個問題:JWT只是OAuth2中的token的一種類型。
jwt client(Resource Server或者Zuul等),使用jwt的業務系統在解碼acces_token的時候,需要一個toke_key,這個token-key就是JWT Client應用啟動的時候從Auth Server拉取的,接口是token/token_key
單點登錄這種功能,好多javaee容器都自帶的,像websphere
spring Security完全將認證和授權分開了,資源只需要聲明自己是需要認證的,需要什么樣的權限,只管跟當前用戶要access_token。
授權的四種方式任意,只要拿到token就可以去讓資源去認證。
邊緣服務器需要開啟@EnableOAuth2Sso,但是邊緣服務器也是一個ResourceServer,邊緣服務器如果是zuul的話,就不是一個ResourceServer了,只需要添加@EnableOAuth2Sso注解就可以了;
client_credentials模式下spring boot不會幫助spring Security構建ClientCredentialsResourceDetails 對象,需要開發者自己創建
favicon.icon,記得把這個東西過濾掉,奶奶的
在其中一個邊緣服務上,您可能需要啟用單點登錄。
@EnableOAuthSso,只需要在邊緣服務器指定沒用來引導未登錄的用戶登錄系統。@EnableOAuthSso將允許您將未經認證的用戶的自動重定向轉向授權服務器,他們將能夠登錄
EnableOAuth2Client,在中間中繼的時候用
ClientCredentialsTokenEndpointFilter,AS設置了allowFormAuthenticationForClients才會有,詳情看這里面的AuthorizationServerSecurityConfigurer#configure(HttpSecurity http)邏輯,這點非常重要,ClientCredentialsTokenEndpointFilter是用來驗證clientid和client_secret的,使用clientid和client_secret換取下一步的東西;
TokenGranter,AuthorizationCodeTokenGranter,ClientCredentialsTokenGranter,RefreshTokenGranter,ImplicitTokenGranter,ResourceOwnerPasswordTokenGranter
TokenServices分為兩類,一個是用在AuthenticationServer端,AuthorizationServerTokenServices,ResourceServer端有自己的tokenServices接口,
BearerTokenExtractor,從其可以看出,token的獲取順序,Header,parameters(get/post)
spring security 保護自己的配置,作為ResourceServer的權限配置和作為AuthorizationServer的配置都是在不同的地方
An OAuth 2 authentication token can contain two authentications: one for the client(OAuth2 Client) and one for the user. Since some OAuth authorization grants don’t require user authentication, the user authentication may be null.
jwt是不需要存儲access_toen的,jwt的機制就是將所有的信息都存在了token里面,從JwtTokenStore也可以看出來
OAuth2AuthenticationManager是密切與token認證相關的,而不是與獲取token密切相關的。這是真正認證的地方,一會重點debug,resourceIds
每一個ResourceServer在配置的時候,
ResourceServerConfiguration,需要配置一個resourceID,一個ResourceServer只能配置一個
oauth/token = 先驗證的是clientid和clientsecret,接著在驗證username和userpassword,都是用的ProvideManager,兩次驗證是兩個不同的請求,oauth2客戶端會使用RestTemplate請求服務器的接口
ClientCredentialsTokenEndpointFilter用來驗證clientId和clientsecret的:?
OAuth2ClientAuthenticationProcessingFilter:OAuth2客戶端用來從OAuth2認證服務器獲取access token,也可以從OAuth2認證服務器加載authentication對象到OAuth2客戶端的SecurityContext對象中;里面調用OAuth2AuthenticationManager#authenticate()方法使用DefaultTokenServices ,DefaultTokenServices 使用JwtTokenStore,JwtTokenStore使用JwtAccessTokenConverter來將JWT解密成Auth對象。 來從AuthServer請求授權信息
accessToken被解密成如下的JWT對象:?
{“alg”:”RS256”,”typ”:”JWT”} {“exp”:1503758022,”user_name”:”admin”,”authorities”:[“ROLE_TRUSTED_CLIENT”,”ROLE_ADMIN”,”ROLE_USER”],”jti”:”d56f43d2-6c4a-46cf-85f3-050ee195a2bd”,”client_id”:”confidential”,”scope”:[“read”]} [128 crypto bytes]
AbstractSecurityInterceptor#befroeInvaction 是ResourceServer獲取認證信息的地方
只要是需要驗證token有效性的都需要jwt.key-uri的配置
AffirmativeBased值得debug
?
TIPS
這兩種方式有差別,牽扯到UsernamePasswordAuthenticationFilter和ClientCredentialsTokenEndpointFilter
security:?oauth2:client:client-id: confidentialclient-secret: secretaccess-token-uri: http://localhost:8080/oauth/tokenuser-authorization-uri: http://localhost:8080/oauth/authorizeuse-current-uri: trueresource:?jwt:key-uri: http://localhost:8080/oauth/token_keyfilter-order: 3 ?client里面的配置最終是用來生成OAuth2ProtectedResourceDetails的bean的,參看OAuth2ProtectedResourceDetailsConfiguration
整體代碼結構如下:資料和源碼來源?
總結
以上是生活随笔為你收集整理的java B2B2C springmvc mybatis电子商务平台源码-Spring Cloud Security的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (四)java B2B2C Spring
- 下一篇: Docker之 默认桥接网络与自定义桥接