oauth2_带有Spring Security的OAuth 2.0快速指南

oauth2

oauth2

“我喜欢编写身份验证和授权代码。” 〜从来没有Java开发人员。 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证。

在构建Web应用程序时,必须进行身份验证和授权。 然而,正确地做起来并不容易。 计算机安全是真正的专业。 众多开发人员昼夜不停地与众多国际黑客进行对抗,从而创造了一个持续的开发周期,以发现漏洞,对其进行攻击并加以修复。 跟上所有这些独奏会很痛苦(如果不是不可能的话)。

幸运的是,没有必要。 Spring Security和Spring Boot使使用OAuth 2.0实施Web应用程序变得非常简单。 Okta,即软件即服务的身份访问提供商,已经在Spring Boot的基础上进行了构建,从而使这一过程变得更加容易。

在本教程中,您将首先使用Spring Boot和Spring Security构建OAuth 2.0 Web应用程序和身份验证服务器。 之后,您将使用Okta摆脱自我托管的身份验证服务器,并进一步简化Spring Boot应用程序。

让我们开始吧!

创建一个OAuth 2.0服务器

首先转到Spring Initializr并使用以下设置创建一个新项目:

  • 将项目类型从Maven更改为Gradle
  • 将组更改为com.okta.spring
  • 将工件更改为AuthorizationServerApplication
  • 添加一个依赖项: Web
Spring安全

下载项目并将其复制到硬盘上有意义的位置。 在本教程中,您将创建三个不同的项目,因此您可能需要创建一个父目录,例如SpringBootOAuth

您需要向build.gradle文件添加一个依赖build.gradle

implementation 'org.springframework.security.oauth:spring-security-oauth2:2.3.3.RELEASE'

这增加了Spring的OAuth优势。

更新src/main/resources/application.properties以使其匹配:

server.port=8081
server.servlet.context-path=/auth
user.oauth.clientId=R2dpxQ3vPrtfgF72
user.oauth.clientSecret=fDw7Mpkk5czHNuSRtmhGmAGL42CaxQB9
user.oauth.redirectUris=http://localhost:8082/login/oauth2/code/
user.oauth.user.username=Andrew
user.oauth.user.password=abcd

这将设置服务器端口,servlet上下文路径以及服务器将返回给客户端的内存中临时生成的令牌以及我们用户的用户名和密码的一些默认值。 在生产中,您将需要更多用于真正的身份验证服务器的复杂后端,而没有硬编码的重定向URI,用户名和密码。

更新AuthorizationServerApplication类以添加@EnableResourceServer

package com.okta.spring.AuthorizationServerApplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;

@SpringBootApplication
@EnableResourceServer
public class AuthorizationServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AuthorizationServerApplication.class, args);
    }
}

在与src/main/java下的应用程序类com.okta.spring.AuthorizationServerApplication相同的包中创建一个新的AuthServerConfig类(从现在开始,请在src/main/java/com/okta/spring/AuthorizationServerApplication创建Java类)。 此Spring配置类启用并配置OAuth授权服务器。

package com.okta.spring.AuthorizationServerApplication;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;

@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {

    @Value("${user.oauth.clientId}")
    private String ClientID;
    @Value("${user.oauth.clientSecret}")
    private String ClientSecret;
    @Value("${user.oauth.redirectUris}")
    private String RedirectURLs;

   private final PasswordEncoder passwordEncoder;
    
    public AuthServerConfig(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public void configure(
        AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.tokenKeyAccess("permitAll()")
            .checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值