javascript
java B2B2C springmvc mybatis电子商务平台源码-Spring Cloud Security
一、SpringCloud Security簡介
??????????Spring Cloud Security提供了一組原語,用于構(gòu)建安全的應(yīng)用程序和服務(wù),而且操作簡便。可以在外部(或集中)進(jìn)行大量配置的聲明性模型有助于實(shí)現(xiàn)大型協(xié)作的遠(yuǎn)程組件系統(tǒng),通常具有中央身份管理服務(wù)。它也非常易于在Cloud Foundry等服務(wù)平臺(tái)中使用。在Spring Boot和Spring Security OAuth2的基礎(chǔ)上,可以快速創(chuàng)建實(shí)現(xiàn)常見模式的系統(tǒng),如單點(diǎn)登錄,令牌中繼和令牌交換。愿意了解源碼的朋友直接求求交流分享技術(shù):二一四七七七五六三三
功能:
從Zuul代理中的前端到后端服務(wù)中繼SSO令牌
資源服務(wù)器之間的中繼令牌
使Feign客戶端表現(xiàn)得像OAuth2RestTemplate(獲取令牌等)的攔截器
在Zuul代理中配置下游身份驗(yàn)證
二、SpringCloud Security知識(shí)點(diǎn)
封裝順序是這樣的:spring security及其各個(gè)模塊=》spring cloud security=》spring boot autoconfigure的security部分,比如autoconfigure模塊有個(gè)spring security的sso,是對(duì)spring security在oath2下的封裝
spring oauth2 authorizeserver,resourceserver,認(rèn)證服務(wù)器和資源服務(wù)器是怎么交互的,token_key,/oauth/token/ refresh/token
resourceserver就是業(yè)務(wù)系統(tǒng),oauth client 就是邊緣業(yè)務(wù)系統(tǒng),比如直接面向用戶的UI系統(tǒng),或者UI系統(tǒng)直接調(diào)用的API接口這一層;
JWT和OAuth2的區(qū)別?看到好多人在比較這兩個(gè)東西,現(xiàn)在終結(jié)這個(gè)問題:JWT只是OAuth2中的token的一種類型。
jwt client(Resource Server或者Zuul等),使用jwt的業(yè)務(wù)系統(tǒng)在解碼acces_token的時(shí)候,需要一個(gè)toke_key,這個(gè)token-key就是JWT Client應(yīng)用啟動(dòng)的時(shí)候從Auth Server拉取的,接口是token/token_key
單點(diǎn)登錄這種功能,好多javaee容器都自帶的,像websphere
spring Security完全將認(rèn)證和授權(quán)分開了,資源只需要聲明自己是需要認(rèn)證的,需要什么樣的權(quán)限,只管跟當(dāng)前用戶要access_token。
授權(quán)的四種方式任意,只要拿到token就可以去讓資源去認(rèn)證。
邊緣服務(wù)器需要開啟@EnableOAuth2Sso,但是邊緣服務(wù)器也是一個(gè)ResourceServer,邊緣服務(wù)器如果是zuul的話,就不是一個(gè)ResourceServer了,只需要添加@EnableOAuth2Sso注解就可以了;
client_credentials模式下spring boot不會(huì)幫助spring Security構(gòu)建ClientCredentialsResourceDetails 對(duì)象,需要開發(fā)者自己創(chuàng)建
favicon.icon,記得把這個(gè)東西過濾掉,奶奶的
在其中一個(gè)邊緣服務(wù)上,您可能需要啟用單點(diǎn)登錄。
@EnableOAuthSso,只需要在邊緣服務(wù)器指定沒用來引導(dǎo)未登錄的用戶登錄系統(tǒng)。@EnableOAuthSso將允許您將未經(jīng)認(rèn)證的用戶的自動(dòng)重定向轉(zhuǎn)向授權(quán)服務(wù)器,他們將能夠登錄
EnableOAuth2Client,在中間中繼的時(shí)候用
ClientCredentialsTokenEndpointFilter,AS設(shè)置了allowFormAuthenticationForClients才會(huì)有,詳情看這里面的AuthorizationServerSecurityConfigurer#configure(HttpSecurity http)邏輯,這點(diǎn)非常重要,ClientCredentialsTokenEndpointFilter是用來驗(yàn)證clientid和client_secret的,使用clientid和client_secret換取下一步的東西;
TokenGranter,AuthorizationCodeTokenGranter,ClientCredentialsTokenGranter,RefreshTokenGranter,ImplicitTokenGranter,ResourceOwnerPasswordTokenGranter
TokenServices分為兩類,一個(gè)是用在AuthenticationServer端,AuthorizationServerTokenServices,ResourceServer端有自己的tokenServices接口,
BearerTokenExtractor,從其可以看出,token的獲取順序,Header,parameters(get/post)
spring security 保護(hù)自己的配置,作為ResourceServer的權(quán)限配置和作為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是不需要存儲(chǔ)access_toen的,jwt的機(jī)制就是將所有的信息都存在了token里面,從JwtTokenStore也可以看出來
OAuth2AuthenticationManager是密切與token認(rèn)證相關(guān)的,而不是與獲取token密切相關(guān)的。這是真正認(rèn)證的地方,一會(huì)重點(diǎn)debug,resourceIds
每一個(gè)ResourceServer在配置的時(shí)候,
ResourceServerConfiguration,需要配置一個(gè)resourceID,一個(gè)ResourceServer只能配置一個(gè)
oauth/token = 先驗(yàn)證的是clientid和clientsecret,接著在驗(yàn)證username和userpassword,都是用的ProvideManager,兩次驗(yàn)證是兩個(gè)不同的請(qǐng)求,oauth2客戶端會(huì)使用RestTemplate請(qǐng)求服務(wù)器的接口
ClientCredentialsTokenEndpointFilter用來驗(yàn)證clientId和clientsecret的:?
OAuth2ClientAuthenticationProcessingFilter:OAuth2客戶端用來從OAuth2認(rèn)證服務(wù)器獲取access token,也可以從OAuth2認(rèn)證服務(wù)器加載authentication對(duì)象到OAuth2客戶端的SecurityContext對(duì)象中;里面調(diào)用OAuth2AuthenticationManager#authenticate()方法使用DefaultTokenServices ,DefaultTokenServices 使用JwtTokenStore,JwtTokenStore使用JwtAccessTokenConverter來將JWT解密成Auth對(duì)象。 來從AuthServer請(qǐng)求授權(quán)信息
accessToken被解密成如下的JWT對(duì)象:?
{“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獲取認(rèn)證信息的地方
只要是需要驗(yàn)證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
整體代碼結(jié)構(gòu)如下:資料和源碼來源?
總結(jié)
以上是生活随笔為你收集整理的java B2B2C springmvc mybatis电子商务平台源码-Spring Cloud Security的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (四)java B2B2C Spring
- 下一篇: Docker之 默认桥接网络与自定义桥接