Весенняя охрана.Не удается разрешить запрос в пользовательском установщике - PullRequest
0 голосов
/ 21 сентября 2018

Мне нужно реализовать авторизацию с определенным заголовком (скажем, "sessionId") и защитить все URI, кроме одного.

Я расширил OncePerRequestFilter и реализовал пользовательский AuthenticationProvider, чтобы проверить, является ли sessionId действительным (кака также пользовательский класс токенов и т. д.).

Как это работает сейчас: для любого uri он сразу же переходит к методу AuthSessionAuthenticationProvider authenticate сразу после применения AuthSessionFilter и возвращает403, если заголовок sessionId не указан.Но я хочу, чтобы некоторые URI разрешили доступ без этого заголовка.

Все вместе:

config:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers(permittedUris).permitAll()
                .anyRequest().authenticated()
                .and().exceptionHandling().accessDeniedHandler(new AuthSessionAccessDeniedHandler())
                .and().addFilterBefore(new AuthSessionFilter(), BasicAuthenticationFilter.class);
    }

Фильтр:

public class AuthSessionFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        Authentication auth = new AuthSessionToken(request.getHeader("sessionId"));
        SecurityContextHolder.getContext().setAuthentication(auth);

        filterChain.doFilter(request, response);
    }
}

Провайдер:

public class AuthSessionAuthenticationProvider implements AuthenticationProvider {

    //...

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        AuthSessionToken token = (AuthSessionToken) authentication;

        if (token.getSessionId() == null) {
            throw new AccessDeniedException("Missing header sessionId");
        }

        AuthSessionAuthorities user = authSessionService.getUserAuthoritiesToken(token.getSessionId());

        if (user == null) {
            throw new AccessDeniedException("Session ID invalid: " + token.getSessionId());
        }

        token.setAuthenticatedUser(user);
        return token;
    }

    //...

}

1 Ответ

0 голосов
/ 02 октября 2018

Я нашел более элегантное решение, которое было разработано именно для этой цели.Это RequestHeaderAuthenticationFilter.И тогда antMatchers работает как положено.Начальная конфигурация выглядит так:

    @Bean
    @SneakyThrows
    public RequestHeaderAuthenticationFilter preAuthenticationFilter() {
        RequestHeaderAuthenticationFilter preAuthenticationFilter = new RequestHeaderAuthenticationFilter();
        preAuthenticationFilter.setPrincipalRequestHeader(SESSION_ID);
        preAuthenticationFilter.setCredentialsRequestHeader(SESSION_ID);
        preAuthenticationFilter.setExceptionIfHeaderMissing(false);
        preAuthenticationFilter.setContinueFilterChainOnUnsuccessfulAuthentication(true);
        preAuthenticationFilter.setAuthenticationManager(authenticationManager());

        return preAuthenticationFilter;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...