温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用Spring Security与Oauth2.0

发布时间:2021-10-20 15:06:41 来源:亿速云 阅读:182 作者:iii 栏目:编程语言

本篇内容主要讲解“如何使用Spring Security与Oauth2.0”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用Spring Security与Oauth2.0”吧!

问题

最近由于工作变动,我又开始搞 Java 后台了(做回老本行)。目前第一个工作项目是搞一个用户认证中心,于是便一脚踏入了 Spring Security 的坑里面。其实当下比较流行的一套解决方案就是 Spring Security + Oauth3.0 + JWT 方式。可是当我开始集成 Spring Security 和 Oauth3.0 的时候,我眉头一皱突然发现这个事情不简单。

<!--more-->

在创建 Springboot 工程时,可以选择以下的 Oauth3.0 依赖:

spring-boot-starter-oauth3-client
spring-boot-starter-oauth3-resource-server
spring-cloud-starter-oauth3

我心想咋还这么多依赖包呢。本着面向百度编程的原则,从一众“天下文章一大抄”的博客里发现他们还用的是另外两个依赖:

spring-security-oauth3
spring-security-oauth3-autoconfigure

这我就头大了啊,咋还整得这么复杂呢。所以只能去扒官方文档了。

官方文档解释

看到 Spring 官方对于 Oauth3.0 的解释,他说了这么一句

The Spring Security OAuth project is deprecated. The latest OAuth 2.0 support is provided by Spring Security. See the OAuth 2.0 Migration Guide for further details.

也就是说原来的 Spring Security OAuth3.0 已经废弃了,有关 OAuth3.0 的支持已经集成到了 Spring Security 里面了。这我就懵了。。。emmm。也就是说现在如果使用spring-security-oauth3,里面大部分方法都是被横线划掉的(被废弃的)。为啥要这样呢?

后来本着吃瓜的心态,扒了扒 OAuth 和 Spring 社区的历史。发现在 2018 年,Spring 社区就发布了声明,说是要逐渐停止现有的 OAuth3 支持,而在 Spring Security5 中构建下一代 OAuth3.0 支持。原因是Oauth3 落地混乱:Spring Security OAuth、Spring Cloud Security、Spring Boot 1.5.x 以及当时最新的 Spring Security5.x 中都提供了对 OAuth3 的实现。因此官方要统一放在一个地方。

我想,这是个好事啊,省的让大家不知道使用哪一个了。当然官方也是很给力,不仅完成了对 Oauth3.0 的支持,也加入了 OpenID Connect1.0 的支持。但是呢,社区又来了个骚操作:宣布不再支持授权服务器。因为官方认为授权服务器是一种产品形态,并非框架该完成的,其次目前已经有很多商用和开源的授权服务器了(例如 Keycloak、Okta)。但是一众开发者不服啊,在社区里进行激烈讨论。于是官方妥协,发起了新的项目 spring-authorization-server,目前已经迭代到了0.0.3版本。

现状 & 迁移

吃完瓜,来看看这几个包现在是什么状态。

spring-security-oauth3  -> 被废弃,建议不使用,否则后期无法维护

spring-security-oauth3-autoconfigure  -> 自动配置,没有用处
spring-boot-starter-oauth3-client -> 最新
spring-boot-starter-oauth3-resource-server  -> 最新
spring-cloud-starter-oauth3 -> 引用 spring-security-oauth3,但尚未标注被废弃

这样就比较明确了,现在的项目中需要依据该服务的用途去引用对应的包。

授权服务器

如果服务想做成授权服务器,暂时只能引用spring-cloud-starter-oauth3。因为这个包也是引用了spring-security-oauth3,但尚未标注@Deprecated,然后仍旧使用@EnableAuthorityServer注解进行配置。等待spring-authorization-server成熟,需要切换过来。

客户端

现在如果要开发客户端,只需要引用spring-boot-starter-oauth3-client,也只需要在原来的 SpringSecurity 的配置类中,调用.oauth3Client()即可配置,不需要以前的@EnableOAuth3Client注解了。

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.authorizeRequests()
				.anyRequest().authenticated()
				.and()
			.formLogin()
				.loginPage("/login")
				.failureUrl("/login-error")
				.permitAll()
				.and()
			.oauth3Client(); //
	}

}

除此之外,也需要配置WebClientOAuth3AuthorizedClientManager这两个 Bean。具体如何实现,先挖个坑,以后再填。

资源服务器

资源服务器也只需要引用spring-boot-starter-oauth3-resource-server,若使用 JWK 方式,其配置如下(也仅仅是调用.oauth3ResourceServer()

@Configuration
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
    @Value("${spring.security.oauth3.resourceserver.jwt.jwk-set-uri}")
    private String jwkSetUri;

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http
                .oauth3ResourceServer(OAuth3ResourceServerConfigurer::jwt)
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/message/**").hasAuthority("SCOPE_all")
                .anyRequest().authenticated();
    }

    @Bean
    JwtDecoder jwtDecoder() {
        return NimbusJwtDecoder.withJwkSetUri(this.jwkSetUri).build();
    }
}

到此,相信大家对“如何使用Spring Security与Oauth2.0”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI