Таблица стилей не добавляется при отображении путей в белом списке - PullRequest
1 голос
/ 27 апреля 2020

Не могу понять, как отобразить мою таблицу стилей для домашней страницы, когда я добавил класс WebApplicationConfig и класс AuthenticationFilter. В моем стиле она находится на ресурсах / static / css. Моя текущая ошибка говорит net :: ERR_TOO_MANY_REDIRECTS ...

Я попытался добавить следующее в свой WebApplicationConfig, но оно тоже не сработало.

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/css/**")
                .addResourceLocations("/static/css/");

    }

Это мой файл структура. Мне нужен дом. css в ресурсах / static / css для отображения на домашней странице. enter image description here

Это мой WebApplicationConfig, который реализует WebMvcConigurer.

@Configuration
public class WebApplicationConfig implements WebMvcConfigurer {

    // Create spring-managed object to allow the app to access our filter
    @Bean
    public AuthenticationFilter authenticationFilter() {
        return new AuthenticationFilter();
    }

    // Register the filter with the Spring container
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationFilter());
    }
}

Это мой класс AuthenticationFilter, который расширяет HandlerInterceptorAdapter.

public class AuthenticationFilter extends HandlerInterceptorAdapter {

    @Autowired
    UserRepository userRepository;

    @Autowired
    AuthenticationController authenticationController;

    private static final List<String> whitelist = Arrays.asList("/login", "/register", "/logout", "/", "/css");

    private static boolean isWhitelisted(String path) {
        for (String pathRoot : whitelist) {
            if (path.equals(pathRoot)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws IOException {

        if (isWhitelisted(request.getRequestURI())) {
            // returning true indicates that the request may proceed
            return true;
        }

        HttpSession session = request.getSession();
        User user = authenticationController.getUserFromSession(session);

        // The user is logged in
        if (user != null) {
            return true;
        }

        // The user is NOT logged in
        response.sendRedirect("");
        return false;
    }

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