詳解基于Spring Cloud幾行配置完成單點(diǎn)登錄開發(fā)
單點(diǎn)登錄概念
單點(diǎn)登錄(Single Sign On),簡稱為 SSO,是目前比較流行的企業(yè)業(yè)務(wù)整合的解決方案之一。SSO的定義是在多個(gè)應(yīng)用系統(tǒng)中,用戶只需要登錄一次就可以訪問所有相互信任的應(yīng)用系統(tǒng)。登錄邏輯如上圖
基于Spring 全家桶的實(shí)現(xiàn)
技術(shù)選型:
- Spring Boot
- Spring Cloud
- Spring Security oAuth2
客戶端:
maven依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-jwt</artifactId> </dependency>
EnableOAuth2Sso 注解
入口類配置@@EnableOAuth2Sso
@SpringBootApplication
public class PigSsoClientDemoApplication {
public static void main(String[] args) {
SpringApplication.run(PigSsoClientDemoApplication.class, args);
}
}
配置文件
security:
oauth2:
client:
client-id: pig
client-secret: pig
user-authorization-uri: http://localhost:3000/oauth/authorize
access-token-uri: http://localhost:3000/oauth/token
scope: server
resource:
jwt:
key-uri: http://localhost:3000/oauth/token_key
sessions: never
SSO認(rèn)證服務(wù)器
認(rèn)證服務(wù)器配置
@Configuration
@Order(Integer.MIN_VALUE)
@EnableAuthorizationServer
public class PigAuthorizationConfig extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient(authServerConfig.getClientId())
.secret(authServerConfig.getClientSecret())
.authorizedGrantTypes(SecurityConstants.REFRESH_TOKEN, SecurityConstants.PASSWORD,SecurityConstants.AUTHORIZATION_CODE)
.scopes(authServerConfig.getScope());
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
.tokenStore(new RedisTokenStore(redisConnectionFactory))
.accessTokenConverter(jwtAccessTokenConverter())
.authenticationManager(authenticationManager)
.exceptionTranslator(pigWebResponseExceptionTranslator)
.reuseRefreshTokens(false)
.userDetailsService(userDetailsService);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
.allowFormAuthenticationForClients()
.tokenKeyAccess("isAuthenticated()")
.checkTokenAccess("permitAll()");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
jwtAccessTokenConverter.setSigningKey(CommonConstant.SIGN_KEY);
return jwtAccessTokenConverter;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Springboot不能自動(dòng)提交數(shù)據(jù)庫連接問題
在使用SSM框架開發(fā)時(shí),若在同一Service內(nèi)部方法間互相調(diào)用,直接使用this關(guān)鍵字會(huì)導(dǎo)致事務(wù)管理失效,從而引發(fā)如數(shù)據(jù)庫連接不足等問題,原因是通過this調(diào)用不會(huì)經(jīng)過Spring的代理,因此不會(huì)自動(dòng)進(jìn)行事務(wù)處理2024-09-09
如何將Spring Session存儲(chǔ)到Redis中實(shí)現(xiàn)持久化
這篇文章主要介紹了如何將Spring Session存儲(chǔ)到Redis中實(shí)現(xiàn)持久化,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
使用Java實(shí)現(xiàn)創(chuàng)建Excel表單控件
在數(shù)據(jù)填報(bào)時(shí),創(chuàng)建Excel表單控件是一項(xiàng)常見的任務(wù),它可以極大地簡化數(shù)據(jù)收集和處理的過程,本文主要介紹了如何使用Java實(shí)現(xiàn)創(chuàng)建Excel表單控件,感興趣的可以了解下2024-03-03
SpringBoot中的@EnableAutoConfiguration注解解析
這篇文章主要介紹了SpringBoot中的@EnableAutoConfiguration注解解析,@EnableAutoConfiguration也是借助@Import的幫助,將所有符合自動(dòng)配置條件的bean定義注冊到IoC容器,需要的朋友可以參考下2023-09-09
java實(shí)現(xiàn)分布式項(xiàng)目搭建的方法
這篇文章主要介紹了java實(shí)現(xiàn)分布式項(xiàng)目搭建的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
SpringBoot自動(dòng)配置實(shí)現(xiàn)流程詳細(xì)分析
這篇文章主要介紹了SpringBoot自動(dòng)配置原理分析,SpringBoot是我們經(jīng)常使用的框架,那么你能不能針對SpringBoot實(shí)現(xiàn)自動(dòng)配置做一個(gè)詳細(xì)的介紹。如果可以的話,能不能畫一下實(shí)現(xiàn)自動(dòng)配置的流程圖。牽扯到哪些關(guān)鍵類,以及哪些關(guān)鍵點(diǎn)2022-12-12
Spring?Boot提高開發(fā)效率必備工具lombok使用
這篇文章主要為大家介紹了Spring?Boot提高開發(fā)效率的必備工具lombok使用方法示例及步驟說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03
Windows環(huán)境使用bat腳本啟動(dòng)Java服務(wù)的過程
Java項(xiàng)目一般會(huì)被打包成jar后啟動(dòng),在windows系統(tǒng)中可以通過終端窗口cmd啟動(dòng)jar包,即在jar包所在的目錄中打開cmd,或在cmd中進(jìn)入到j(luò)ar包目錄,這篇文章主要介紹了Windows環(huán)境使用bat腳本啟動(dòng)Java服務(wù),需要的朋友可以參考下2023-08-08

