стиль CSS весной не работает безопасность, я все сделал - PullRequest
0 голосов
/ 28 февраля 2019

Я ничего не могу поделать со статическими ресурсами в моем веб-приложении после добавления модуля безопасности Spring.Я использую Thymeleaf 3.0.11, Spring web 5.1.5 и Spring-security-config 5.1.4

. Я стараюсь сделать этот проект настолько простым, насколько могу, но когда я добавляю конфигурацию безопасности в стили моего проекта, это не так.т работает.Таблица стилей находится в файле resurces / static / css / style.css Домашняя страница выглядит следующим образом:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link type="text/css" rel="stylesheet" href=../static/css/styles.css th:src="@{css/styles.css}" />
    <link rel="icon" href=../static/img/favicon.ico th:src="@{/img/site-logo.png}">
</head>
<body>
    <header>
        <h1>Body header</h1>
        <a href="index.html" th:href="@{/}"><img /></a>
        <img src=../static/img/favicon.ico th:src="@{/img/favicon.ico}" alt="favicon"/>
        <img src=../static/img/site-logo.png th:src="@{/img/site-logo.png}" alt="Site logo"/>

        <a href=../static/img/favicon.ico/></a>
    </header>
    <p> Some text</p>

</body>
</html>

Конфигурация безопасности Spring:

    import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;


@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/"
                        ,"/users/registration"
                        ,"/static/css/**"
                        ,"../static/css/**"
                        ,"resources/static/css/**"
                        ,"/resources/static/css/**"
                ).permitAll()                .anyRequest().authenticated()
                .and()
                    .authorizeRequests()
                    .antMatchers("/","/registration").permitAll()
                    .anyRequest().authenticated()
                .and()
                    .formLogin()
                    .loginPage("/login")
                    .permitAll()
                    .defaultSuccessUrl("/main",true)
                .and()
                    .logout()
                    .permitAll();

    }


    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        UserDetails user =
                User.withDefaultPasswordEncoder()
                        .username("usr")
                        .password("pas")
                        .roles("USER")
                        .build();

        return new InMemoryUserDetailsManager(user);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**").anyRequest();
    }
}

И файл конфигурации Mvc:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry){
        registry.addViewController("/").setViewName("index");
//        registry.addViewController("/login").setViewName("login");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");

    }
}

Я пробовал что-нибудь, отключить безопасность, ностиль не активен.Может ли кто-нибудь помочь мне с этим?

После некоторых изменений я исправил ссылки на изображения в теле веб-страницы, используя пространство имен thymeleaf, например th: src = "@ {}", но ссылки в теге по-прежнему не помогают.работа.

...