首页 > 其他分享 >spring-securty-oauth2使用例子

spring-securty-oauth2使用例子

时间:2024-04-27 15:11:38浏览次数:16  
标签:oauth2 securty spring springframework import oauth org security

oauth2概念

https://www.cnblogs.com/LQBlog/p/16996125.html

环境搭建

1.引入依赖

   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>

 

凭证模式

package com.yxt.datax.auth;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;

/*
[/oauth/authorize]
[/oauth/token]
[/oauth/check_token]
[/oauth/confirm_access]
[/oauth/token_key]
[/oauth/error]
*/
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    private final BCryptPasswordEncoder passwordEncoder= new BCryptPasswordEncoder();
    /**
     * :用来配置客户端详情信息,一般使用数据库来存储或读取应用配置的详情信息(client_id ,client_secret,redirect_uri 等配置信息)。
     * @param clients
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        super.configure(clients);
        //基于内存模式定义一个oauth2客户端
        clients.inMemory()
                .withClient("client_1") //客户端id
                .authorizedGrantTypes("client_credentials")//oatuh2 凭证模式
                .scopes("all","read", "write")
                .authorities("client_credentials")//oatuh2 凭证模式
                .accessTokenValiditySeconds(7200)//token有效期
                 //使用passwordEncoder对密码进行加密,正常是存在数据库里面
                .secret(passwordEncoder.encode("123456"));//客户端secret
    }

    /**
     * 用来配置令牌端点(Token Endpoint)的安全与权限访问。
     * @param security
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        super.configure(security);
        //后续根据用户输入的密码来做encode后做比较
        security.passwordEncoder(passwordEncoder);
    }

    /**
     * 用来配置授权以及令牌(Token)的访问端点和令牌服务(比如:配置令牌的签名与存储方式)
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        super.configure(endpoints);
    }
}

posman调用

crul

curl --location 'http://localhost:8080/oauth/token' \
--header 'Authorization: Basic Y2xpZW50XzE6MTIzNDU2' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: JSESSIONID=E1211820CB66DAA0880897446BEEB01A' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'scope=read'
View Code

 

 

标签:oauth2,securty,spring,springframework,import,oauth,org,security
From: https://www.cnblogs.com/LQBlog/p/18162078

相关文章

  • spring boot
    链接:https://pan.baidu.com/s/1quiC-bqO5s3KgoLT5MWX7Q?pwd=412p提取码:412p1.Springboot入门springboot-简化了开发-比如-我们之前导入依赖--到需要自己写配置类-返回Beanspringboot帮我们简化了这个工程SpringBoot提供了一种快速使用Spring的方式,基于约定优于配置的思想sp......
  • 有意思!一个关于 Spring 历史的在线小游戏
    发现SpringOne的官网上有个好玩的彩蛋,分享给大家!进到SpringOne的官网,可以看到右下角有个类似马里奥游戏中的金币图标。点击该金币之后,会打开一个新的页面,进入下面这样一个名为:TheHistoryOfSpring的在线小游戏你可以使用上下左右的方向键来控制Spring的Logo一步步经历......
  • 一个通用的SpringBoot项目响应实体类Response
    packagecom.luky.vo;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;importlombok.ToString;importorg.springframework.http.HttpStatus;@Data@ToString@AllArgsConstructor@NoArgsConstructorpublicclassResponse&......
  • SpringBoot+MyBatisPlus报错 Invalid value type for attribute 'factoryBeanObjectTy
    依赖版本org.springframework.boot:spring-boot-starter-web:3.2.5com.baomidou:mybatis-plus-boot-starter:3.5.5错误Invalidvaluetypeforattribute'factoryBeanObjectType'问题原因:这个问题是由于依赖传递导致,在MyBatis起步依赖中的myBatis-spring版本过低,导致程......
  • spring-接口大全
    1.Bean相关1.InitializingBeanInitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法。demo@ComponentpublicclassMyInitBeanimplementsInitializingBean{publicvoidafterPro......
  • springboot链接redis IPV6
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><exclusions><exclusion>......
  • Spring Boot应用中如何动态指定数据库,实现不同用户不同数据库的场景
    当在SpringBoot应用程序中使用SpringDataJPA进行数据库操作时,配置Schema名称是一种常见的做法。然而,在某些情况下,模式名称需要是动态的,可能会在应用程序运行时发生变化。比如:需要做数据隔离的SaaS应用。所以,这篇博文将帮助您解决了在SpringBoot应用程序中如何设置动态S......
  • springBoot源码(一)
    构造函数运行代码publicConfigurableApplicationContextrun(String...args){ Startupstartup=Startup.create(); if(this.registerShutdownHook){ SpringApplication.shutdownHook.enableShutdownHookAddition(); } DefaultBootstrapContextbootstrapConte......
  • SpringSecurity认证授权完整流程
    SpringSecurity认证流程:loadUserByUsername()方法内部实现。实现步骤:   构建一个自定义的service接口,实现SpringSecurity的UserDetailService接口。建一个service实现类,实现此loadUserByUsername方法。调用登录的login接口,会经过authenticationManager.authenticate(authent......
  • SpringBoot整合AOP实现打印方法执行时间切面
    pom.xml<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency>代码创建注解importjava.lang.annotation.ElementType;importja......