oauth2 java 获取token_OAuth2简易实战(一)-四种模式

本文介绍了OAuth2的四种授权模式:授权码模式,涉及授权服务器和资源服务器的配置;隐式授权模式,适用于无服务器端应用;密码模式,适用于用户高度信任的应用;客户端凭证模式,用于应用程序以自身名义交互。每种模式都详细阐述了配置、操作步骤和适用场景。
摘要由CSDN通过智能技术生成

1. OAuth2简易实战(一)-四种模式

1.1. 授权码授权模式(Authorization code Grant)

1.1.1. 流程图

fbcf70c55c31829b7a891bae8a004608.png

1.1.2. 授权服务器配置

配置授权服务器中 client,secret,redirectUri,授权模式,权限配置

//授权服务器配置

@Configuration

@EnableAuthorizationServer

public class OAuth2AuthorizationServer extends

AuthorizationServerConfigurerAdapter {

@Override

public void configure(ClientDetailsServiceConfigurer clients)

throws Exception {

clients.inMemory()

.withClient("clientapp")

.secret("112233")

.redirectUris("http://localhost:9001/callback")

// 授权码模式

.authorizedGrantTypes("authorization_code")

.scopes("read_userinfo", "read_contacts");

}

}

1.1.3. 资源服务器配置

配置需要资源授权的接口地址

//资源服务配置

@Configuration

@EnableResourceServer

public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter {

@Override

public void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()

.anyRequest()

.authenticated()

.and()

.requestMatchers()

.antMatchers("/api/**");

}

}

1.1.4. 操作步骤

浏览器请求下列地址,获取授权code,请求参数client_id,redirect_uri回调地址,response_type响应类型,scope权限

http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=code&scope=read_userinfo

输入用户名密码,该密码为Spring Security的登路密码,application.properties里配置

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值