【发布时间】:2020-03-04 06:51:53
【问题描述】:
我试图了解 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>
这没问题,但意味着任何其他 SecurityFilterChain 都发生在这个 URL 上,对吧?
【问题讨论】:
标签: java spring spring-boot spring-security spring-oauth2