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

坚强是说给别人听的谎言 提交于 2019-12-25 08:01:33

问题


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;
        }

回答1:


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


来源:https://stackoverflow.com/questions/39697077/i-can-not-load-the-css-pages-which-can-be-accessed-after-a-login-carry-with-spri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!