SpringSecurity(14)——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("implicit","refresh_token")
                //授权范围标识,哪部分资源可访问(all是标识,不是代表所有)
                //比如指定微服务名称,则只可以访问指定的微服务
                .scopes("all")
                //false跳转到授权页面手动点击授权,true不用手动授权,直接响应授权码
                .autoApprove(false)
                //客户端回调地址,一定要和申请授权码时用的redirect_uri一致
                //当获取授权码后,认证服务器会重定向到指定的这个URL,并且带着一个授权码code响应
                .redirectUris("http://www.baidu.com/");
    }
}

获取Access Token

接口获取

Authorization Request

  • response_type:必须的。值必须是"token"。
  • client_id:必须的。
  • redirect_uri:可选的。
  • scope:可选的。

例如:

  1. http://localhost:8080/oauth/authorize?response_type=token\&client_id=banana\&redirect_uri=http://baidu.com
  2. http://localhost:8080/oauth/authorize?response_type=token\&client_id=client1

浏览器获取

  1. 输入访问地址:http://localhost:8080/oauth/authorize?client_id=test-pc&response_type=token
  2. 当请求到达认证服务器的AuthorizationEndpoint后,它会要求资源所有者做身份验证.
  1. 点击登录后,会跳转到指定的redirect_uri,回调路径会携带者令牌access_token、expires_in、scope等
相关推荐
你好~每一天1 天前
2025年B端产品经理进阶指南:掌握这些计算机专业技能,决胜职场!
java·人工智能·经验分享·学习·产品经理·大学生
一只韩非子1 天前
Spring AI Alibaba 快速上手教程:10 分钟接入大模型
java·后端·ai编程
叫我阿柒啊1 天前
从Java全栈到云原生:一场技术深度对话
java·spring boot·docker·微服务·typescript·消息队列·vue3
ONLYOFFICE1 天前
【技术教程】如何将文档编辑器集成至基于Java的Web应用程序
java·编辑器·onlyoffice
lbwxxc1 天前
手写 Tomcat
java·tomcat
CHEN5_021 天前
【CouponHub项目开发】使用RocketMQ5.x实现延时修改优惠券状态,并通过使用模板方法模式重构消息队列发送功能
java·重构·模板方法模式·项目
杨杨杨大侠1 天前
实战案例:商品详情页数据聚合服务的技术实现
java·spring·github
杨杨杨大侠1 天前
实战案例:保险理赔线上审核系统的技术实现
java·spring·github
计算机毕设定制辅导-无忧学长1 天前
MQTT 与 Java 框架集成:Spring Boot 实战(一)
java·网络·spring boot
叫我阿柒啊1 天前
从Java全栈到Vue3实战:一次真实面试的深度复盘
java·spring boot·微服务·vue3·响应式编程·前后端分离·restful api