kong 删除oauth2生成的Token

创建API

curl -i -X POST \
    --url http://localhost:8001/apis/ \
    --data 'name=example-api' \
    --data 'uris=/user' \
    --data 'upstream_url=http://test.my'

返回值

{
    "created_at":1513756862000,
    "strip_uri":true,
    "id":"47b03eba-2394-448e-99cd-5b2b9e43ead7",
    "name":"example-api",
    "http_if_terminated":false,
    "preserve_host":false,
    "upstream_url":"http://test.my",
    "uris":[
        "/user"
    ],
    "upstream_connect_timeout":60000,
    "upstream_send_timeout":60000,
    "upstream_read_timeout":60000,
    "retries":5,
    "https_only":false
}

关联oauth插件

curl -X POST \http://localhost:8001/apis/example-api/plugins \
    --data "name=oauth2" \
    --data "config.enable_authorization_code=true" \
    --data "config.scopes=email,phone,address" \
    --data "config.mandatory_scope=true"

返回值

{
    "created_at":1513757015000,
    "config":{
        "token_expiration":7200,
        "mandatory_scope":true,
        "hide_credentials":false,
        "enable_authorization_code":true,
        "enable_implicit_grant":false,
        "global_credentials":false,
        "scopes":[
            "email",
            "phone",
            "address"
        ],
        "enable_password_grant":false,
        "accept_http_if_already_terminated":false,
        "anonymous":"",
        "enable_client_credentials":false,
        "provision_key":"qKu4d1T3XfJUI1bFz5I36zTelX3Bg2xs"
    },
    "id":"f34b7df6-a43f-4872-8958-61838392fc25",
    "name":"oauth2",
    "api_id":"47b03eba-2394-448e-99cd-5b2b9e43ead7",
    "enabled":true
}

创建消费者

curl -X POST http://localhost:8001/consumers/ \
    --data "username=user123"

返回值

{
    "created_at":1513756931000,
    "username":"user123",
    "id":"96a3dddd-3e04-45f2-aaf0-ab4fd63a322e"
}

创建应用

curl -X POST http://localhost:8001/consumers/96a3dddd-3e04-45f2-aaf0-ab4fd63a322e/oauth2 \
    --data "name=test-app" \
    --data "redirect_uri=http://test.my"

返回值

{
    "client_id":"7IzBLyXHFTptd3kuHNpGU4z3hHql7NMD",
    "created_at":1513756974000,
    "id":"ac02108b-2ff0-4866-b9a5-2b89360c3898",
    "redirect_uri":[
        "http://test.my"
    ],
    "name":"test-app",
    "client_secret":"NLx8FJtSFXvu9I3WtMjwCNmzaNWn0D4m",
    "consumer_id":"96a3dddd-3e04-45f2-aaf0-ab4fd63a322e"
}

查看应用信息

curl localhost:8001/oauth2?client_id=7IzBLyXHFTptd3kuHNpGU4z3hHql7NMD

返回值

{
    "total":1,
    "data":[
        {
            "created_at":1513756974000,
            "client_id":"7IzBLyXHFTptd3kuHNpGU4z3hHql7NMD",
            "id":"ac02108b-2ff0-4866-b9a5-2b89360c3898",
            "redirect_uri":[
                "http://test.my"
            ],
            "name":"test-app",
            "client_secret":"NLx8FJtSFXvu9I3WtMjwCNmzaNWn0D4m",
            "consumer_id":"96a3dddd-3e04-45f2-aaf0-ab4fd63a322e"
        }
    ]
}

获取Token

获取code

curl -X POST https://localhost:8443/user/oauth2/authorize \
    --data "client_id=7IzBLyXHFTptd3kuHNpGU4z3hHql7NMD" \
    --data "response_type=code" \
    --data "provision_key=qKu4d1T3XfJUI1bFz5I36zTelX3Bg2xs" \
    --data "authenticated_userid=0" \
    --data "scope=email" \
    --insecure

返回值

{
    "redirect_uri":"http://test.my?code=PiY5UtiNY3FnFi1XRGPFKoPOodxtuPx1"
}

获取Token

curl -X POST https://localhost:8443/user/oauth2/token \
    --data client_id=7IzBLyXHFTptd3kuHNpGU4z3hHql7NMD \
    --data client_secret=NLx8FJtSFXvu9I3WtMjwCNmzaNWn0D4m \
    --data provision_key=qKu4d1T3XfJUI1bFz5I36zTelX3Bg2xs \
    --data code=PiY5UtiNY3FnFi1XRGPFKoPOodxtuPx1 \
    --data grant_type=authorization_code \
    --insecure

返回值

{
    "refresh_token":"ipbs2amg6kLpTMwWbafORFSDhBPTVogG",
    "token_type":"bearer",
    "access_token":"K0BcshombAFvRi04sS2mdm6RlgGHw4uG",
    "expires_in":7200
}

验证Token

curl -X GET https://localhost:8443/user?access_token=K0BcshombAFvRi04sS2mdm6RlgGHw4uG \
    --insecure

返回值

<html>
    <head><title>502 Bad Gateway</title></head>
    <body bgcolor="white">
        <center><h1>502 Bad Gateway</h1></center>
        <hr><center>nginx/1.10.3</center>
    </body>
</html>

忽略返回值哈;

删除Token

curl -X DELETE http://localhost:8001/oauth2_tokens/K0BcshombAFvRi04sS2mdm6RlgGHw4uG \
    --insecure

没有返回值

再次请求api

curl -X GET https://localhost:8443/user?access_token=K0BcshombAFvRi04sS2mdm6RlgGHw4uG \
    --insecure

返回值

{
    "error_description":"The access token is invalid or has expired",
    "error":"invalid_token"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
OAuth2生成Token的过程通常是用户登录成功后,向授权服务器发送授权请求,授权服务器验证用户身份并生成Token,然后将Token返回给客户端。客户端在接下来的请求中使用Token进行身份验证和授权。 以下是一个基于Spring Security OAuth2的Token生成示例: 1. 添加Maven依赖 ```xml <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.4.0.RELEASE</version> </dependency> ``` 2. 配置OAuth2服务器 ```java @Configuration @EnableAuthorizationServer public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private DataSource dataSource; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.jdbc(dataSource); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); } } ``` 3. 配置Spring Security ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/oauth/**").permitAll().anyRequest().authenticated().and().csrf().disable(); } @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } } ``` 4. 添加授权请求 ```java @Controller @RequestMapping("/oauth2") public class OAuth2Controller { @Autowired private TokenStore tokenStore; @RequestMapping(value = "/token", method = RequestMethod.POST) @ResponseBody public ResponseEntity<?> getToken(@RequestParam("username") String username, @RequestParam("password") String password) { UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); OAuth2Authentication authentication = (OAuth2Authentication) authenticationManager.authenticate(authenticationToken); OAuth2AccessToken token = tokenStore.getAccessToken(authentication); return ResponseEntity.ok(token); } } ``` 以上代码仅供参考,具体实现需要根据实际场景进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值