我试图了解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;根据我的理解,这意味着/login和/oauth/authorize的请求是映射的,并且应该是授权请求。所有其他请求都需要身份验证。
意味着端点/user/me必须通过身份验证,因为它由第5点和第6点决定?对这个端点的调用正在为我工作。
在我的ohter配置中,我尝试了一种不同的方法:
@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>这是好的,但意味着任何SecurityFilterChain发生在这个网址上,对吗?
发布于 2019-11-07 21:01:33
如果某个requestMatchers()将由该SecurityFilterChain处理,则SecurityFilterChain将进行配置。因此,如果一个URL不匹配,那么整个SecurityFilterChain将被跳过,这意味着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进行身份验证与Security无关。
http
.authorizeRequests() //1
.antMatchers("/login", "/oauth/authorize", "/img/**").permitAll() //2
.anyRequest() //3
.authenticated() //4这意味着所有的URL都将由这个SecurityFilterChain (requestMatchers()的默认值)处理。/login、/oauth/authorize和/img/**不需要任何授权。其他URL必须经过身份验证。
发布于 2019-11-07 19:40:11
你的第一个配置
.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();配置的其余部分(.hasRole()、.hasAnyRole、.authenticated()或.authenticated())只有在传入请求uri与配置的蚁匹配器(/api/**)匹配时才会被查询。
考虑到您需要配置多个URL(不同的模式),那么您就不能只使用一个antMatcher。您需要多个,那么您应该使用requestMatcher,如下所示
http.requestMatchers()
.antMatchers("/api/**", "employee/**", "/customer/**")
.and()
.authorizeRequests().antMatchers() //2
用于配置RequestMatcherConfigurer返回类型的.requestMatchers()
或者也是
用于配置ExpressionInterceptUrlRegistry返回类型的.authorizeRequests()
.and() //3
返回用于进一步自定义的HttpSecurity
.anyRequest() //5
创建RequestMatcher后被链接的对象
所有请求都与已配置的请求匹配模式匹配。
.authenticated() //6
下面的配置是自我解释
.antMatchers("/app/admin/**").hasRole("ADMIN")
//or
.antMatchers("/app/admin/**").hasAnyRole("ADMIN", "USER")
//or
.antMatchers("/app/admin/**").authenticated()
//or
.antMatchers("/app/admin/**").permitAll()这是关于antMatchers、authorizeRequests和身份验证的粗略想法。您可以在这个链接for sequence of execution in spring security中参考我的答案。
https://stackoverflow.com/questions/58753001
复制相似问题