springboot oauth2 security

Spring Boot 集成 OAuth 2.0 Security 主要涉及使用 Spring Security OAuth2 客户端或资源服务器功能来保护你的应用程序。从 Spring Security 5.x 开始,OAuth 2.0 客户端和授权服务器支持被拆分到了单独的模块中,即 spring-security-oauth2-clientspring-security-oauth2-jose(用于处理 JSON Web Tokens, JWTs 等)。

1. 添加依赖

首先,你需要在你的 Spring Boot 项目的 pom.xml 文件中添加相关的依赖。对于 OAuth 2.0 客户端,你需要:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2. 配置 application.yml 或 application.properties

application.ymlapplication.properties 文件中,你需要配置 OAuth 2.0 客户端的详细信息,比如客户端 ID、客户端密钥、授权服务器 URL 等。

application.yml 示例

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            clientId: your-google-client-id
            clientSecret: your-google-client-secret
            scope: openid,profile,email
            redirectUri: "{baseUrl}/oauth2/callback/{registrationId}"
          facebook:
            clientId: your-facebook-client-id
            clientSecret: your-facebook-client-secret
            scope: public_profile,email
            redirectUri: "{baseUrl}/oauth2/callback/{registrationId}"
        provider:
          google:
            authorizationUri: https://accounts.google.com/o/oauth2/v2/auth
            tokenUri: https://www.googleapis.com/oauth2/v4/token
            userInfoUri: https://openidconnect.googleapis.com/v1/userinfo
            userNameAttribute: sub
          facebook:
            authorizationUri: https://www.facebook.com/v3.2/dialog/oauth
            tokenUri: https://graph.facebook.com/v3.2/oauth/access_token
            userInfoUri: https://graph.facebook.com/me?fields=id,name,email
            userNameAttribute: id

3. 配置 Security 配置类

虽然 Spring Boot 会自动配置很多 OAuth 2.0 客户端的设置,但在某些情况下,你可能需要自定义安全配置。

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/error", "/webjars/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .oauth2Login();
    }
}

4. 使用 OAuth 2.0 认证

现在,当用户尝试访问受保护的资源时,他们将被重定向到配置的 OAuth 2.0 提供者(如 Google 或 Facebook)进行登录。登录成功后,用户将被重定向回你的应用程序,并携带一个授权令牌。Spring Security 将使用这个令牌来验证用户并设置安全上下文。

5. 访问用户信息

你可以通过 OAuth2AuthenticationTokenPrincipal 来访问用户的详细信息,例如用户名、电子邮件等。

@GetMapping("/user")
public Principal user(Principal principal) {
    return principal;
}

或者使用 @AuthenticationPrincipal 注入 OAuth2User

@GetMapping("/user-info")
public Map<String, Object> userInfo(@AuthenticationPrincipal OAuth2User principal) {
    return principal.getAttributes();
}

这就是 Spring Boot 集成 OAuth 2.0 Security 的基本步骤。根据你的具体需求,你可能需要进行一些额外的配置或自定义。

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值