如上一篇文章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/
本文系【银河架构师】原创,如需转载请在文章明显处注明作者及出处。
微信搜索【银河架构师】,发现更多精彩内容。