Pat*_*ick 7 java spring spring-security spring-boot spring-oauth2
我尝试了解 RequestMatcher、AntMatcher 等是如何工作的。我阅读了一些帖子并了解了基础知识。实际上我有这个简单的基本配置:
\n\n@Override\nprotected void configure(HttpSecurity http) throws Exception {\n http.requestMatchers() //1\n .antMatchers("/login", "/oauth/authorize") //2\n .and() //3\n .authorizeRequests() //4\n .anyRequest() //5\n .authenticated() //6;\nRun Code Online (Sandbox Code Playgroud)\n\n我真的不明白第 1,2 和 3 点。根据我的理解,这意味着/login和的请求/oauth/authorize请求被映射并且应该是授权请求。所有其他请求都需要进行身份验证。
对于端点来说,/user/me我必须进行身份验证,因为它由第 5 点和第 6 点规定?\n对此端点的调用对我有用。
在我的其他配置中,我尝试了一种不同的方法:
\n\n@Override\nprotected void configure(HttpSecurity http) throws Exception { // @formatter:off\n http\n .authorizeRequests() //1\n .antMatchers("/login", "/oauth/authorize", "/img/**").permitAll() //2\n .anyRequest() //3\n .authenticated() //4\nRun Code Online (Sandbox Code Playgroud)\n\n从我的角度来看,这应该与第一个配置的逻辑相同。但实际上端点/user/me不再可访问。
我非常感谢您的澄清
\n\n更新1:
\n\n这是我现在的配置:
\n\n@Override\nprotected void configure(HttpSecurity http) throws Exception { // @formatter:off\n http\n .requestMatchers()\n .antMatchers("/", "/login", "/oauth/authorize", \n "/main", "/logout-success", "/single-logout",\n "/password_forgotten", "/enter_new_password", "/img/**",\n "/logout", "/access_denied")\n .and().authorizeRequests()\n .antMatchers("/img/**", "/logout-success", "/password_forgotten",\n "/enter_new_password", "/access_denied").permitAll()\n .requestMatchers(SecurityUtils::isFrameworkInternalRequest).permitAll()\n .and()\n .authorizeRequests()\n .anyRequest()\n .authenticated()\n .and()\n .formLogin()\n .loginPage("/login")\n .failureUrl("/login?error")\n .defaultSuccessUrl("/main")\n .permitAll()\n .and()\n .logout()\n .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))\n .logoutSuccessUrl("/logout-success")\n .deleteCookies("JSESSIONID")\n .invalidateHttpSession(true)\n .and()\n .exceptionHandling()\n .accessDeniedPage("/access_denied")\n .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))\n .and().csrf().disable();\nRun Code Online (Sandbox Code Playgroud)\n\n如果我\\user\\me以未经身份验证的用户身份输入 URL,则会收到 401 和以下消息:
<oauth>\n<error_description>\nVollst\xc3\xa4ndige Authentifikation wird ben\xc3\xb6tigt um auf diese Resource zuzugreifen\n</error_description>\n<error>unauthorized</error>\n</oauth>\nRun Code Online (Sandbox Code Playgroud)\n\n这没问题,但意味着此 URL 会发生任何其他 SecurityFilterChain,对吧?
\nrequestMatchers()配置 URL 是否将被处理SecurityFilterChain。因此,如果 URL 与它不匹配,则整个 URLSecurityFilterChain将被跳过,这意味着 Spring Security 之后将不会处理该 URL。如果不配置,默认匹配所有URL。
配置authorizeRequests()URL 的授权内容,例如是否需要进行身份验证或只有某些角色可以访问它等。它仅对由该 SecurityFilterChain 处理的那些 URL 有效(即与 匹配的那些 URL requestMatchers())
那么,回到第一个例子:
http.requestMatchers() //1
.antMatchers("/login", "/oauth/authorize") //2
.and() //3
.authorizeRequests() //4
.anyRequest() //5
.authenticated() //6;
Run Code Online (Sandbox Code Playgroud)
这意味着这个SecurityFilterChain只会对/login和起作用/oauth/authorize。两个 URL 都需要进行身份验证。所有其他 URL 将不会由该 SecurityFilterChain 处理。所以是否/user/me需要认证与Spring Security无关。
http
.authorizeRequests() //1
.antMatchers("/login", "/oauth/authorize", "/img/**").permitAll() //2
.anyRequest() //3
.authenticated() //4
Run Code Online (Sandbox Code Playgroud)
这意味着所有 URL 都将由该 SecurityFilterChain 处理(默认值为requestMatchers())。/login,/oauth/authorize并且/img/**不需要任何授权。其他 URL 需要进行身份验证。
| 归档时间: |
|
| 查看次数: |
6082 次 |
| 最近记录: |