Spring Securing Web添加css,js和图像

时间:2014-02-26 18:39:22

标签: java javascript css spring

我想将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.

是否有人可以指出添加所需的更改以使其正常工作?

1 个答案:

答案 0 :(得分: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>

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