OAuth2 token个人笔记

本文详细解析了SpringSecurity OAuth2框架下密码模式的使用方式,包括通过请求头和参数传递token,以及在配置中声明使用加密规则的重要性和具体实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.spring security oauth2简易的分为三个步骤

  • 配置资源服务器

  • 配置认证服务器

  • 配置spring security

2.oauth2根据使用场景不同,分成了4种模式

  • 授权码模式(authorization code)

  • 简化模式(implicit)

  • 密码模式(resource owner password credentials)

  • 客户端模式(client credentials)

下面是我调研的密码模式使用方式:

1.基本认证可以通过请求头传递

键值对:Authorization=bearer13b22000-1224-4ac2-b467-bb0f376dfd87

这里不区分大小写,在请求头中携带token验证可以访问需要认证的接口。

 

这里访问check_token接口时,需要把客户端基本认证加上,参数token=41c96126-019c-47bb-887d-7100dc2e7670,即可访问。

http://127.0.0.1:1203/oauth/check_token?token=41c96126-019c-47bb-887d-7100dc2e7670  

2.基本认证也同样可以通过参数传递

http://127.0.0.1:1203/api/member?access_token=41c96126-019c-47bb-887d-7100dc2e7670

没有配置.secret(new BCryptPasswordEncoder().encode("browser") ),并且也没有声明使用加密

http://127.0.0.1:1203/oauth/token?grant_type=password&client_id=browser&username=1&password=1   post请求

如果你使用了密码加密规则,那么客户端密码也需要以同样的规则声明。并且在使用的时候一定要加上客户端密码,不能只使用客户端账号来进行认证。而且需要在相关configure方法中配置.secret(new BCryptPasswordEncoder().encode("browser") ),没有配置不行,因为使用了加密方式OAuth2会进行校验。

@Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
       // clients.withClientDetails(clientDetails());
        clients.inMemory()
                .withClient("browser")
                .secret(new BCryptPasswordEncoder().encode("browser") )
                .authorizedGrantTypes("refresh_token", "password")
                .scopes("read","write");
    }

http://127.0.0.1:1203/oauth/token?grant_type=password&username=1&password=1&client_id=browser&client_secret=browser

http://127.0.0.1:1203/oauth/token?grant_type=refresh_token&refresh_token=78fc9d39-c9ea-42d1-a5e7-12f83ac770f6&client_id=browser   post请求

http://127.0.0.1:1203/oauth/token?grant_type=refresh_token&refresh_token=78fc9d39-c9ea-42d1-a5e7-12f83ac770f6&client_id=browser&client_secret=browser   post请求

参数传递 不支持check_token接口,该接口只可以使用请求头传递

 

 


 
 

OAuth2的access_token是用于访问受保护的资源时进行身份验证和授权的凭证。通过将access_token附加到请求头或请求参数中,客户端可以向资源服务器发送请求并获得资源的访问权限。access_token通常是使用JWT(JSON Web Token)进行签名的,以确保其安全性和完整性。然而,access_token只提供了身份验证的功能,而没有提供有关用户的其他信息。这就是为什么在OAuth2中还存在idToken的原因。idToken是一个包含用户相关信息的令牌,如用户ID、姓名、电子邮件等。它通常被用于提供身份验证和用户信息的完整性,并且可以通过认证服务器的/token端点进行验证。因此,通过同时使用access_token和idToken,客户端可以获得对资源的访问权限,并获得关于用户的更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【笔记】浅谈OAuth2 accessToken和OIDC idToken的理解和使用场景](https://blog.youkuaiyun.com/qq_26878363/article/details/115394602)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [springsecurity-oauth2令牌 access_token验证](https://blog.youkuaiyun.com/clonetx/article/details/125395593)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值