当前位置: 首页> 汽车> 时评 > SpringBoot3集成Spring Authorization Server搭建服务认证中心

SpringBoot3集成Spring Authorization Server搭建服务认证中心

时间:2025/7/10 0:20:09来源:https://blog.csdn.net/liu320yj/article/details/141746226 浏览次数: 0次

1. 概述

OAuth 是描述授权过程的开放标准,它可用于授权用户访问 API,OAuth 授权服务器负责对用户进行身份验证并颁发包含用户数据和适当访问策略的访问令牌。之前介绍过低版本的集成方案SpringCloud搭建微服务之OAuth2.1认证和授权,在新版本中集成SpringBoot更加方便和简洁,只需要在application.yml中配置就行

2. 版本说明

SpringBoot:3.3.3
Spring Authorization Server:1.3.2
JDK:17

3. 搭建认证中心

可以直接使用Spring Initializr搭建基础框架,选择需要的maven依赖

3.1. 引入核心依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</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-oauth2-authorization-server</artifactId>
</dependency>

3.2. 编写application.yml配置文件

server:port: 9000spring:security:user:name: adminpassword: 123456oauth2:authorizationserver:issuer: http://localhost:9000client:messageing-client:registration:client-id: messageing-clientclient-secret: '{noop}secret'client-name: Articles Clientclient-authentication-methods:- client_secret_basicauthorization-grant-types:- authorization_code- refresh_tokenredirect-uris:- http://127.0.0.1:8080/login/oauth2/code/messageing-client-oidc- http://127.0.0.1:8080/authorizedscopes:- openid- profile- message.read- message.writerequire-authorization-consent: true

3.3. 编写主启动类

编写一个SpringBoot项目的主启动类

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

至此就完成了认证中心的搭建

后记

如果不想直接配置在application.yml文件中,也可以编写config配置文件,如果需要持久化用户登录信息和客户端信息,需要在config配置文件中声明,如下所示:

@Bean
UserDetailsService userDetailsService() {PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();UserDetails userDetails = User.builder().username("admin").password("123456").passwordEncoder(encoder::encode).roles("USER").build();return new InMemoryUserDetailsManager(userDetails);
}
关键字:SpringBoot3集成Spring Authorization Server搭建服务认证中心

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: