我正在使用Spring 4,Spring安全模块和tomcat 8创建一个Web应用程序。我正在尝试在.jsp文件中包含一些css文件和js文件,但它无法正常工作。当我在Chrome中检查sources标记时,css的内容似乎是一种登录形式。我怀疑它可能与弹簧安全有关。
我的css文件包含在.jsp
中<link href="<c:url value='resources/css/materialize.min.css' />" rel="stylesheet"
type="text/css"></link>
这是WebConfig文件
@Configuration
@ComponentScan(basePackages = "mypackage")
@EnableWebMvc
@EnableTransactionManagement
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Autowired
@Bean
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
return transactionManager;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
这是SecurityConfig文件
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/js/**", "/resources/css/**", "/resources/img/**", "/resources/font/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().loginPage("/signin")
.failureUrl("/signin?param.error=bad_credentials")
.and().logout().logoutUrl("/signout")
.and().authorizeRequests()
.antMatchers("/favicon.ico", "/resources/css/**", "/resources/font/**",
"/resources/js/**", "/auth/**", "/signin/**", "/signup/**", "/disconnect/facebook").permitAll()
.antMatchers("/**").authenticated()
.and()
.rememberMe().
and().csrf();
}
}
根据stackoverflow中的其他答案,它应该与我拥有的代码一起工作,但css只返回这个:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:social="http://spring.io/springsocial"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layout">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div id="content" >
<form id="signin" action="signup" method="post">
<input type="hidden" name="" value=""/>
<div class="formInfo">
</div>
<fieldset>
<label for="login">Email</label>
<input id="login" name="email" type="text" size="25"></input>
<label for="Nombre">Email</label>
<input id="nombre" name="nombre" type="text" size="25"></input>
<label for="password">Password</label>
<input id="password" name="contrasena" type="password" size="25"></input>
</fieldset>
<button type="submit">Sign In</button>
<p>Or you can <a href="signin">signin</a> with a new account.</p>
</form>
</div>
我的所有css和js文件都在WebContent / resources
中答案 0 :(得分:0)
我解决了这个问题,显然我的一个控制器中存在一个模糊的路由,所以当我尝试访问一个以&#34; / resources&#34;开头的网址时它将它路由到控制器,因此返回.jsp而不是css / js / image。我的原始控制器在@Controller中绑定了url,并在不指示路由的情况下离开了@RequestMapping。
@Controller("/signup")
public class SignUpController {
@RequestMapping(method=RequestMethod.GET)
public String signUpForm(){
...
}
@RequestMapping(method=RequestMethod.POST)
public String crearUsuario(HttpServletRequest request){
...
}
所以我更改了@Controller注释,并将url放在每个@RequestMapping中,如下所示:
@Controller
public class SignUpController {
@RequestMapping(value="/signup", method=RequestMethod.GET)
public String signUpForm(){}
@RequestMapping(value="/signup",method=RequestMethod.POST)
public String crearUsuario(HttpServletRequest request){}
}