简体   繁体   中英

I can not load the CSS pages which can be accessed after a login carry with spring security. How can I fix it?

I should add CSS to two pages to which it is accessed after a login for which data are checked with spring security

How do I add CSS to these two pages protected with spring security?

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/" />

According to above line,you should make sure that your css files is under location="/resources/" (resources folder).If they are present in some other folder make change location path.

And in spring-security.xml add below line so that for resources no security is required.

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

After that in jsp .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM