Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring安全性:静态资源don';t载荷_Spring_Spring Security_Static_Thymeleaf - Fatal编程技术网

Spring安全性:静态资源don';t载荷

Spring安全性:静态资源don';t载荷,spring,spring-security,static,thymeleaf,Spring,Spring Security,Static,Thymeleaf,我正在使用SpringMVC和Thymleaf以及Spring安全性。 我想使用Thymleaf模板加载一个页面,我可以加载我的静态资源 例如,我想从template.html加载位于:static/img/theme/logo.png中的图片 以下是我所拥有的: template.html: body> div layout:fragment="content"> a href="">img src="../static/img/th

我正在使用SpringMVC和Thymleaf以及Spring安全性。 我想使用Thymleaf模板加载一个页面,我可以加载我的静态资源

例如,我想从template.html加载位于:static/img/theme/logo.png中的图片

以下是我所拥有的:


template.html:

body> div layout:fragment="content"> a href="">img src="../static/img/theme/logo.png" alt="Logo"> h1>Hello /div> /body> 正文> div布局:fragment=“content”> a href=“”>img src=“../static/img/theme/logo.png”alt=“logo”> h1>你好 /div> /正文>
MvcConfig.java

@Configuration public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/home").setViewName("home"); registry.addViewController("/index").setViewName("index"); registry.addViewController("/template").setViewName("template"); registry.addViewController("/layout").setViewName("layout"); registry.addViewController("/login").setViewName("login"); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } } @配置 公共类MvcConfig扩展了WebMVCConfigureAdapter{ @凌驾 public void addViewController(ViewControllerRegistry注册表){ registry.addViewController(“/home”).setViewName(“home”); registry.addViewController(“/index”).setViewName(“index”); registry.addViewController(“/template”).setViewName(“template”); registry.addViewController(“/layout”).setViewName(“layout”); registry.addViewController(“/login”).setViewName(“login”); } @凌驾 公共无效配置DefaultServletHandling(DefaultServletHandlerConfigurer-configurer){ configurer.enable(); } }
Web安全配置:

