Spring Cloud進階之路 | 九:資源服務(Spring Cloud Oauth2)


轉載請注明作者及出處:

作者:銀河架構師

原文鏈接:https://www.cnblogs.com/luas/p/12201421.html

​如上一篇文章Spring Cloud進階之路 | 八:授權服務(Spring Cloud Oauth2)中所述,授權服務和資源服務總是同時存在的,本文即針對資源服務器相關配置進行講解。

 

搭建資源服務器

復用之前文章中的xmall-product商品工程,進行相關改造。

添加依賴

添加spring-cloud-starter-oauth2依賴,修改后的pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion><parent>
    <groupId>com.luas.cloud</groupId>
    <artifactId>java-boot-parent-2.1</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <relativePath>../../java-boot-parent-2.1</relativePath>
  </parent><groupId>com.luas.cloud</groupId>
  <artifactId>xmall-product</artifactId>
  <version>1.0.0-SNAPSHOT</version><name>xmall-product</name>
  <description>Spring Cloud Learning,nacos-client</description><dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency><dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-oauth2</artifactId>
    </dependency><!-- nacos cloud -->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency><dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency><dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies><build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build></project>

 

Oauth2資源服務器配置

在application.yml中進行oauth2資源器信息配置,user信息獲取地址為文章Spring Cloud進階之路 | 八:授權服務(Spring Cloud Oauth2)中定義的用戶信息查詢端點-http://localhost:7777/oauth/user。

server:
  port: 8080
​
security:
  oauth2:
    resource:
      user-info-uri: http://localhost:7777/oauth/user
      prefer-token-info: false

 

開啟資源服務器

添加@EnableResourceServer注解即可開啟資源服務器,有兩種方法添加該注解。

第一種,直接在啟動類添加。

package com.luas.xmall;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
​
@EnableResourceServer
@SpringBootApplication
public class XmallProductApplication {
​
  public static void main(String[] args) {
    SpringApplication.run(XmallProductApplication.class, args);
  }
​
}

 

第二種,如果針對資源服務器有相關配置,則需要添加ResourceServerConfiguration,此時可在此類添加。

package com.luas.xmall.configuration;
​
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
​
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .requestMatchers()
                .anyRequest()
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated();
    }
}

 

如資源服務器沒有特殊配置,可直接在啟動類添加@EnableResourceServer注解。

 

身份認證

先啟動xmall-auth授權服務器,端口為7777。然后再啟動xmall-product工程,端口為8080。

先通過Postman訪問http://localhost8080/sku/1122,發現不能像之前一樣正常訪問了。

通過返回結果中的信息,可以發現一些蛛絲馬跡,大概率是因為沒有授權,身份認證未通過。

結合前篇文章Spring Cloud進階之路 | 八:授權服務(Spring Cloud Oauth2)中授權流程的介紹,基本已可以確認是身份認證環節出了問題,當前用戶沒有經過授權,即訪問受保護的資源。

此時,解決方法有兩種,如果當前請求是公共資源,無需身份認證,那么,需要在資源服務配置中,允許該資源可以未經授權訪問。

package com.luas.xmall.configuration;
​
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
​
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .requestMatchers()
                .anyRequest()
                .and()
                .authorizeRequests()
                .antMatchers("/application").permitAll()
                .anyRequest()
                .authenticated();
    }
}

 

重啟xmall-product工程,再次訪問http://localhost:8080/application,可以正常訪問。

如果當前請求資源不是公共資源,那么就需要先從授權服務器請求授權,然后攜帶授權再次訪問該資源即可。

先從授權服務器請求授權。

攜帶該授權(header方式)訪問資源。

資源服務器改造完成!

 

源碼

github

https://github.com/liuminglei/SpringCloudLearning/tree/master/09/

gitee

https://gitee.com/xbd521/SpringCloudLearning/tree/master/09/

 

 

微信搜索【銀河架構師】,發現更多精彩內容。

技術資料領取方法:關注公眾號,回復微服務,領取微服務相關電子書;回復MK精講,領取MK精講系列電子書;回復JAVA 進階,領取JAVA進階知識相關電子書;回復JAVA面試,領取JAVA面試相關電子書,回復JAVA WEB領取JAVA WEB相關電子書。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM