Spring Boot 整合 Spring Security + OAuth2

学习在 Spring Boot 中整合 Spring Security 和 OAuth2 。

1 OAuth2 概述

1.1 OAuth2 简介

OAuth 是一个开放标准,该标准允许用户让第三方应用访问该用户在某一网站上存储的私密资源(如头像、照片、视频等),而在这个过程中无需将用户名和密码提供给第三方应用。实现这一功能是通过提供一个令牌(token),而不是用户名和密码来访问他们存放在特定服务提供者的数据。采用令牌(token)的方式可以让用户灵活的对第三方应用授权或者收回权限。

OAuth2 是 OAuth 协议的下一版本,但不向下兼容 OAuth 1.0 。传统的 Web 开发登录认证一般都是基于 session 的,但是在前后端分离的架构中继续使用 session 就会有许多不便,因为移动端(Android、iOS、微信小程序等)要么不支持 cookie (微信小程序),要么使用非常不便,对于这些问题,使用 OAuth2 认证都能解决。

1.2 OAuth2 授权模式

OAuth 协议的授权模式共分为四种:

  1. 授权码模式:常见的第三方平台登录功能基本都是使用这种模式。
  2. 简化模式:简化模式是不需要客户端服务器参与,直接在浏览器中向授权服务器申请令牌(token),一般如果网站是纯静态页面则可以采用这种方式。
  3. 密码模式:密码模式是用户把用户名密码直接告诉客户端,客户端使用说这些信息向授权服务器申请令牌(token)。这需要用户对客户端高度信任,例如客户端应用和服务提供商就是同一家公司。
  4. 客户端模式:客户端模式是指客户端使用自己的名义而不是用户的名义向服务提供者申请授权,严格来说,客户端模式并不能算作 OAuth 协议要解决的问题的一种解决方案,但是,对于开发者而言,在一些前后端分离应用或者为移动端提供的认证授权服务器上使用这种模式还是非常方便的。

这四种模式各有千秋,分别适用于不同的开发场景,开发者要根据实际情况进行选择,本文主要介绍密码模式

2 实战

2.1 创建工程并配置

创建 Spring Boot 项目 spring-boot-springsecurity-oauth2 ,添加 Web/Spring Security/Redis 依赖,如下:

之后手动在 pom 文件中添加 oauth2/commos-pool2 依赖,最终的依赖如下:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</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>
        <version>2.3.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

接着在 application.properties 配置文件中添加 Redis 相关信息的配置和 Redis 连接池的配置,如下:

# 方法1:通过配置文件配置用户/角色
# spring.security.user.name=admin
# spring.security.user.password=123456
# spring.security.user.roles=admin

# Redis 配置
spring.redis.host=192.168.71.62
spring.redis.port=6379
spring.redis.database=0
spring.redis.password=000000

# 连接池配置, Spring Boot 默认用的是 lettuce ,而不是 Jedis ,需增加 commons-pool2 依赖
spring.redis.lettuce.pool.min-idle=5
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=1ms
spring.redis.lettuce.shutdown-timeout=100ms

2.2 配置 OAuth2

  • 资源服务配置
// 资源服务配置
@Configuration
@EnableResourceServer
public class MyResourceServerConfigurer extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        // 资源 id = rid
        resources.resourceId("rid").stateless(true);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin/**").hasRole("admin")
                .antMatchers("/user/**").hasRole("user")
                .anyRequest().authenticated();
    }
}
  • 授权服务配置
// 授权服务配置
@Configuration
@EnableAuthorizationServer
public class MyAuthorizationServerConfigurer extends AuthorizationServerConfigurerAdapter {
    @Autowired
    AuthenticationManager authenticationManager;
    @Autowired
    RedisConnectionFactory redisConnectionFactory;
    @Autowired
    UserDetailsService userDetailsService;

    @Bean
    PasswordEncoder passwordEncoder() {
        // return NoOpPasswordEncoder.getInstance();// 密码不加密
        return new BCryptPasswordEncoder();// 密码加密
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("cid")// 客户端 id
                .authorizedGrantTypes("password", "refresh_token")// 授权方式
                .accessTokenValiditySeconds(1800)
                .resourceIds("rid")// 资源 id,与资源服务配置中的保持一致
                .scopes("all")
                .secret("$2a$10$kwLIAqAupvY87OM.O25.Yu1QKEXV1imAv7jWbDaQRFUFWSnSiDEwG");// 客户端秘钥(123)
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(new RedisTokenStore(redisConnectionFactory))
                .authenticationManager(authenticationManager)
                .userDetailsService(userDetailsService);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients();
    }
}

2.3 配置 Spring Security

新增 SecurityConfig 配置类,如下:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Bean
    @Override
    protected UserDetailsService userDetailsService() {
        return super.userDetailsService();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 在内存中配置2个用户
        /*auth.inMemoryAuthentication()
                .withUser("admin").password("123456").roles("admin")
                .and()
                .withUser("user").password("123456").roles("user");// 密码不加密*/

        auth.inMemoryAuthentication()
                .withUser("admin").password("$2a$10$fB2UU8iJmXsjpdk6T6hGMup8uNcJnOGwo2.QGR.e3qjIsdPYaS4LO").roles("admin")
                .and()
                .withUser("user").password("$2a$10$3TQ2HO/Xz1bVHw5nlfYTBON2TDJsQ0FMDwAS81uh7D.i9ax5DR46q").roles("user");// 密码加密
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 1.先访问 http://127.0.0.1:8080/oauth/token [POST] 获取 token
        // 输入参数:
        /*client_id:cid
        client_secret:123
        grant_type:password
        username:user
        password:123456
        scope:all(这个貌似可以不用传)*/
        // 输出参数:
        /*{
            "access_token": "bd0b8836-3b9b-41da-8b7b-75620e71c8a3",
            "token_type": "bearer",
            "refresh_token": "9ae500a7-7789-45df-bfa3-a9d14d351ab5",
            "expires_in": 1799,
            "scope": "all"
        }*/

        // 2.再根据上面获取的 access_token 访问实际的请求 http://127.0.0.1:8080/user/hello?access_token=bd0b8836-3b9b-41da-8b7b-75620e71c8a3 [GET]

        // 另外可访问 http://127.0.0.1:8080/oauth/token [POST] 刷新 token
        // 输入参数:
        /*client_id:cid
        client_secret:123
        grant_type:refresh_token
        refresh_token:9ae500a7-7789-45df-bfa3-a9d14d351ab5*/
        // 输出参数:
        /*{
            "access_token": "4ba1a3c6-857b-444a-b8a2-e2c7ef06abe3",
            "token_type": "bearer",
            "refresh_token": "9ae500a7-7789-45df-bfa3-a9d14d351ab5",
            "expires_in": 1799,
            "scope": "all"
        }*/

        // 放过 /oauth/token 等请求
        http.antMatcher("/oauth/**")
                .authorizeRequests()
                .antMatchers("/oauth/**").permitAll()
                .and()
                .csrf().disable();
    }
}

