SpringSecurity:授权服务器与客户端应用(入门案例)

文章目录

一、需求概述


maven需要3.6.0以上版本

二、开发授权服务器

1、pom依赖

xml 复制代码
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-authorization-server</artifactId>
        </dependency>

2、yml配置

yml 复制代码
server:
  port: 9000

logging:
  level:
    org.springframework.security: trace

spring:
  security:
    # 授权服务器的账号密码
    user:
      name: admin
      password: 1111
    oauth2:
      authorizationserver:
        # 客户端配置
        client:
          myclient:
            registration:
              client-id: pzj
              client-secret: "{noop}123456"
              client-authentication-methods:
                - "client_secret_basic"
              authorization-grant-types:
                - "authorization_code"
                - "refresh_token"
              # 客户端的回调地址
              redirect-uris:
                - "http://localhost:8080/login/oauth2/code/myclient"
              # 客户端的应用首页
              post-logout-redirect-uris:
                - "http://localhost:8080/"
              scopes:
                - "openid"
                - "profile"
            require-authorization-consent: true

3、启动服务端

三、开发客户端应用

1、pom依赖

xml 复制代码
        <!-- spring security 安全认证 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!-- oauth2 客户端 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>

2、yml配置

yml 复制代码
# Spring配置
spring:
  security:
    oauth2:
      client:
        registration:
          myclient:
            provider: sas
            client-id: pzj
            client-secret: 123456
            # 重定向的url地址,这个地址为默认的
            redirect-uri: http://localhost:8080/login/oauth2/code/myclient
            authorization-grant-type: "authorization_code"
            scope:
              - openid
              - profile
        provider:
          sas:
            # 以下地址是默认配置在 AuthorizationServerSettings builder方法中
            # 授权服务器地址
            authorization-uri: http://oauth2-server:9000/oauth2/authorize
            # 获取token的地址
            token-uri: http://oauth2-server:9000/oauth2/token
            # 用于验证JWT签名的、oauth2授权服务器的公钥集合
            jwk-set-uri: http://oauth2-server:9000/oauth2/jwks

3、SecurityConfig

java 复制代码
@EnableWebSecurity
@Configuration
public class SecurityConfig {

	@Bean
	protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception
	{
		http.authorizeHttpRequests(auth -> auth.anyRequest().authenticated());
		http.oauth2Login(Customizer.withDefaults());
		return http.build();
	}
}

4、接口

java 复制代码
@RestController
public class HelloController {

	@GetMapping("/hello")
	public String hello(){
		return "<h1>hello,spring authorization server!</h1>";
	}
}

5、测试

浏览器访问:http://localhost:8080/hello

会跳转到

点击浏览器的×,进入登陆页

登陆后,进入授权确认页面

点击submit按钮,就会访问到我们的hello接口

工程名:authorization-project

相关推荐
曲幽4 天前
FastAPI 身份验证总踩坑?这份 FastAPI Users “避坑指南”请收好
python·fastapi·web·jwt·oauth2·user·authentication
海市公约14 天前
OAuth2授权码模式与密码模式的底层流转及安全边界分析
oauth2·认证授权·授权码模式·密码模式·access token·双信道安全
梵得儿SHI16 天前
SpringCloud 进阶拓展:Spring Security OAuth2+JWT 微服务统一认证授权全实战|生产级方案 + 源码解析 + 踩坑实录
spring·spring cloud·微服务·spring security·jwt·oauth2·统一认证授权
曲幽24 天前
你的Agent API还在裸奔?从认证到沙箱,我用FastAPI搭了几道防线
python·fastapi·web·security·jwt·oauth2·limit·sandbox·ai agent
indexsunny2 个月前
互联网大厂Java求职面试实战:Spring Boot微服务在电商场景中的应用与挑战
java·spring boot·redis·面试·kafka·oauth2·microservices
Devin~Y2 个月前
大厂 Java 面试实战:Spring Boot 微服务 + Redis 缓存 + Kafka 消息 + Kubernetes + RAG(小Y水货翻车记)
java·spring boot·redis·kafka·spring security·jwt·oauth2
却话巴山夜雨时i2 个月前
互联网大厂Java面试:从Spring到微服务
spring cloud·微服务·oauth2·java面试·stream api
空太Jun2 个月前
Spring Security 角色权限&资源权限配置 学习笔记
笔记·学习·spring·mybatis·security·springsecurity
九皇叔叔2 个月前
003-SpringSecurity-Demo 统一响应类
java·javascript·spring·springsecurity
九皇叔叔2 个月前
004-SpringSecurity-Demo 拆分环境
java·springboot3·springsecurity