java - 我无法加载带有 spring security 的登录后可以访问的 CSS 页面。我该如何解决?

标签 java css spring spring-mvc spring-security

我应该将 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

<http auto-config="true">
    <access-denied-handler error-page="/error" />
    <intercept-url pattern="/admin**" access="ROLE_ADMIN" />
    <intercept-url pattern="/utente**" access="ROLE_USER" />
        <form-login login-page="/login" default-target-url="/role" authentication-failure-url="/login"  />
    </http>

    <authentication-manager>
      <authentication-provider>
      <password-encoder hash="bcrypt" />
          <jdbc-user-service data-source-ref="dataSource"
     users-by-username-query="select username,password,TRUE from Student where username = ?" 
     authorities-by-username-query="select username,role from Student where username = ?"  />
     </authentication-provider>

LoginController.java

@RequestMapping(value="/role", method = RequestMethod.GET)
    public String loginRole() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            String role = auth.getAuthorities().toString();
String targetUrl = "";
            if(role.contains("ROLE_USER")) {
                targetUrl = "/users/task";
            } else if(role.contains("ROLE_ADMIN")) {
                targetUrl = "/administration/homeAdmin";
            }
            return targetUrl;
        }

最佳答案

<mvc:resources mapping="/resources/**" location="/resources/" />

根据上一行,您应该确保您的 css 文件位于 location="/resources/"(资源文件夹)下。如果它们存在于其他文件夹中,请更改位置路径。

并且在 spring-security.xml 中添加下面一行,这样资源就不需要安全了。

<http pattern="/resources/**" security="none"/> 

然后在 jsp 中。

<link type="text/css" href="${pageContext.request.contextPath}/resources/css/fontello.css"  rel="stylesheet">

关于java - 我无法加载带有 spring security 的登录后可以访问的 CSS 页面。我该如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39697077/

相关文章:

css - Kendo UI DropDownList - CSS 的换肤问题

html - 如何在标题中并排放置两个图像

可选的 Spring ResponseEntity

html - CSS导致右侧出现空白

java - JSP 在 Web 浏览器中输出纯文本

java - Spring MVC 和 jackson 不支持内容类型 'application/json'

java - YAJSW Java初学者示例

Java - 我需要一个非常快速的图像缩放算法

Java Lucene 搜索 - 是否可以搜索某个范围内的数字?

java - Spring 3 绑定(bind)模糊了命令对象的真实值