spring安全中的CSS


问题内容

我应该将CSS添加到两个页面上,在使用Spring Security检查数据后登录后可以访问该页面

如何将CSS添加到受Spring安全保护的这两页中?

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 Security

https://spring.io/guides/gs/securing-
web/

您被告知在config类中添加以下注释:

@组态

@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,并且在css被授予访问权限时,所有其他Spring Security功能均按预期工作。

希望能帮助到你。