我尝试了解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,对吗?