spring boot security自定义配置授权服务器实现Oauth2授权(支持密码和授权码两种模式)

本文介绍了如何在Spring Boot Security中自定义配置授权服务器,实现Oauth2授权机制,支持密码和授权码两种模式。详细步骤包括:配置application.yml和pom.xml,编写CustomAuthorizationServerConfiguration和CustomUserDetailsService,测试授权资源和两种授权模式的token获取及资源访问流程。
摘要由CSDN通过智能技术生成
               

1、application.yml

server:  port: 8888  security:  oauth2:    client:      client-id: test_client_id      client-secret: test_client_secret      scope: read,write           auto-approve-scopes: '.*'logging:  level:    org.springframework.security: info

2、pom.xml

                <dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-actuator</artifactId>  </dependency>  <dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-security</artifactId>  </dependency>  <dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-web</artifactId>  </dependency>  <dependency>   <groupId>org.springframework.security.oauth</groupId>   <artifactId>spring-security-oauth2</artifactId>  </dependency>

3、自定义授权服务器配置CustomAuthorizationServerConfiguration.java

package com.zmx.springcloud.config.authorization_server;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.bind.RelaxedPropertyResolver;import org.springframework.context.EnvironmentAware;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;import org.springframework.security.authentication.AuthenticationManager;import org.springframework.security.core.userdetails.UserDetailsService;import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;import org.springfram
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值