@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { //List of all free pages private static final String[] pagesFree = { "/home", "/template", "/layout", //Thymleaf directory "/css/**", "/js/**", "/img/**", "/fonts/**", "/ico/**", "/twitter/**", "/" }; @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers(pagesFree).permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("u").password("u").roles("USER"); } } @配置 @启用Web安全性 公共类WebSecurityConfig扩展了WebSecurityConfigureAdapter{ //所有免费页面的列表 私有静态最终字符串[]页面自由={ “/主页”, “/template”, “/布局”, //胸腺叶目录 “/css/**”, “/js/**”, “/img/**”, “/fonts/**”, “/ico/**”, “/twitter/**”, "/" }; @凌驾 受保护的无效配置(HttpSecurity http)引发异常{ http .授权请求() .antMatchers(pagesFree).permitAll() .anyRequest().authenticated() .及() .formLogin() .login页面(“/login”) .permitAll() .及() .logout() .permitAll(); } @自动连线 public void configureGlobal(AuthenticationManagerBuilder auth)引发异常{ auth.inMemoryAuthentication() .带用户(“u”)。密码(“u”)。角色(“用户”); } }

在安全配置中,您可以声明如下内容:

/** Public URLs. */
private static final String[] PUBLIC_MATCHERS = {
        "/webjars/**",
        "/css/**",
        "/js/**",
        "/images/**",
        "/"
};
@Override
protected void configure(HttpSecurity http) throws Exception {

    List<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
    if (activeProfiles.contains("dev")) {
        http.csrf().disable();
        http.headers().frameOptions().disable();
    }

    http
            .authorizeRequests()
            .antMatchers(PUBLIC_MATCHERS).permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").defaultSuccessUrl("/payload")
            .failureUrl("/login?error").permitAll()
            .and()
            .logout().permitAll();
}
<img class="featurette-image pull-left" th:src="@{/images/browser-icon-firefox.png}" />
然后像这样:

/** Public URLs. */
private static final String[] PUBLIC_MATCHERS = {
        "/webjars/**",
        "/css/**",
        "/js/**",
        "/images/**",
        "/"
};
@Override
protected void configure(HttpSecurity http) throws Exception {

    List<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
    if (activeProfiles.contains("dev")) {
        http.csrf().disable();
        http.headers().frameOptions().disable();
    }

    http
            .authorizeRequests()
            .antMatchers(PUBLIC_MATCHERS).permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").defaultSuccessUrl("/payload")
            .failureUrl("/login?error").permitAll()
            .and()
            .logout().permitAll();
}
<img class="featurette-image pull-left" th:src="@{/images/browser-icon-firefox.png}" />
@覆盖
受保护的无效配置(HttpSecurity http)引发异常{
List activeProfiles=Arrays.asList(env.getActiveProfiles());
if(activeProfiles.contains(“dev”)){
http.csrf().disable();
http.headers().frameOptions().disable();
}
http
.授权请求()
.antMatchers(PUBLIC_MATCHERS).permitAll()
.anyRequest().authenticated()
.及()
.formLogin().loginPage(“/login”).defaultSuccessUrl(“/payload”)
.failureUrl(“/login?error”).permitAll()
.及()
.logout().permitAll();
}
在您的Thymeleaf模板中,您可以声明如下内容:

/** Public URLs. */
private static final String[] PUBLIC_MATCHERS = {
        "/webjars/**",
        "/css/**",
        "/js/**",
        "/images/**",
        "/"
};
@Override
protected void configure(HttpSecurity http) throws Exception {

    List<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
    if (activeProfiles.contains("dev")) {
        http.csrf().disable();
        http.headers().frameOptions().disable();
    }

    http
            .authorizeRequests()
            .antMatchers(PUBLIC_MATCHERS).permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").defaultSuccessUrl("/payload")
            .failureUrl("/login?error").permitAll()
            .and()
            .logout().permitAll();
}
<img class="featurette-image pull-left" th:src="@{/images/browser-icon-firefox.png}" />


可以找到项目的工作副本。

在安全配置中,您可以声明如下内容:

/** Public URLs. */
private static final String[] PUBLIC_MATCHERS = {
        "/webjars/**",
        "/css/**",
        "/js/**",
        "/images/**",
        "/"
};
@Override
protected void configure(HttpSecurity http) throws Exception {

    List<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
    if (activeProfiles.contains("dev")) {
        http.csrf().disable();
        http.headers().frameOptions().disable();
    }

    http
            .authorizeRequests()
            .antMatchers(PUBLIC_MATCHERS).permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").defaultSuccessUrl("/payload")
            .failureUrl("/login?error").permitAll()
            .and()
            .logout().permitAll();
}
<img class="featurette-image pull-left" th:src="@{/images/browser-icon-firefox.png}" />
然后像这样:

/** Public URLs. */
private static final String[] PUBLIC_MATCHERS = {
        "/webjars/**",
        "/css/**",
        "/js/**",
        "/images/**",
        "/"
};
@Override
protected void configure(HttpSecurity http) throws Exception {

    List<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
    if (activeProfiles.contains("dev")) {
        http.csrf().disable();
        http.headers().frameOptions().disable();
    }

    http
            .authorizeRequests()
            .antMatchers(PUBLIC_MATCHERS).permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").defaultSuccessUrl("/payload")
            .failureUrl("/login?error").permitAll()
            .and()
            .logout().permitAll();
}
<img class="featurette-image pull-left" th:src="@{/images/browser-icon-firefox.png}" />
@覆盖
受保护的无效配置(HttpSecurity http)引发异常{
List activeProfiles=Arrays.asList(env.getActiveProfiles());
if(activeProfiles.contains(“dev”)){
http.csrf().disable();
http.headers().frameOptions().disable();
}
http
.授权请求()
.antMatchers(PUBLIC_MATCHERS).permitAll()
.anyRequest().authenticated()
.及()
.formLogin().loginPage(“/login”).defaultSuccessUrl(“/payload”)
.failureUrl(“/login?error”).permitAll()
.及()
.logout().permitAll();
}
在您的Thymeleaf模板中,您可以声明如下内容:

/** Public URLs. */
private static final String[] PUBLIC_MATCHERS = {
        "/webjars/**",
        "/css/**",
        "/js/**",
        "/images/**",
        "/"
};
@Override
protected void configure(HttpSecurity http) throws Exception {

    List<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
    if (activeProfiles.contains("dev")) {
        http.csrf().disable();
        http.headers().frameOptions().disable();
    }

    http
            .authorizeRequests()
            .antMatchers(PUBLIC_MATCHERS).permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").defaultSuccessUrl("/payload")
            .failureUrl("/login?error").permitAll()
            .and()
            .logout().permitAll();
}
<img class="featurette-image pull-left" th:src="@{/images/browser-icon-firefox.png}" />


可以找到项目的工作副本。

我有相同的安全配置,只是暂时不管理我的配置文件。当我把这行代码放到图片上时:

我仍然没有看到图片。这是项目:[link]()嗨,我做了一些更改。你可以在这里从GitHub下载:模板页面返回了徽标nowHi我试过了,一切都很好!非常感谢:)我的荣幸。顺便说一句,我正在完成最后几堂课的在线课程,内容是如何使用Thymeleaf、Bootstrap、Spring Boot、Email、Data JPA、Security和Amazon Web服务从头开始创建Spring Boot网站。如果你愿意,你可以在这里注册你的兴趣:我有相同的安全配置,只是我暂时不管理我的个人资料。当我把这行代码放到图片上时:

我仍然没有看到图片。这是项目:[link]()嗨,我做了一些更改。你可以在这里从GitHub下载:模板页面返回了徽标nowHi我试过了,一切都很好!非常感谢:)我的荣幸。顺便说一句,我正在完成最后几堂课的在线课程,内容是如何使用Thymeleaf、Bootstrap、Spring Boot、Email、Data JPA、Security和Amazon Web服务从头开始创建Spring Boot网站。如果需要,您可以在此注册您的兴趣: