gpt4 book ai didi

java - Spring Security HttpSecurity 配置

转载 作者:行者123 更新时间:2023-11-30 06:01:07 24 4
gpt4 key购买 nike

我试图了解 RequestMatcher、AntMatcher 等是如何工作的。我阅读了一些帖子并了解了基础知识。实际上我有这个简单的基本配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatchers() //1
.antMatchers("/login", "/oauth/authorize") //2
.and() //3
.authorizeRequests() //4
.anyRequest() //5
.authenticated() //6;

我真的不明白第 1、2 和 3 点。根据我的理解,这意味着 /login/oauth/authorize 的请求被映射,应该是授权请求.所有其他请求都需要身份验证。

端点/user/me 的方法我必须进行身份验证,因为它由第 5 点和第 6 点规定?对此端点的调用对我有用。

在我的其他配置中,我尝试了一种不同的方法:

@Override
protected void configure(HttpSecurity http) throws Exception { // @formatter:off
http
.authorizeRequests() //1
.antMatchers("/login", "/oauth/authorize", "/img/**").permitAll() //2
.anyRequest() //3
.authenticated() //4

从我的角度来看,这应该与第一个配置的逻辑相同。但实际上端点 /user/me 不再可访问。

非常感谢您的澄清


更新 1:

这是我现在的配置:

@Override
protected void configure(HttpSecurity http) throws Exception { // @formatter:off
http
.requestMatchers()
.antMatchers("/", "/login", "/oauth/authorize",
"/main", "/logout-success", "/single-logout",
"/password_forgotten", "/enter_new_password", "/img/**",
"/logout", "/access_denied")
.and().authorizeRequests()
.antMatchers("/img/**", "/logout-success", "/password_forgotten",
"/enter_new_password", "/access_denied").permitAll()
.requestMatchers(SecurityUtils::isFrameworkInternalRequest).permitAll()
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error")
.defaultSuccessUrl("/main")
.permitAll()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/logout-success")
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.and()
.exceptionHandling()
.accessDeniedPage("/access_denied")
.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
.and().csrf().disable();

如果我以未验证用户身份输入 URL \user\me,我会收到 401 消息和此消息:

<oauth>
<error_description>
Vollständige Authentifikation wird benötigt um auf diese Resource zuzugreifen
</error_description>
<error>unauthorized</error>
</oauth>

没关系,但这意味着此 URL 发生了任何其他 SecurityFilterChain,对吗?

最佳答案

requestMatchers() 配置 URL 是否将由该 SecurityFilterChain 处理。因此,如果 URL 不匹配,整个 SecurityFilterChain 将被跳过,这意味着 Spring Security 之后将不会处理该 URL。如果不配置,默认匹配所有的url。

authorizeRequests() 为 URL 配置授权内容,例如它是否需要进行身份验证或者是否只有某些角色可以访问它等。它只对那些 URL 有效由该 SecurityFilterChain 处理(即由 requestMatchers() 匹配的那些 URL)

那么,回到你的第一个例子:

  http.requestMatchers() //1
.antMatchers("/login", "/oauth/authorize") //2
.and() //3
.authorizeRequests() //4
.anyRequest() //5
.authenticated() //6;

这意味着这个SecurityFilterChain只会对/login/oauth/authorize有效。两个 URL 都需要经过身份验证。此 SecurityFilterChain 不会处理所有其他 URL。所以/user/me是否需要认证与Spring Security无关。

http
.authorizeRequests() //1
.antMatchers("/login", "/oauth/authorize", "/img/**").permitAll() //2
.anyRequest() //3
.authenticated() //4

这意味着所有 URL 都将由这个 SecurityFilterChain 处理(requestMatchers() 的默认值)。 /login , /oauth/authorize/img/** 不需要任何授权。其他网址需要认证。

关于java - Spring Security HttpSecurity 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58753001/

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