Spring Security 5.x+OAuth2.0+OIDC1.0

本文记录了Spring Boot应用中整合Spring Security、OAuth2.0和OIDC1.0的过程,包括准备工作、创建OAuth2服务器、资源服务器和身份提供者服务的详细步骤,以及在配置和运行过程中遇到的问题和解决方案。
摘要由CSDN通过智能技术生成

Spring Security+OAuth2.0+OIDC1.0



前言

之前学习springboot集成spring security的OAauth2.1和OIDC1.0遇到的问题及成功情况,这里做个笔记下次好查看。


一、准备工作

1.Spring Security相关依赖包

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
...
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			  <artifactId>spring-security-oauth2-authorization-server</artifactId>
			  <version>0.3.0</version>
		</dependency>
		 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
		</dependency>

		<dependency>
			<groupId>com.nimbusds</groupId>
			<artifactId>oauth2-oidc-sdk</artifactId>
		</dependency>

2.先看下Authorization Code Grant经典图

在这里插入图片描述

2.还是画个结构草图

workflow

二、创建对应微服务

1.创建springboot-oauth2-server服务

结构如下所示:
在这里插入图片描述

核心类MyAuthorizationServerConfig如下:

package com.example.demo.config;
...
@EnableWebSecurity
public class MyAuthorizationServerConfig {
   

    @Bean
    @Order(1)
    public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
   
        OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
//                // Redirect to the login page when not authenticated from the
//                // authorization endpoint
//        http.exceptionHandling((exceptions) -> exceptions
//                        .authenticationEntryPoint(
//                                new LoginUrlAuthenticationEntryPoint("/login"))
//                );
        http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);

        //customized authorizationEndpoint,redirectionEndpoint,tokenEndpoint,userInfoEndpoint
//        http.oauth2Login(oauth2 -> oauth2.userInfoEndpoint(userinfo ->
//        userinfo.oidcUserService(oauth2UserService())
//        )
//        .oauth2.tokenEndpoint(...)
//        );

        //if defined http.exceptionHandling then no need to define http.formLogin(Customizer.withDefaults())
        return http.formLogin(Customizer.withDefaults()).build();
    }


    //for authorization_code purpose
    @Bean
    @Order(2)
    public SecurityFilterChain standardSecurityFilterChain(HttpSecurity http) throws Exception {
   
        http.authorizeHttpRequests
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值