Spring Security HttpSecurity配置

我尝试了解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","/main","/logout-success","/single-logout","/password_forgotten","/enter_new_password","/img/**","/logout","/access_denied")
            .and().authorizeRequests()
                .antMatchers("/img/**","/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,对吗?

feirenchaodi 回答:Spring Security HttpSecurity配置

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

authorizeRequests()为URL配置授权内容,例如是否需要进行身份验证或仅某些角色可以访问它等。它仅对由该SecurityFilterChain处理的那些URL有效(即那些与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/**不需要任何授权。其他URL需要进行身份验证。

,

您的第一个配置
.requestMtchers()// 1

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

让我解释一下您的 .authorizeRequests() // 4

http.authorizeRequests()是通配符(/**)(就像过滤器允许每个请求一样)
HttpSecurity配置将仅考虑具有这些模式的请求 在这里你可以说

http.authorizeRequests()
//is nothing but equals to
http.antMatcher("/**").authorizeRequests();
//and also equals to
http.requestMatchers()
            .antMatchers("/**")
            .and()
            .authorizeRequests()

如果您进行如下配置

http.antMatcher("/api/**").authorizeRequests();

仅当传入请求uri与配置的antmatcher(.hasRole()匹配时,才会咨询其余配置(.hasAnyRole.authenticated().authenticated()/api/**) )

考虑到您需要配置多个URL(不同模式),那么就不能仅使用一个antMatcher。您需要多个,然后应使用如下所示的requestMatcher

http.requestMatchers()
            .antMatchers("/api/**","employee/**","/customer/**")
            .and()
            .authorizeRequests()

.antMatchers()// 2
用于配置RequestMatcherConfigurer的{​​{1}}返回类型
还是
用于配置.requestMatchers()的{​​{1}}返回类型

.and()// 3

  

返回HttpSecurity进行进一步的自定义

.anyRequest()// 5

  

在创建RequestMatcher之后链接的对象

所有请求均与配置的请求匹配器模式匹配

.authenticated()// 6
下面的配置是自我解释

ExpressionInterceptUrlRegistry

这是关于antMatchers,authorizeRequests和Authenticated的粗略概念。您可以在此链接for sequence of execution in spring security

中引用我的答案
本文链接:https://www.f2er.com/3143160.html

大家都在问