javascript
gateway oauth2 对称加密_SpringCloud(六) oauth2认证中心(单点登陆)
1.介紹
在Spring Cloud需要使用OAUTH2來實(shí)現(xiàn)多個(gè)微服務(wù)的統(tǒng)一認(rèn)證授權(quán),通過向OAUTH服務(wù)發(fā)送某個(gè)類型的grant type進(jìn)行集中認(rèn)證和授權(quán),從而獲得access_token,而這個(gè)token是受其他微服務(wù)信任的,我們在后續(xù)的訪問可以通過access_token來進(jìn)行,從而實(shí)現(xiàn)了微服務(wù)的統(tǒng)一認(rèn)證授權(quán)
OAuth 2.0定義了四種授權(quán)方式。
密碼模式(resource owner password credentials)
授權(quán)碼模式(authorization code)
簡化模式(implicit)
客戶端模式(client credentials)
密碼模式(resource owner password credentials)這種模式是最不推薦的,因?yàn)閏lient可能存了用戶密碼 這種模式主要用來做遺留項(xiàng)目升級為oauth2的適配方案 當(dāng)然如果client是自家的應(yīng)用,也是可以
支持refresh token
授權(quán)碼模式(authorization code)這種模式算是正宗的oauth2的授權(quán)模式設(shè)計(jì)了auth code,通過這個(gè)code再獲取token支持refresh token
簡化模式(implicit)這種模式比授權(quán)碼模式少了code環(huán)節(jié),回調(diào)url直接攜帶token這種模式的使用場景是基于瀏覽器的應(yīng)用這種模式基于安全性考慮,建議把token時(shí)效設(shè)置短一些不支持refresh token
客戶端模式(client credentials)這種模式直接根據(jù)client的id和密鑰即可獲取token,無需用戶參與這種模式比較合適消費(fèi)api的后端服務(wù),比如拉取一組用戶信息等不支持refresh token,主要是沒有必要 refresh token的初衷主要是為了用戶體驗(yàn)不想用戶重復(fù)輸入賬號密碼來換取新token,因而設(shè)計(jì)了refresh token用于換取新token
這種模式由于沒有用戶參與,而且也不需要用戶賬號密碼,僅僅根據(jù)自己的id和密鑰就可以換取新token,因而沒必要refresh token
代碼模塊展示
SSO 設(shè)計(jì)分為服務(wù)端和客戶端2個(gè)部分,sso服務(wù)端為每個(gè)應(yīng)用提供了統(tǒng)一的訪問控制和授權(quán)認(rèn)證服務(wù),在模塊merchant-sso中進(jìn)行開發(fā)和包含用戶登陸設(shè)計(jì) 主頁設(shè)計(jì) 認(rèn)證服務(wù)設(shè)計(jì)等
merchant-sso 服務(wù)端 配置
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
# 配置獲取連接等待超時(shí)的時(shí)間
maxWait: 60000
# 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打開PSCache,并且指定每個(gè)連接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
#配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql將無法統(tǒng)計(jì),'wall'用于防火墻
filters: stat,wall,log4j
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù)
#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超時(shí)設(shè)置
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的配置
啟動(dòng)類
@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);
}
}
認(rèn)證中心 主要類
@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
總結(jié)
以上是生活随笔為你收集整理的gateway oauth2 对称加密_SpringCloud(六) oauth2认证中心(单点登陆)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: amd显卡显存测试程序_AMD发布Rad
- 下一篇: js 字符串加减法_JavaScript