Spring Cloud系列 Spring Cloud OAuth2密码模式(resource owner password credentials)

本文主要介绍Spring Cloud OAuth2中的密码模式(resource owner password credentials)。在这个模式下,客户端直接携带用户凭证(用户名和密码)向授权服务器申请令牌。流程包括:客户端请求令牌,使用令牌访问服务,服务验证令牌。文章还提供了应用配置、测试步骤,包括使用Postman进行测试。
摘要由CSDN通过智能技术生成

OAuth 2 有四种授权模式,分别是授权码模式(authorization code)、简化模式(implicit)、密码模式(resource owner password credentials)、客户端模式(client credentials),具体 OAuth2 是什么,可以参考这篇文章(http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html)
————————————————
上篇讲解的是授权码模式,本篇主要讲解密码模式。平常主要使用的就是这两种模式。(注:本篇基于上一篇的代码基础之上)

1.授权流程

1.客户端带着clientid,screteid,用户名,密码。到授权服务器获取token。

2.客户端获取到token之后,带着token访问服务。

3.服务获取客户端带过来的token,到授权服务器进行验证token的合法性。有效允许访问服务,无效反之。就是如此简单。

到这。我们开发人员需要实现的就第一,第二步。第三步由我们配置验证url,Spring Cloud OAuth2框架会自动帮我们完成验证。

项目组成authe-server 权限验证服务在上一篇博客已经实现,本篇只需要事项client-user用户需要访问的服务。

DemoApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class DemoApplication {

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

}
ResourceServerConfig.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * Created by Administrator on 2020-06-12.
 */
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled =
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值