javascript
gateway oauth2 对称加密_SpringCloud(六) oauth2认证中心(单点登陆)
1.介紹
在Spring Cloud需要使用OAUTH2來實現多個微服務的統一認證授權,通過向OAUTH服務發送某個類型的grant type進行集中認證和授權,從而獲得access_token,而這個token是受其他微服務信任的,我們在后續的訪問可以通過access_token來進行,從而實現了微服務的統一認證授權
OAuth 2.0定義了四種授權方式。
密碼模式(resource owner password credentials)
授權碼模式(authorization code)
簡化模式(implicit)
客戶端模式(client credentials)
密碼模式(resource owner password credentials)這種模式是最不推薦的,因為client可能存了用戶密碼 這種模式主要用來做遺留項目升級為oauth2的適配方案 當然如果client是自家的應用,也是可以
支持refresh token
授權碼模式(authorization code)這種模式算是正宗的oauth2的授權模式設計了auth code,通過這個code再獲取token支持refresh token
簡化模式(implicit)這種模式比授權碼模式少了code環節,回調url直接攜帶token這種模式的使用場景是基于瀏覽器的應用這種模式基于安全性考慮,建議把token時效設置短一些不支持refresh token
客戶端模式(client credentials)這種模式直接根據client的id和密鑰即可獲取token,無需用戶參與這種模式比較合適消費api的后端服務,比如拉取一組用戶信息等不支持refresh token,主要是沒有必要 refresh token的初衷主要是為了用戶體驗不想用戶重復輸入賬號密碼來換取新token,因而設計了refresh token用于換取新token
這種模式由于沒有用戶參與,而且也不需要用戶賬號密碼,僅僅根據自己的id和密鑰就可以換取新token,因而沒必要refresh token
代碼模塊展示
SSO 設計分為服務端和客戶端2個部分,sso服務端為每個應用提供了統一的訪問控制和授權認證服務,在模塊merchant-sso中進行開發和包含用戶登陸設計 主頁設計 認證服務設計等
merchant-sso 服務端 配置
pom.xml的配置
server:
port: 8000
session:
cookie:
name: SESSIONID
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
encrypt:
failOnError: false
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/myzipkin?characterEncoding=utf8&useSSL=false
username: myzipkin
password: myzipkin
# 初始化大小,最小,最大
initialSize: 5
minIdle: 5
maxActive: 20
# 配置獲取連接等待超時的時間
maxWait: 60000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打開PSCache,并且指定每個連接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
#配置監控統計攔截的filters,去掉后監控界面sql將無法統計,'wall'用于防火墻
filters: stat,wall,log4j
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多個DruidDataSource的監控數據
#useGlobalDataSourceStat=true
jpa:
database: MYSQL
show-sql: false
## Hibernate ddl auto (validate|create|create-drop|update)
hibernate:
ddl-auto: none
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
--- ##FeignClient超時設置
spring.cloud.loadbalancer.retry.enabled: true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:10000
ribbon.ConnectTimeout: 250 ribbon.ReadTimeout: 1000
ribbon.OkToRetryOnAllOperations: true
ribbon.MaxAutoRetriesNextServer: 2 ribbon.MaxAutoRetries: 1
bootstrap.yml的配置
啟動類
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
@EnableHystrix
@EnableFeignClients(basePackages = “com.demo”)
@ComponentScan(basePackages = “com.demo”)
public class MerchantSsoApplication {
public static void main(String[] args) {
SpringApplication.run(MerchantSsoApplication.class, args);
}
}
認證中心 主要類
@Configuration
@EnableAuthorizationServer
public class OAuthConfigurer extends AuthorizationServerConfigurerAdapter {
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
//非對稱加密
KeyPair keyPair = new KeyStoreKeyFactory(new ClassPathResource(
"keystore.jks"), "tc123456".toCharArray()).getKeyPair("tycoonclient");
//對稱加密
// converter.setSigningKey("123");
converter.setKeyPair(keyPair);
return converter;
}
@Override
public void configure(ClientDetailsServiceConfigurer clients)
throws Exception {
clients.inMemory().withClient("ssoclient").secret("ssosecret")
.autoApprove(true)
.authorizedGrantTypes("authorization_code
總結
以上是生活随笔為你收集整理的gateway oauth2 对称加密_SpringCloud(六) oauth2认证中心(单点登陆)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: amd显卡显存测试程序_AMD发布Rad
- 下一篇: 导出滴滴行程单_身穿统一的绿马甲!滴滴货