【问题标题】:Spring Securing Web add css, js and imagesSpring Securing Web 添加 css、js 和图像
【发布时间】:2014-03-29 19:20:21
【问题描述】:

我想将 css、javascript 和图像添加到 spring 4 web 应用程序中,我可以举一个例子,但我正在尝试一个 spring 教程:

保护网络 - http://spring.io/guides/gs/securing-web/

我添加了以下更改:

package hello;

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

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

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

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

然后我在资源文件夹中添加了一个带有简单 css 的文件夹 css:

@CHARSET "ISO-8859-1";

p {
    border-style:solid;
    border-width:5px;
}

然后我将 css 添加到 home.html:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Security Example</title>
        <link type="text/css" rel="stylesheet" href="/resources/css/dummy.css"></link>
    </head>
    <body>
        <h1>Welcome!</h1>

        <p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
    </body>
</html>

安全配置通过以下java类注入:

    package hello;

    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;

    @Configuration
    @EnableWebMvcSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .antMatchers("/", "/home").permitAll()
                    .anyRequest().authenticated();
            http
                .formLogin()
                    .loginPage("/login")
                    .permitAll()
                    .and()
                .logout()
                    .permitAll();
        }

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth
                .inMemoryAuthentication()
                    .withUser("user").password("password").roles("USER");
        }
    }

然后在使用谷歌调试时返回代码

Status Code:302 Found but the css is not applied.

谁能指出需要添加的更改才能使其正常工作?

【问题讨论】:

  • 我认为您在 css 中的 href 中缺少项目的根目录。您也可以发布安全配置,也许您保护资源并且客户端无法访问它。
  • I Evgeni 我已经添加了安全配置,但即使我完全删除安全并尝试访问浏览器,也会返回 HTML 代码 404 Not Found。
  • 尝试将&lt;link type="text/css" rel="stylesheet" href="/resources/css/dummy.css"&gt;&lt;/link&gt;改为&lt;link type="text/css" rel="stylesheet" href="@{/resources/css/dummy.css}"&gt;&lt;/link&gt;
  • 我试过了,但没有用,我什至删除了安全性,但没有。

标签: java javascript css spring


【解决方案1】:

经过一番搜索发现问题,需要修改的例子中的css可以使用: 在 src/main 下创建以下目录:

- webapp
- resources
- css

然后添加 dummy.css 并在所需的 html 中使用:

<link th:href="@{/resources/css/dummy.css}" type="text/css" rel="stylesheet" href="/resources/css/dummy.css"></link>

感谢所有试图提供帮助的人:)

【讨论】:

  • 在我的情况下,我必须在我的 Spring Security JavaConfig 类上添加 .antMatchers("/resources/**").permitAll() 。你的帖子帮助我思考。
  • 令人沮丧的问题并浪费大量时间:如果您遇到同样的问题,请尝试,这个已修复的问题:1-从扩展 WebMVCConfigurerAdapter 的类中删除 @EnableWebMVC(主要解决问题)2-在 webSecurityAdapter 扩展类中在 onConfigure 方法中添加 http.authorizeRequests().antMatchers("/css/**", "/js/**", "/images/**").permitAll();当使用带有注释的 Spring Boot 安全性时,您必须将静态资源放在 src/main/resources/ /css/apple.css 的视图中:" rel="stylesheet" type="text/css">
猜你喜欢
  • 2015-10-15
  • 2018-10-14
  • 1970-01-01
  • 2017-03-08
  • 2012-10-04
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 2015-01-22
相关资源
最近更新 更多