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

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://www.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)

綿陽服務器托管
日本av天堂中文字幕| 国产精品五月婷婷六月丁香| 免费国产网站在线观看不卡| 国产福利在线观看午夜| 欧美日韩久久亚洲精品| 欧美日韩专区一区二区三区| 国产成人综合亚洲乱淫.| 人妻乱人伦中文字幕在线| 国产性做爰片免费网站| 免费国产污网站在线观看| 亚欧熟女乱色一二三区日韩| 国产麻豆剧传媒国产av| 国产精品不卡一不卡二| 一卡二卡精品在线免费| 97视频精品全部免费观看| 午夜精品一区二区亚洲| 蜜桃av在线观看一区二区| 日本一区中文字幕怎么用| 中文字幕人妻丝袜二区| 日本人妻风俗店中文字幕| 日韩精品中文女同在线播放| 久久久国产精品9999综合| 中文字幕乱码在线观看一区| 日韩av不卡免费播放| 久久精品性少妇一区=区三区| 麻豆国产97在线精品一区| 国产成人av三级在线观看| 精品国产女同一区二区| 国产视频三级在线观看| 成人午夜福利影院在线| 国产日韩欧美在线精品| 亚洲女同中文字幕在线| 播放欧美日韩特黄大片| 九九热超在线视频精品| 亚欧熟女乱色一二三区日韩| 亚洲av成人在线资源| 精品女同一区二区三区久久| 日韩三级精品一区二区| 色91精品在线观看剧情| 国产午夜福利不卡在线观看| 久久这里只有精品视频六|