一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - Spring Cloud下基于OAUTH2認證授權的實現示例

Spring Cloud下基于OAUTH2認證授權的實現示例

2021-04-10 13:24智頂筆記 JAVA教程

這篇文章主要介紹了Spring Cloud下基于OAUTH2認證授權的實現示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

spring cloud需要使用oauth2來實現多個微服務的統一認證授權,通過向oauth服務發送某個類型的grant type進行集中認證和授權,從而獲得access_token,而這個token是受其他微服務信任的,我們在后續的訪問可以通過access_token來進行,從而實現了微服務的統一認證授權。

本示例提供了四大部分:

  1. discovery-service:服務注冊和發現的基本模塊
  2. auth-server:oauth2認證授權中心
  3. order-service:普通微服務,用來驗證認證和授權
  4. api-gateway:邊界網關(所有微服務都在它之后)

oauth2中的角色:

  1. resource server:被授權訪問的資源
  2. authotization server:oauth2認證授權中心
  3. resource owner: 用戶
  4. client:使用api的客戶端(如android 、ios、web app)

grant type:

  1. authorization code:用在服務端應用之間
  2. implicit:用在移動app或者web app(這些app是在用戶的設備上的,如在手機上調起微信來進行認證授權)
  3. resource owner password credentials(password):應用直接都是受信任的(都是由一家公司開發的,本例子使用
  4. client credentials:用在應用api訪問。

1.基礎環境

使用postgres作為賬戶存儲,redis作為token存儲,使用docker-compose在服務器上啟動postgresredis

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
redis:
 image: sameersbn/redis:latest
 ports:
 - "6379:6379"
 volumes:
 - /srv/docker/redis:/var/lib/redis:z
 restart: always
 
postgresql:
 restart: always
 image: sameersbn/postgresql:9.6-2
 ports:
 - "5432:5432"
 environment:
 - debug=false
 
 - db_user=wang
 - db_pass=yunfei
 - db_name=order
 volumes:
 - /srv/docker/postgresql:/var/lib/postgresql:z

2.auth-server

2.1 oauth2服務配置

redis用來存儲token,服務重啟后,無需重新獲取token.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@configuration
@enableauthorizationserver
public class authorizationserverconfig extends authorizationserverconfigureradapter {
 @autowired
 private authenticationmanager authenticationmanager;
 @autowired
 private redisconnectionfactory connectionfactory;
 
 
 @bean
 public redistokenstore tokenstore() {
  return new redistokenstore(connectionfactory);
 }
 
 
 @override
 public void configure(authorizationserverendpointsconfigurer endpoints) throws exception {
  endpoints
    .authenticationmanager(authenticationmanager)
    .tokenstore(tokenstore());
 }
 
 @override
 public void configure(authorizationserversecurityconfigurer security) throws exception {
  security
    .tokenkeyaccess("permitall()")
    .checktokenaccess("isauthenticated()");
 }
 
 @override
 public void configure(clientdetailsserviceconfigurer clients) throws exception {
  clients.inmemory()
    .withclient("android")
    .scopes("xx") //此處的scopes是無用的,可以隨意設置
    .secret("android")
    .authorizedgranttypes("password", "authorization_code", "refresh_token")
   .and()
    .withclient("webapp")
    .scopes("xx")
    .authorizedgranttypes("implicit");
 }
}

2.2 resource服務配置

auth-server提供user信息,所以auth-server也是一個resource server

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@configuration
@enableresourceserver
public class resourceserverconfig extends resourceserverconfigureradapter {
 
 @override
 public void configure(httpsecurity http) throws exception {
  http
    .csrf().disable()
    .exceptionhandling()
    .authenticationentrypoint((request, response, authexception) -> response.senderror(httpservletresponse.sc_unauthorized))
   .and()
    .authorizerequests()
    .anyrequest().authenticated()
   .and()
    .httpbasic();
 }
}
?
1
2
3
4
5
6
7
8
@restcontroller
public class usercontroller {
 
 @getmapping("/user")
 public principal user(principal user){
  return user;
 }
}

2.3 安全配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@configuration
public class securityconfig extends websecurityconfigureradapter {
 
 
 
 @bean
 public userdetailsservice userdetailsservice(){
  return new domainuserdetailsservice();
 }
 
 @bean
 public passwordencoder passwordencoder() {
  return new bcryptpasswordencoder();
 }
 
 @override
 protected void configure(authenticationmanagerbuilder auth) throws exception {
  auth
    .userdetailsservice(userdetailsservice())
    .passwordencoder(passwordencoder());
 }
 
 @bean
 public securityevaluationcontextextension securityevaluationcontextextension() {
  return new securityevaluationcontextextension();
 }
 
 //不定義沒有password grant_type
 @override
 @bean
 public authenticationmanager authenticationmanagerbean() throws exception {
  return super.authenticationmanagerbean();
 }
 
}

2.4 權限設計

采用用戶(sysuser) 角色(sysrole) 權限(sysauthotity)設置,彼此之間的關系是多對多。通過domainuserdetailsservice 加載用戶和權限。

2.5 配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
spring:
 profiles:
 active: ${spring_profiles_active:dev}
 application:
  name: auth-server
 
 jpa:
 open-in-view: true
 database: postgresql
 show-sql: true
 hibernate:
  ddl-auto: update
 datasource:
 platform: postgres
 url: jdbc:postgresql://192.168.1.140:5432/auth
 username: wang
 password: yunfei
 driver-class-name: org.postgresql.driver
 redis:
 host: 192.168.1.140
 
server:
 port: 9999
 
 
eureka:
 client:
 serviceurl:
  defaultzone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
 
 
 
logging.level.org.springframework.security: debug
 
logging.leve.org.springframework: debug
 
##很重要
security:
 oauth2:
 resource:
  filter-order: 3

2.6 測試數據

data.sql里初始化了兩個用戶admin->role_admin->query_demo,wyf->role_user

3.order-service

3.1 resource服務配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@configuration
@enableresourceserver
public class resourceserverconfig extends resourceserverconfigureradapter{
 
 @override
 public void configure(httpsecurity http) throws exception {
  http
    .csrf().disable()
    .exceptionhandling()
    .authenticationentrypoint((request, response, authexception) -> response.senderror(httpservletresponse.sc_unauthorized))
   .and()
    .authorizerequests()
    .anyrequest().authenticated()
   .and()
    .httpbasic();
 }
}

3.2 用戶信息配置

order-service是一個簡單的微服務,使用auth-server進行認證授權,在它的配置文件指定用戶信息在auth-server的地址即可:

?
1
2
3
4
5
6
security:
 oauth2:
 resource:
  id: order-service
  user-info-uri: http://localhost:8080/uaa/user
  prefer-token-info: false

3.3 權限測試控制器

具備authorityquery-demo的才能訪問,即為admin用戶

?
1
2
3
4
5
6
7
8
@restcontroller
public class democontroller {
 @getmapping("/demo")
 @preauthorize("hasauthority('query-demo')")
 public string getdemo(){
  return "good";
 }
}

4 api-gateway

api-gateway在本例中有2個作用:

  1. 本身作為一個client,使用implicit
  2. 作為外部app訪問的方向代理

4.1 關閉csrf并開啟oauth2 client支持

?
1
2
3
4
5
6
7
8
9
@configuration
@enableoauth2sso
public class securityconfig extends websecurityconfigureradapter{
 @override
 protected void configure(httpsecurity http) throws exception {
 
  http.csrf().disable();
 }
}

4.2 配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
zuul:
 routes:
 uaa:
  path: /uaa/**
  sensitiveheaders:
  serviceid: auth-server
 order:
  path: /order/**
  sensitiveheaders:
  serviceid: order-service
 add-proxy-headers: true
 
security:
 oauth2:
 client:
  access-token-uri: http://localhost:8080/uaa/oauth/token
  user-authorization-uri: http://localhost:8080/uaa/oauth/authorize
  client-id: webapp
 resource:
  user-info-uri: http://localhost:8080/uaa/user
  prefer-token-info: false

5 演示

5.1 客戶端調用

使用postmanhttp://localhost:8080/uaa/oauth/token發送請求獲得access_token(admin用戶的如7f9b54d4-fd25-4a2c-a848-ddf8f119230b)

admin用戶

Spring Cloud下基于OAUTH2認證授權的實現示例

Spring Cloud下基于OAUTH2認證授權的實現示例

Spring Cloud下基于OAUTH2認證授權的實現示例

wyf用戶

Spring Cloud下基于OAUTH2認證授權的實現示例

Spring Cloud下基于OAUTH2認證授權的實現示例

Spring Cloud下基于OAUTH2認證授權的實現示例

5.2 api-gateway中的webapp調用

暫時沒有做測試,下次補充。

6 源碼地址

https://github.com/wiselyman/uaa-zuul

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.wisely.top/2017/06/14/spring-cloud-oauth2-zuul/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 小小水蜜桃免费影院 | 四虎影院永久网站 | 2012在线观看免费视频大全 | 99精品国产自在现线观看 | 91真人毛片一级在线播放 | 婷婷久久综合九色综合九七 | 国产最强大片免费视频 | 日本草草视频在线观看 | 日韩欧免费一区二区三区 | 97精品国产自在现线免费观看 | 四虎影院4hu | 国产成人免费片在线视频观看 | 久久视频这有精品63在线国产 | 金牛网155755水心论坛黄大父母 | 男人天堂久久 | 成人国产在线视频在线观看 | 国产色司机在线视频免费观看 | 国产原创一区二区 | 9久爱午夜视频 | 91热国内精品永久免费观看 | 喷出奶汁了h| 国产精品午夜性视频网站 | 亚洲欧美日韩综合在线 | 金莲你下面好紧夹得我好爽 | 黄 色 成 年人在线 幻女free性俄罗斯第一次摘花 | 久久精选视频 | 久久热在线视频精品店 | 日本一区二区三区视频在线观看 | 日韩精品亚洲专区在线影视 | 亚洲国产中文字幕在线视频综合 | 国产婷婷综合丁香亚洲欧洲 | 免费观看国产视频 | avtt在线| 四虎影院网址大全 | 消息称老熟妇乱视频一区二区 | 99久久精品国产免看国产一区 | 牛牛色婷婷在线视频播放 | 99久久免费视频 | 教师系列 大桥未久在线 | 青草视频网站在线观看 | 国产欧美一区二区精品性色 |