SpringBoot集成SpringSecurity5和OAuth2 — 4、OAuth2的资源服务器

一、项目环境配置

1.1 、JDK8

1.2、Spring Boot: 2.3.0.RELEASE

1.3、spring-cloud.version:Hoxton.SR5

二、准备工作

2.1 pom.xml

2.1.1 thymeleaf页面模板依赖



   org.springframework.boot
   spring-boot-starter-thymeleaf

2.1.2 starter-web依赖



   org.springframework.boot
   spring-boot-starter-web

2.1.3 OAuth2依赖



   org.springframework.cloud
   spring-cloud-starter-oauth2

2.1.4 数据库连接依赖



   org.mybatis.spring.boot
   mybatis-spring-boot-starter
   2.1.2




   mysql
   mysql-connector-java
   5.1.47
   runtime




   com.alibaba
   druid
   1.1.13

2.1.5 lombok



   org.projectlombok
   lombok
   true

2.1.6 单元测试依赖


   org.springframework.boot
   spring-boot-starter-test
   test
   
      
         org.junit.vintage
         junit-vintage-engine
      
   

2.1.7 完整的pom.xml文件





	4.0.0

	
		org.springframework.boot
		spring-boot-starter-parent
		2.3.0.RELEASE
		 
	

	cn.iotspider
	oauth2resource
	0.0.1-SNAPSHOT
	oauth2resource

	Demo project for Spring Boot

	
		1.8
		Hoxton.SR5
	

	

		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		

		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
		
			org.springframework.cloud
			spring-cloud-starter-oauth2
		

		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			2.1.2
		

		
		
			mysql
			mysql-connector-java
			5.1.47
			runtime
		

		
		
			com.alibaba
			druid
			1.1.13
		

		
		
			org.projectlombok
			lombok
			true
		

		
		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		

	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



2.2 application.propeties


spring.application.name=oauth2-resource
server.port=8090

mybatis.mapper-locations=classpath:mapper/*/*.xml
#将扫描到的mapper.xml显示在控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/oauth2jdbc?useUnicode=true&characterEncoding=utf8&useSSL=false&useTimezone=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

spring.thymeleaf.prefix = classpath:/templates/
#spring.thymeleaf.suffix = .html
spring.thymeleaf.encoding = UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache = false


#资源客户端配置
security.oauth2.client.client-id=client
security.oauth2.client.client-secret=secret
security.oauth2.resource.id=resource
#校验token的路径
security.oauth2.resource.token-info-uri=http://localhost:8080/oauth/check_token

三、代码:

3.1 ResourceServerConfig加入@EnableResourceServer注解


package cn.iotspider.oauth2resource.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {


    /**
     * 配置资源服务器
     *
     * @param http
     * @throws Exception
     */
    @Override
    public void configure(HttpSecurity http) throws Exception {
        // 验证所有请求
        http.authorizeRequests()
                .anyRequest()
                .authenticated();

    }

}

3.2 ResourceController

package cn.iotspider.oauth2resource.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class ResourceController {

    @RequestMapping("resource")
    public String resource() {
        return "ResourceServer Module";
    }

}

本项目只是为了用于练习搭建OAuth2的资源服务器,所以就没有加入对数据库的操作。至此,OAuth2的资源服务器就搭建完成了。

四、测试

4.1 在浏览器端输入:http://localhost:8090/resource

SpringBoot集成SpringSecurity5和OAuth2 — 4、OAuth2的资源服务器_第1张图片

上图说明在访问时没有被授权,所以没有权限请求资源,需要在请求中加入OAuth2认证服务器生成的token才行。

4.2使用PostMan测试:

按下图设置,然后再请求资源就能请求成功了

SpringBoot集成SpringSecurity5和OAuth2 — 4、OAuth2的资源服务器_第2张图片

 

完结!

 

你可能感兴趣的:(SpringBoot集成SpringSecurity5和OAuth2 — 4、OAuth2的资源服务器)