SpringBoot通过OAuth token访问资源

1. 引入依赖

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>feignoauth</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>feignoauth</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <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>
            <version>2.2.5.RELEASE</version>
        </dependency>
         <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2. 配置文件

server.port=8081

security.oauth2.resource.tokenInfoUri=http://localhost:8080/oauth/check_token
security.oauth2.resource.preferTokenInfo=true
#从认证服务器上验证token
security.oauth2.client.accessTokenUri=http://localhost:8080/oauth/token
security.oauth2.client.userAuthorizationUri=http://localhost:8080/oauth/authorize
security.oauth2.client.clientId=csdn
security.oauth2.client.clientSecret=123456

3. 拦截配置

package com.example.feignoauth.config;

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 ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
    
    
    @Override
    public void configure(HttpSecurity http) throws Exception {
    
    
        // 对 api/oauth 请求进行拦截
        http.authorizeRequests().antMatchers("/api/oauth/**").authenticated();
    }
}

4. 创建controller

package com.example.feignoauth.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/oauth")
public class OauthController {
    
    

    @RequestMapping("/test")
    public Object test(@RequestHeader("Authorization") String authorization) {
    
    
        String token = authorization.substring(authorization.indexOf(" ") + 1);
        Claims body = Jwts.parser()
        		//需要与认证服务器的jwt key相同
                .setSigningKey("jwtkey".getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token)
                .getBody();
        log.info("test,{}", body);
        return body;
    }

}

5. 启动类

package com.example.feignoauth;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableOAuth2Sso
public class FeignoauthApplication {
    
    

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

}

6. 获取token

在这里插入图片描述

7. 不带token请求

在这里插入图片描述

8. 带token请求

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJmaXNoZXIiLCJzY29wZSI6WyJhbGwiXSwiZXhwIjoxNjIwOTg3NjkxLCJ1c2VyTmFtZSI6ImZpc2hlciIsImF1dGhvcml0aWVzIjpbIlJPTEVfVVNFUiJdLCJqdGkiOiJhODI1MDYwZC01MGM3LTQyMGMtODhjNi1jOTdhYjM2NDg5YmMiLCJjbGllbnRfaWQiOiJjc2RuIn0.3sGETJ8zZK2H9fEJDI6xx0O46L2WCZgZBiHL2P-_Fko

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_40977118/article/details/116758230