手把手教你搭建一个基于OAuth2.1规范的授权认证服务器(一):附源码

[复制链接]
发表于 2025-5-10 02:23:53 | 显示全部楼层 |阅读模式
媒介

技术更新:Spring Security OAuth已经逐渐被镌汰,而Spring Authorization Server是其官方保举的替代方案。它提供了更现代、更安全的授权服务器实现,符合最新的OAuth 2.1规范。
项目需求:小灰工作上的项目需要搭建一个授权服务器,而原先使用的Spring Security OAuth,而该项目已经逐渐被镌汰,固然网上有许多相关教程和资料,但考虑到技术的更新迭代,决定采用Spring Authorization Server来实现。
功能实现:该项目包括认证授权中央、资源服务器、客户端和服务端。通过使用Spring Authorization Server,可以确保这些功能安全性和可靠性,同时遵循最新的安全尺度。
项目功能

授权中央Server:进行认证、授权,并发放token、刷新token,不负责token鉴权(由资源服务器自行鉴权);
客户端Client:面向用户的操作入口;向Server哀求token,携带token访问Resource;
资源服务器Resource:提供资源,需要携带token哀求,可以自行鉴权;
项目布局


项目搭建

说明:项目相关架包、数据库脚本相关信息在项目里,具体可链接下载。
授权中央Server(spring-authorization-server):
application.yaml
  1. server:
  2.   port: 9000
  3.   servlet:
  4.     session:
  5.       cookie:
  6.         name: OAuth-Auth-Server
  7. logging:
  8.   level:
  9.     root: info
  10.     org.springframework.web: info
  11.     org.springframework.security: trace
  12.     org.springframework.security.oauth2: trace
  13. spring:
  14.   thymeleaf:
  15.     cache: false
  16.     prefix: classpath:/templates
  17. #    check-template-location: true
  18.     suffix: .html
  19.     mode: HTML
  20.   datasource:
  21.     driver-class-name: com.mysql.cj.jdbc.Driver
  22.     url: jdbc:mysql://ip:3306/oauth2.0?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
  23.     username: root
  24.     password: ********
复制代码
新建AuthorizationServer相关设置
  1. @Configuration
  2. @EnableWebSecurity
  3. public class AuthorizationServerConfig {
  4.    
  5.     @Autowired
  6.     private PasswordEncoder passwordEncoder;
  7.     /**
  8.      * 授权服务器 SecurityFilterChain
  9.      * @param http
  10.      * @return
  11.      * @throws Exception
  12.      */
  13.     @Bean
  14.     @Order(Ordered.HIGHEST_PRECEDENCE)
  15.     public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http)
  16.             throws Exception {
  17.    
  18.         OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
  19.         http.getConfigurer(OAuth2AuthorizationServerConfigurer.class)
  20.                 .oidc(Customizer.withDefaults());  // Enable OpenID Connect 1.0
  21.         http
  22.                 // 未从授权终结点进行身份验证时重定向到登录页面
  23.                 .exceptionHandling((exceptions) -> exceptions
  24.                         .defaultAuthenticationEntryPointFor(
  25.                                 new LoginUrlAuthenticationEntryPoint("/login"),
  26.                                 new MediaTypeRequestMatcher(MediaType.TEXT_HTML)
  27.                         )
  28.                 )
  29.                 // 接受用户信息和/或客户端注册的访问令牌
  30.                 .oauth2ResourceServer((resourceServer) -> resourceServer
  31.                         .jwt(Customizer.withDefaults()));
  32.         return http.build();
  33.     }
  34.     /**
  35.      * 配置认证相关的过滤器链
  36.      *
  37.      * @param http spring security核心配置
  38.      * @return 过滤器链
  39.      * @throws Exception 抛出
  40.      */
  41.     @Bean
  42.     @Order(2)
  43.     SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
  44.    
  45.         http.authorizeHttpRequests(authorizeRequests ->
  46.                 authorizeRequests
  47.                 // 允许的请求
  48.                 .requestMatchers(HttpMethod.GET, "/test").
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表