日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java B2B2C springmvc mybatis电子商务平台源码-Spring Cloud Security

發(fā)布時(shí)間:2025/5/22 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java B2B2C springmvc mybatis电子商务平台源码-Spring Cloud Security 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、SpringCloud Security簡介
??????????Spring Cloud Security提供了一組原語,用于構(gòu)建安全的應(yīng)用程序和服務(wù),而且操作簡便。可以在外部(或集中)進(jìn)行大量配置的聲明性模型有助于實(shí)現(xiàn)大型協(xié)作的遠(yuǎn)程組件系統(tǒng),通常具有中央身份管理服務(wù)。它也非常易于在Cloud Foundry等服務(wù)平臺中使用。在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知識點(diǎn)

封裝順序是這樣的:spring security及其各個(gè)模塊=》spring cloud security=》spring boot autoconfigure的security部分,比如autoconfigure模塊有個(gè)spring security的sso,是對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)用啟動的時(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不會幫助spring Security構(gòu)建ClientCredentialsResourceDetails 對象,需要開發(fā)者自己創(chuàng)建

favicon.icon,記得把這個(gè)東西過濾掉,奶奶的

在其中一個(gè)邊緣服務(wù)上,您可能需要啟用單點(diǎn)登錄。

@EnableOAuthSso,只需要在邊緣服務(wù)器指定沒用來引導(dǎo)未登錄的用戶登錄系統(tǒng)。@EnableOAuthSso將允許您將未經(jīng)認(rèn)證的用戶的自動重定向轉(zhuǎn)向授權(quán)服務(wù)器,他們將能夠登錄
EnableOAuth2Client,在中間中繼的時(shí)候用
ClientCredentialsTokenEndpointFilter,AS設(shè)置了allowFormAuthenticationForClients才會有,詳情看這里面的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是不需要存儲access_toen的,jwt的機(jī)制就是將所有的信息都存在了token里面,從JwtTokenStore也可以看出來

OAuth2AuthenticationManager是密切與token認(rèn)證相關(guān)的,而不是與獲取token密切相關(guān)的。這是真正認(rèn)證的地方,一會重點(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è)不同的請求,oauth2客戶端會使用RestTemplate請求服務(wù)器的接口

ClientCredentialsTokenEndpointFilter用來驗(yàn)證clientId和clientsecret的:?

OAuth2ClientAuthenticationProcessingFilter:OAuth2客戶端用來從OAuth2認(rèn)證服務(wù)器獲取access token,也可以從OAuth2認(rèn)證服務(wù)器加載authentication對象到OAuth2客戶端的SecurityContext對象中;里面調(diào)用OAuth2AuthenticationManager#authenticate()方法使用DefaultTokenServices ,DefaultTokenServices 使用JwtTokenStore,JwtTokenStore使用JwtAccessTokenConverter來將JWT解密成Auth對象。 來從AuthServer請求授權(quán)信息
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獲取認(rèn)證信息的地方

只要是需要驗(yàn)證token有效性的都需要jwt.key-uri的配置

AffirmativeBased值得debug
?
TIPS

@Overridepublic void configure(WebSecurity web) throws Exception {web.ignoring().antMatchers("/favicon.ico");}@Bean@Overrideprotected UserDetailsService userDetailsService(){InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();manager.createUser(User.withUsername("user").password("password").roles("USER").authorities("USER").build());manager.createUser(User.withUsername("admin").password("password").roles("USER", "ADMIN", "TRUSTED_CLIENT").authorities("USER").build());return manager;}

這兩種方式有差別,牽扯到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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。