SpringSecurity(16)——OAuth2客户端授权模式

工作流程

基本使用

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <version>2.3.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
    <version>2.3.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.3.12.RELEASE</version>
</dependency>
java 复制代码
@Configuration
public class MyOAuth2Config {

    /**
    * 加密方式
    */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
  1. 创建安全配置类:指定认证用户的用户名和密码,用户和密码是资源的所有者,
  2. 创建认证服务器:这个客户端id和密码跟上面的用户名和密码是不一样的,客户端id和密码是应用系统的标识,每个应用系统对应一个客户端id和密码
java 复制代码
/**
 * 安全配置类
 */
@EnableWebSecurity
public class OAuth2SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;
    
    /**
     * 用户类信息
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("admin")
                .password(passwordEncoder.encode("123456"))
                .authorities("admin_roles");
    }
}
java 复制代码
/**
 * 认证服务器
 */
@Configuration
@EnableAuthorizationServer //开启认证服务器
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 配置被允许访问此认证服务器的客户端详细信息
     * 1.内存管理
     * 2.数据库管理方式
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                //客户端名称
                .withClient("test-pc")
                //客户端密码
                .secret(passwordEncoder.encode("123456"))
                //资源id,商品资源
                .resourceIds("oauth2-server")
                /**
                 * 授权类型,可同时支持多种授权类型
                 * authorization_code:授权码模式
                 * password:密码模式
                 * implicit:简化模式
                 * client_credentials:客户端模式
                 * refresh_token:更新令牌
                 */
                .authorizedGrantTypes("client_credentials","refresh_token")
                //授权范围标识,哪部分资源可访问(all是标识,不是代表所有)
                //比如指定微服务名称,则只可以访问指定的微服务
                .scopes("all")
                //false跳转到授权页面手动点击授权,true不用手动授权,直接响应授权码
                .autoApprove(false)
                //客户端回调地址,一定要和申请授权码时用的redirect_uri一致
                //当获取授权码后,认证服务器会重定向到指定的这个URL,并且带着一个授权码code响应
                .redirectUris("http://www.baidu.com/");
    }
}

获取Access Token

接口获取

Access Token Request(客户端授权模式获取Token)

  • grant_type:必须的。值必须是"client_credentials"。
  • scope:可选的。
txt 复制代码
POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials

例如:http://localhost:8080/oauth/token?client_id=test-pc\&client_secret=123456\&grant_type=client_credentials

Access Token Response

txt 复制代码
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache

{
    "access_token":"2YotnFZFEjr1zCsicMWpAA",
    "token_type":"example",
    "expires_in":3600,
    "example_parameter":"example_value"
}

Postman获取

相关推荐
张伯毅3 分钟前
Java 类型参数 T、R 、 O 、K、V 、E 、? 区别
java·开发语言
lifallen5 分钟前
Flink checkpoint
java·大数据·算法·flink
爱尚你19938 分钟前
Java并发编程:读写锁与普通互斥锁的深度对比
java·lock·readwritelock
比特森林探险记22 分钟前
Go 中 map 的双值检测写法详解
java·前端·golang
IT_Octopus23 分钟前
多线程下使用缓存+锁Lock, 出现“锁失效” + “缓存未命中竞争”的缓存击穿情况,双重检查缓存解决问题
java·spring·缓存
杰哥技术分享1 小时前
IDEA 打开文件乱码
java·ide·intellij-idea
猫头虎1 小时前
[特殊字符]解决 “IDEA 登录失败。不支持早于 14.0 的 GitLab 版本” 问题的几种方法
java·ide·网络协议·http·https·gitlab·intellij-idea
娃哈哈哈哈呀1 小时前
html-pre标签
java·前端·html
LanLance1 小时前
ES101系列09 | 运维、监控与性能优化
java·运维·后端·elasticsearch·云原生·性能优化·golang
Java永无止境1 小时前
Web前端基础:HTML-CSS
java·前端·css·html·javaweb