欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

SpringCloud中怎么利用OAUTH2實現(xiàn)認證授權-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關Spring Cloud中怎么利用OAUTH2實現(xiàn)認證授權,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

永定ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

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是在用戶的設備上的,如在手機上調(diào)起微信來進行認證授權)

  3. Resource Owner Password Credentials(password):應用直接都是受信任的(都是由一家公司開發(fā)的,本例子使用

  4. Client Credentials:用在應用API訪問。

1.基礎環(huán)境

使用Postgres作為賬戶存儲,Redis作為Token存儲,使用docker-compose在服務器上啟動PostgresRedis。

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 OAuth3服務配置

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

@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

@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();
 }
}
@RestController
public class UserController {

 @GetMapping("/user")
 public Principal user(Principal user){
  return user;
 }
}

2.3 安全配置

@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 配置

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:
 oauth3:
 resource:
  filter-order: 3

2.6 測試數(shù)據(jù)

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

3.order-service

3.1 Resource服務配置

@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的地址即可:

security:
 oauth3:
 resource:
  id: order-service
  user-info-uri: http://localhost:8080/uaa/user
  prefer-token-info: false

3.3 權限測試控制器

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

@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并開啟Oauth3 client支持

@Configuration
@EnableOAuth3Sso
public class SecurityConfig extends WebSecurityConfigurerAdapter{
 @Override
 protected void configure(HttpSecurity http) throws Exception {

  http.csrf().disable();
 }
}

4.2 配置

zuul:
 routes:
 uaa:
  path: /uaa/**
  sensitiveHeaders:
  serviceId: auth-server
 order:
  path: /order/**
  sensitiveHeaders:
  serviceId: order-service
 add-proxy-headers: true

security:
 oauth3:
 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 客戶端調(diào)用

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

admin用戶

Spring Cloud中怎么利用OAUTH2實現(xiàn)認證授權

Spring Cloud中怎么利用OAUTH2實現(xiàn)認證授權

Spring Cloud中怎么利用OAUTH2實現(xiàn)認證授權

wyf用戶

Spring Cloud中怎么利用OAUTH2實現(xiàn)認證授權

Spring Cloud中怎么利用OAUTH2實現(xiàn)認證授權

Spring Cloud中怎么利用OAUTH2實現(xiàn)認證授權

看完上述內(nèi)容,你們對Spring Cloud中怎么利用OAUTH2實現(xiàn)認證授權有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)頁名稱:SpringCloud中怎么利用OAUTH2實現(xiàn)認證授權-創(chuàng)新互聯(lián)
路徑分享:http://aaarwkj.com/article44/ccpjee.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、小程序開發(fā)品牌網(wǎng)站建設、定制開發(fā)、微信公眾號、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務器托管
精品欧美熟妇高潮喷水特黄| 亚洲欧美另类不卡专区| 一区二区三区精品人妻| 在线一区二区三区成人观看| 亚洲国产精品综合色在线| 禁止18岁以下的视频| 午夜在线免费观看小视频| 99精品欧美一区二区三区视频| 日本97久久久久久精品| 国产精品一区二区激情视频| 91日韩国产中文字幕| 欧美日韩亚洲高清专区| 日韩一二区不卡在线视频| 青青草青青草在线观看视频| 日韩欧美亚洲综合另类| 日本亚洲一区二区在线观看| 朋友的尤物人妻中文字幕| 蜜桃一区二区三区免费| 国产美女亚洲精品久久久| 亚洲av一本岛在线播放| 亚洲熟妇亚洲熟妇亚洲熟妇| 性感美女国产av一区二区三区 | 久久精品色一情一乱一伦| 中文字幕一区二区三区久久| 亚洲成人av日韩在线| 欧美老熟妇一区二区三区| 久久精品国产亚洲av久一一区| 日韩久久精品免费视频| 久久综合伊人欧美精品| 欧美三级影院网上在线| 激情内射日本一区二区三区| 自拍一区日韩二区欧美三区| 国产毛毛片一区二区三区| 81精品国产综合久久精品伦理| 免费毛片一区二区三区| 国产成人精品亚洲日本片| 18禁免费无遮挡免费视频| 精品成人在线一区二区| 69精品一区二区蜜桃视频| 中文字幕熟女av一区二区| 日本区一区二区三啪啪|