我应该将 CSS 添加到登录后访问的两个页面,并使用 spring security 检查数据
如何为这两个受 spring security 保护的页面添加 CSS?
task.jsp
<link rel="stylesheet" href="<c:url value=" resources/css/bootstrap.responsive.css" />" type="text/css">
<link rel="stylesheet" href="<c:url value=" resources/css/bootstrap.css" />" type="text/css">
<link rel="stylesheet" href="<c:url value=" resources/css/fontello-ie7.css" />" type="text/css">
<link rel="stylesheet" href="<c:url value=" resources/css/fontello.css" />" type="text/css">
<link rel="stylesheet" href="<c:url value=" resources/css/prettyPhoto.css" />" type="text/css">
<link rel="stylesheet" href="<c:url value=" resources/css/style.css" />" type="text/css">
DispatcherServlet.xml
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
spring-security.xml
最佳答案
您没有提到您是否正在使用 Spring Boot,很可能您已经解决了这个问题 - 但我发布它希望它可以帮助其他人:
使用 Spring Boot;
将 Spring Security 添加为依赖项,并且
不能让 CSS 工作(或使用其他静态资源)。
我遇到了类似的问题,这是我发现的:
如您所知,Spring Security 会阻止资源,除了那些应该被授予访问权限的资源(如配置类或 xml 中所述)。据我所知,它在配置文件中大致如下完成:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(“/css/**”,”/fonts/**”,”/libs/**”);
}
但是,在添加以上内容之前,请考虑以下事项:
如果您按照
中的教程实现 Spring Securityhttps://spring.io/guides/gs/securing-web/
您被告知要在配置类中添加以下注释:
@配置
@EnableWebSecurity
但是根据这个来源(也是 Spring 自己的站点):
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-security.html
上面的第二个注释“...将关闭 Spring Boot 中的默认 webapp 安全设置...”。
根据此来源(下面提供的链接),默认情况下,Spring Boot 元素的 webapp 安全设置包含类似于上面提供的代码(允许 css 或类似资源的代码)。
http://www.mkyong.com/spring-boot/spring-boot-spring-security-thymeleaf-example/
(查看源码中SpringSecurityConfig.java的注释)
所以我注释掉了 @EnableWebSecurity 和所有其他 Spring Security 功能,同时 css 被授予访问权限。
希望对您有所帮助。
关于java - Spring Security 中的 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39689430/