• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Spring Authorization Server 四资源服务器搭建

武飞扬头像
Rommel8888
帮助1

学新通

资源服务器搭建

在spring-oauth-parent父工程下,创建spring-oauth-resource子工程,添加spring-boot-starter-oauth2-resource-server依赖,跟客户端工程一样,由于此处与Spring Authorization Server框架无关,可不用必须使用SpringBoot3,但为了版本统一,在此还是继续使用父工程中的SpringBoot3。

pom.xml添加依赖如下。

<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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.oauth</groupId>
        <artifactId>spring-oauth-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>spring-oauth-resource</artifactId>
    <name>资源服务器</name>

    <dependencies>

        <!--spring-boot-starter-oauth2-resource-server-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
        </dependency>
        <!--spring-boot-starter-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

</project>

      application.yml配置如下

server:
  port: 9002

logging:
  level:
    org.springframework.security: trace

spring:
  application:
    name: spring-oauth-resource
  security:
    oauth2:
      resource-server:
        jwt:
          issuer-uri: http://spring-oauth-server:9000

启动类如下。

@SpringBootApplication
public class SpringOauthResourceApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringOauthResourceApplication.class,args);
    }
}

      Spring Security 配置类如下。

@Configuration
@EnableWebSecurity
@EnableMethodSecurity(jsr250Enabled = true, securedEnabled = true)
public class ResourceServerConfig {

	@Bean
	public SecurityFilterChain securityFilterChain(HttpSecurity http)
			throws Exception {
		http.authorizeHttpRequests(authorize -> authorize
                        //所有的访问都需要通过身份认证
						.anyRequest().authenticated()
				)
				.oauth2ResourceServer(oauth2 -> oauth2
								.jwt(Customizer.withDefaults())

				);
		return http.build();

	}
}

     添加MessagesController类作为测试,代码如下。

@RestController
public class MessagesController {

    @GetMapping("/messages1")
    public String getMessages1() {
        return " hello Message 1";
    }

    @GetMapping("/messages2")
    @PreAuthorize("hasAuthority('SCOPE_profile')")
    public String getMessages2() {
        return " hello Message 2";
    }

    @GetMapping("/messages3")
    @PreAuthorize("hasAuthority('SCOPE_Message')")
    public String getMessages3() {
        return " hello Message 3";
    }

}

上面的MessagesController类中,有三个接口,分别是messages1、messages2、messages3,由于接口资源地址没有在Spring Security配置中放开,因此三个接口访问都需要传入accessToken。其中,messages1接口的要求只需传入accessToken,messages2接口要求传入accessToken和拥有profile权限,messages3接口要求传入accessToken和拥有Message权限,

资源服务器工程结构如下。

学新通

资源服务器测试

为了测试方便,可以将accessToken的过期时间设置长一点,修改表oauth2_registered_client中token_settings字段的settings.token.access-token-time-to-live的值,默认插入的是5分钟,可以设置成30分钟,甚至更长。

学新通

      postman直接访问http://localhost:9002/messages1地址,不带token,则返回如下401结果。

学新通

使用http://spring-oauth-client:9001/token地址,向客户端发起请求,获取token,accessToken的jwt解析信息如下。

学新通

从上面的jwt解析信息中可以看到,当前accessToken拥有openid、profile权限。

然后请求http://localhost:9002/messages1地址,带上accessToken,则返回如下成功结果。

学新通

再请求http://localhost:9002/messages2地址,带上accessToken,同样返回成功,结果如下。

学新通

再请求http://localhost:9002/messages3地址,带上accessToken,则返回403了,结果如下。

学新通

打开Headers信息,看到WWW-Authenticate返回错误信息了,意思是权限不足,因为messages3接口需要有Message权限。

学新通

总结

本文简单介绍了oauth2资源服务器的搭建及测试,资源服务器往往和客户端一起配合使用,客户端侧重于身份认证,资源服务器侧重于权限校验,如果在Spring Cloud(Alibaba)微服务架构中,可以将客户端框架pring-boot-starter-oauth2-client集成到网关服务上,将资源服务器框架spring-boot-starter-oauth2-resource-server集成到用户服务、商品服务、订单服务等微服务上。

项目代码地址:地址链接

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhiaeige
系列文章
更多 icon
同类精品
更多 icon
继续加载