正在try 配置JWT配置.看来JWT已经过时了.我现在怎么用OAuth2ResourceServerConfigurer::jwt

我的代码是:

@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http.authorizeHttpRequests((requests) -> requests.anyRequest().authenticated());
    http.sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
    //http.formLogin(withDefaults());
    http.httpBasic(Customizer.withDefaults());
    http.csrf(csrf -> csrf.disable());
    http.headers(headers -> headers.frameOptions(frameOptionsConfig -> frameOptionsConfig.sameOrigin()));
    http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
    return http.build();
}

此外,在Spring Security6.0中,antMatchers()以及其他用于保护请求的配置方法(即mvcMatchers()regexMatchers())已经从API中删除.

推荐答案

除了@Schrom Answer和更多与OAuth2ResourceServerConfigurer#jwt的弃用相关的方法外,Spring Security不推荐使用返回自己的配置器的方法,而支持返回HttpSecurity的方法,并且不推荐使用HttpSecurity中的.and()方法.

例如,httpBasic()不推荐使用,而支持使用httpBasic(Customizer).这些反对是为了只有一种方法来配置安全DSL,那就是使用lambdas.看一看at the documentation个.

因此,对于JWT配置,您必须执行以下操作:

oauth2ResourceServer((oauth2) -> oauth2
    .jwt(Customizer.withDefaults())
)

Java相关问答推荐

无法从TemporalAccessor获取Instant:{},ISO解析为2024-04- 25 T14:32:42类型为java.time. form.Parsed

如何使用CSS为选定但未聚焦的表格行设置背景 colored颜色 ?

Cucumber TestNG Assert失败,出现java. lang. Numbercycle异常

Java记录的不同序列化/反序列化

';com.itextpdf.ext.html.WebColors已弃用

如何才能使我的程序不会要求两次输入?

为什么在maven中,getLast方法不适用于List?

如何使用带有谓词参数的方法,而不使用lambda表达式

无法在Java中处理PayPal支付响应

使用Jolt将字段转换为列表

有没有可能在时间范围内得到多种解决方案?

删除打印语句会影响功能...腐败在起作用?

Android应用程序为错误的显示类型 Select 尺寸文件

A.ForEach与For(类型a:集合)

当我在Java中有一个Synchronized块来递增int时,必须声明一个变量Volatile吗?

组合连接以从两个表返回数据

如何在Spring Boot中为不同的部署环境管理多个.properties文件?

Java 21内置http客户端固定运营商线程

JOOQ:批处理CRUD操作使用动态表定义,如何?

在输入端没有可行的替代方案'; Select *';