【发布时间】: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。
-
尝试将
<link type="text/css" rel="stylesheet" href="/resources/css/dummy.css"></link>改为<link type="text/css" rel="stylesheet" href="@{/resources/css/dummy.css}"></link> -
我试过了,但没有用,我什至删除了安全性,但没有。
标签: java javascript css spring