2.4 测试

新建 HelloController ,如下:

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("/admin/hello")
    public String admin() {
        return "hello admin";
    }

    @GetMapping("/user/hello")
    public String user() {
        return "hello user";
    }
}

项目启动之后,用 Postman 完成测试。

先访问 http://127.0.0.1:8080/oauth/token [POST] 获取 token 。

再根据上面获取的 access_token 访问实际的请求 http://127.0.0.1:8080/user/hello?access_token=bd0b8836-3b9b-41da-8b7b-75620e71c8a3 [GET]。

另外可访问 http://127.0.0.1:8080/oauth/token [POST] 刷新 token 。



扫码关注微信公众号 程序员35 ,获取最新技术干货,畅聊 #程序员的35,35的程序员# 。独立站点:https://cxy35.com

### Spring Boot 2 中通过集成 Spring SecurityOAuth2 实现单点登录 (SSO) #### 1. 添加必要的依赖项 为了实现基于 Spring Boot 的 SSO 功能,需要引入 `spring-boot-starter-security` 和 `spring-cloud-starter-oauth2` 这两个核心依赖。以下是 Maven 配置中的示例: ```xml <dependencies> <!-- Spring Boot Starter Security --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- Spring Cloud OAuth2 Support --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </dependency> </dependencies> ``` 此部分配置确保项目能够支持 OAuth2 认证机制并提供自动化的安全功能[^1]。 --- #### 2. 创建认证服务器 创建一个独立的服务作为认证服务器,负责管理用户的令牌发放和验证逻辑。在该服务中定义如下配置类: ```java @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("client-id") // 客户端ID .secret("{noop}client-secret") // 客户端密钥 .authorizedGrantTypes("authorization_code", "refresh_token", "password") .scopes("read", "write"); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); } } ``` 上述代码片段展示了如何设置内存中的客户端详情以及授权模式的支持情况[^3]。 --- #### 3. 配置资源服务器 对于需要保护的应用程序(即资源服务器),需指定其受保护的 URL 路径,并声明所使用的认证方式为 OAuth2: ```java @Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/api/public/**").permitAll() // 公开接口无需身份验证 .anyRequest().authenticated(); // 所有其他请求均需经过身份验证 } } ``` 这段代码明确了哪些 API 是公开访问的,而其余则强制执行用户身份验证过程。 --- #### 4. 启用单点登录功能 在实际业务系统的入口处启用 SSO 支持,通常是在主启动类上添加特定注解完成初始化工作: ```java @SpringBootApplication @EnableOAuth2Sso public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 借助于 `@EnableOAuth2Sso` 注解的作用,Spring Boot 将会自动生成一系列默认行为来简化开发者的操作流程[^2]。 --- #### 5. 应用属性文件调整 最后一步是对 application.yml 或者 application.properties 文件做出相应修改,以便适配外部 OAuth 提供商的具体参数设定: ```yaml security: oauth2: client: clientId: your-client-id-here clientSecret: your-client-secret-here accessTokenUri: https://example.com/oauth/token userAuthorizationUri: https://example.com/oauth/authorize resource: userInfoUri: https://example.com/userinfo ``` 以上 YAML 片段指定了与远程 OAuth 授权服务器交互所需的关键字段值集合。 --- ### 总结 综上所述,在 Spring Boot 2 环境下结合 Spring SecurityOAuth2 技术栈可以轻松达成跨多个子域下的统一账户管理体系目标。整个架构设计围绕着集中式的 Token 发放中心展开,从而有效减少了重复性的密码输入动作频率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值