Я буду sh, чтобы мое весеннее веб-приложение просто возвращало код состояния 403 UNAUTHORIZED
вместо перенаправления на /login
, когда пользователь пытается получить доступ к ресурсу без входа в систему или с правильными полномочиями.
В настоящее время у меня настроена весенняя защита с помощью oauth2
входа в систему клиента, но я полагаю, что он работает так же, как и при входе в систему basi c.
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.csrf().disable()
.authorizeRequests()
.antMatchers("/allowed").permitAll()
.anyRequest()
.authenticated()
.and()
.oauth2Login(this::configureOauthLogin) // sets up custom user services
.exceptionHandling()
.accessDeniedHandler((request, response, accessDeniedException) -> {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
});
}
Я пытался решить проблему с предоставлением accessDeniedHandler
, но не вижу, чтобы это что-то изменило, так как этот обработчик никогда не запускается.
TLDR
Сделать пружинный бросок 403
вместо 302
для несанкционированных запросов.
EDIT
Вот дамп моей цепочки фильтров
filters = {ArrayList@6668} size = 14
0 = {WebAsyncManagerIntegrationFilter@6635}
1 = {SecurityContextPersistenceFilter@6634}
2 = {HeaderWriterFilter@6633}
3 = {LogoutFilter@6632}
4 = {OAuth2AuthorizationRequestRedirectFilter@6631}
5 = {OAuth2LoginAuthenticationFilter@6630}
6 = {DefaultLoginPageGeneratingFilter@6629}
7 = {DefaultLogoutPageGeneratingFilter@6628}
8 = {RequestCacheAwareFilter@6627}
9 = {SecurityContextHolderAwareRequestFilter@6626}
10 = {AnonymousAuthenticationFilter@6625}
11 = {SessionManagementFilter@6624}
12 = {ExceptionTranslationFilter@6623}
13 = {FilterSecurityInterceptor@6622}