gpt4 book ai didi

spring-boot - 为什么通过 DSL 配置 HttpSecurity 似乎与显式配置不同?

转载 作者:行者123 更新时间:2023-12-05 07:42:06 24 4
gpt4 key购买 nike

我费尽心思编写了一个 DSL 来为我的自定义身份验证机制配置 HttpSecurity,但是我对其应用的大部分配置在应用程序运行时似乎都没有生效,而当我在 webapp 中手动配置时,一切都完美无缺。

首先,手动配置,导致我的 EntryPoint 触发,authenticationProvider 被查询,过滤器被添加到链中,我的 rememberMeServices 被添加到该过滤器。一切正确。

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/auth/callback").permitAll()
.anyRequest().authenticated()
.and()
.authenticationProvider(authProvider)
.rememberMe()
.rememberMeServices(rememberMeServices)
.and()
.exceptionHandling()
.authenticationEntryPoint(entryPoint)
.and()
.addFilterAfter(filter, UsernamePasswordAuthenticationFilter.class);
/* The following code is basically what gets run when the DSL is in use
http
.apply(new EPIdentityDsl())
// lots of setters called here, removed for clarity
.and()
.authorizeRequests().anyRequest().authenticated();
*/
}

}

但是,DSL 中的代码看起来像这样,并且在使用它时,authenticationEntryPoint 永远不会触发。 rememberMeServices 确实已配置,看起来过滤器已正确添加到链中,但我只是收到 403 响应的错误页面,而不是看到 entryPoint 重定向.

public class EPIdentityDsl extends AbstractHttpConfigurer<EPIdentityDsl, HttpSecurity> {
@Override
public void init(HttpSecurity http) throws Exception {
// any method that adds/removes another configurer
// must be done in the init method
log.debug("dsl init");
http
.exceptionHandling()
.and()
.rememberMe();
}

@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(filterProcessesUrl).permitAll()
.and()
.authenticationProvider(authProvider)
.exceptionHandling()
.authenticationEntryPoint(entryPoint)
.and()
.rememberMe()
.rememberMeServices(rememberMeServices)
.and()
.addFilterAfter(filter, UsernamePasswordAuthenticationFilter.class);

}
}

显然,我在文档或其他内容中遗漏了一些微妙的交互,导致我的 entryPoint 基于 DSL 的配置丢失。知道为什么吗?如果我不得不猜测,那可能是我在指定路径的方式上做错了,但我想不出来。

最佳答案

我遇到了类似的问题。我通过将 entryPoint 移动到 init 解决了这个问题

关于spring-boot - 为什么通过 DSL 配置 HttpSecurity 似乎与显式配置不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44818399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com