亚洲精品久久久久久第一页-人妻少妇精彩视品一区二区三区-91国产自拍免费视频-免费一级a在线播放视频正片-少妇天天日天天射天天爽-国产大屁股喷水视频在线观看-操美女骚穴抽插性爱视频-亚洲 欧美 中文字幕 丝袜-成人免费无码片在线观看

單點登錄失敗解決措施 單點登錄框架有哪些( 三 )


頒發(fā)登陸成功令牌構(gòu)建令牌配置對象本次我們借助JWT(Json Web Token-是一種json格式)方式將用戶相關(guān)信息進行組織和加密,并作為響應(yīng)令牌(Token),從服務(wù)端響應(yīng)到客戶端,客戶端接收到這個JWT令牌之后,將其保存在客戶端(例如localStorage),然后攜帶令牌訪問資源服務(wù)器,資源服務(wù)器獲取并解析令牌的合法性,基于解析結(jié)果判定是否允許用戶訪問資源.
package com.jt.auth.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.oauth2.provider.token.TokenStore;import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;@Configurationpublic class TokenConfig {//定義簽名key,在執(zhí)行令牌簽名需要這個key,可以自己指定.private String SIGNING_KEY = "auth";//定義令牌生成策略.@Beanpublic TokenStore tokenStore() {return new JwtTokenStore(jwtAccessTokenConverter());}//定義Jwt轉(zhuǎn)換器,負責生成jwt令牌,解析令牌內(nèi)容@Beanpublic JwtAccessTokenConverter jwtAccessTokenConverter(){JwtAccessTokenConverter converter=new JwtAccessTokenConverter();//設(shè)置加密/解密口令converter.setSigningKey(SIGNING_KEY);return converter;}}定義認證授權(quán)核心配置第一步:在SecurityConfig中添加如下方法(創(chuàng)建認證管理器對象,后面授權(quán)服務(wù)器會用到):
@Beanpublic AuthenticationManager authenticationManagerBean()throws Exception {return super.authenticationManagerBean();}第二步:所有零件準備好了開始拼裝最后的主體部分,這個主體部分就是授權(quán)服務(wù)器的核心配置
package com.jt.auth.config;import lombok.AllArgsConstructor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.HttpMethod;import org.springframework.security.authentication.AuthenticationManager;import org.springframework.security.core.userdetails.UserDetailsService;import org.springframework.security.crypto.password.PasswordEncoder;import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;import org.springframework.security.oauth2.provider.token.DefaultTokenServices;import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;import org.springframework.security.oauth2.provider.token.TokenStore;import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;import java.util.Arrays;/** * 完成所有配置的組裝,在這個配置類中完成認證授權(quán),JWT令牌簽發(fā)等配置操作 * 1)SpringSecurity (安全認證和授權(quán)) * 2)TokenConfig * 3)Oauth2(暫時不說) */@AllArgsConstructor@Configuration@EnableAuthorizationServer //開啟認證和授權(quán)服務(wù)public class Oauth2Config extends AuthorizationServerConfigurerAdapter {//此對象負責完成認證管理private AuthenticationManager authenticationManager;//TokenStore負責完成令牌創(chuàng)建,信息讀取private TokenStore tokenStore;//JWT令牌轉(zhuǎn)換器(基于用戶信息構(gòu)建令牌,解析令牌)private JwtAccessTokenConverter jwtAccessTokenConverter;//密碼加密匹配器對象private PasswordEncoder passwordEncoder;//負責獲取用戶信息信息private UserDetailsService userDetailsService;//設(shè)置認證端點的配置(/oauth/token),客戶端通過這個路徑獲取JWT令牌@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {endpoints//配置認證管理器.authenticationManager(authenticationManager)//驗證用戶的方法獲得用戶詳情.userDetailsService(userDetailsService)//要求提交認證使用post請求方式,提高安全性.allowedTokenEndpointRequestMethods(HttpMethod.POST,HttpMethod.GET)//要配置令牌的生成,由于令牌生成比較復雜,下面有方法實現(xiàn).tokenServices(tokenService());//這個不配置,默認令牌為UUID.randomUUID().toString()}//定義令牌生成策略@Beanpublic AuthorizationServerTokenServices tokenService(){//這個方法的目標就是獲得一個令牌生成器DefaultTokenServices services=new DefaultTokenServices();//支持令牌刷新策略(令牌有過期時間)services.setSupportRefreshToken(true);//設(shè)置令牌生成策略(tokenStore在TokenConfig配置了,本次我們應(yīng)用JWT-定義了一種令牌格式)services.setTokenStore(tokenStore);//設(shè)置令牌增強(允許設(shè)置令牌生成策略,默認是非jwt方式,現(xiàn)在設(shè)置為jwt方式,并在令牌Payload部分允許添加擴展數(shù)據(jù),例如用戶權(quán)限信息)TokenEnhancerChain chain=new TokenEnhancerChain();chain.setTokenEnhancers(Arrays.asList(jwtAccessTokenConverter));services.setTokenEnhancer(chain);//設(shè)置令牌有效期services.setAccessTokenValiditySeconds(3600);//1小時//刷新令牌應(yīng)用場景:一般在用戶登錄系統(tǒng)后,令牌快過期時,系統(tǒng)自動幫助用戶刷新令牌,提高用戶的體驗感services.setRefreshTokenValiditySeconds(3600*72);//3天return services;}//設(shè)置客戶端詳情類似于用戶詳情@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory()//客戶端id (客戶端訪問時需要這個id).withClient("gateway-client")//客戶端秘鑰(客戶端訪問時需要攜帶這個密鑰).secret(passwordEncoder.encode("123456"))//設(shè)置權(quán)限.scopes("all")//all只是個名字而已和寫abc效果相同//允許客戶端進行的操作這里的認證方式表示密碼方式,里面的字符串千萬不能寫錯.authorizedGrantTypes("password","refresh_token");}// 認證成功后的安全約束配置,對指定資源的訪問放行,我們登錄時需要訪問/oauth/token,需要對這樣的url進行放行@Overridepublic void configure(AuthorizationServerSecurityConfigurer security) throws Exception {//認證通過后,允許客戶端進行哪些操作security//公開oauth/token_key端點.tokenKeyAccess("permitAll()")//公開oauth/check_token端點.checkTokenAccess("permitAll()")//允許提交請求進行認證(申請令牌).allowFormAuthenticationForClients();}}配置網(wǎng)關(guān)認證的URL- id: router02uri: lb://sca-authpredicates:#- Path=/auth/login/**#沒要令牌之前,以前是這樣配置- Path=/auth/oauth/**#微服務(wù)架構(gòu)下,需要令牌,現(xiàn)在要這樣配置filters:- StripPrefix=1Postman訪問測試第一步:啟動服務(wù)依次啟動sca-auth服務(wù),sca-resource-gateway服務(wù) 。


以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時就醫(yī)或請專業(yè)人士給予相關(guān)指導!

「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助: