Как проверить антивещество безопасности весной в пользовательском фильтре - PullRequest
0 голосов
/ 09 января 2019

Я определил пользовательский фильтр, я добавил его сразу после BasicAutenticationFilter ...

Проблема в том, что, когда я пингую свой микросервис (в этом случае проверяется работоспособность ранчера), он всегда идет к фильтру и делает что-то, если этого не следует, я говорю, что это не должно, потому что я ставлю пинг в antmatchers для разрешения всех запросов, код в моем фильтре такой:

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {

    logger.debug(this + "received authentication request from " + request.getRemoteHost() + " to " + request.getLocalName());

    if (request instanceof HttpServletRequest) {
        if (isAuthenticationRequired()) {
            authenticate(request,response);
        } else {
            logger.debug("session already contained valid Authentication - not checking again");
        }
    }
    chain.doFilter(request, response);
}   

private boolean isAuthenticationRequired() {
    Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication();
    if ((existingAuth == null) || !existingAuth.isAuthenticated()) {
        return true;
    }
    return false;
}  

Я предположил, когда вы SecurityContextHolder.getContext (). GetAuthentication (); он должен проверить antmatchers и вернуть существующий True с true, я знаю, я знаю, что это магически, чтобы думать, как я, но затем мне нужно найти прочь, чтобы проверить запрос и сказать, что весенняя безопасность находится внутри списка antmatchers ..

1 Ответ

0 голосов
/ 10 января 2019

Вы можете просто использовать фильтр для проверки работоспособности. поместите его в положение FIRST

public class HealthFilter extends OncePerRequestFilter {

    private String healthPath = "/healthcheck/**";
    private AntPathRequestMatcher matcher = new AntPathRequestMatcher(healthPath);

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

        if (matcher.matches(request)) { //evaluate health check
            //do anything you want over here.
            //including performing your health check
            response.getWriter().write("OK");
            response.setStatus(200);
        }
        else {
            //only execute the other filters if we're not doing a health check
            filterChain.doFilter(request, response);
        }

    }
}

Онлайн доступны интеграционные тесты

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