2017-03-17 46 views
0

嗨,我正在學習一個項目的Spring,並試圖教自己使用sec標籤來創建多個視圖,當不同的用戶登錄時。我一直遇到設置標籤的錯誤。春季新手 - 使用sec標記不起作用

這裏是我的JSP:

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> 

<html> 
<head> 
<title>Welcome</title> 
</head> 
<body> 
    <h1>Welcome</h1> 
    <security:authorize access="hasRole('admin')"> 
    Only admins can see the <a href="second">Second link</a> 
    </security:authorize> 
</body> 
</html> 

這是給錯誤:無法找到的標籤庫描述符 「http://www.springframework.org/security/tags

這裏是我的xml:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:security="http://www.springframework.org/schema/security" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd 
    http://www.springframework.org/schema/security 
      http://www.springframework.org/schema/security/spring-security-4.0.xsd 
    "> 



    <mvc:annotation-driven /> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/views/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 

    <!-- Filter for role checking --> 
<bean id="filterSecurityInterceptor" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor"> 
    <property name="authenticationManager" ref="authenticationManager"/> 
    <property name="accessDecisionManager" ref="httpRequestAccessDecisionManager"/> 
    <property name="securityMetadataSource"> 
     <security:filter-security-metadata-source lowercase-comparisons="true" request-matcher="ant" use-expressions="true"> 
      <security:intercept-url pattern="/pages/Security/**" access="permitAll"/> 
      <security:intercept-url pattern="/resources/**" access="permitAll"/> 
      <security:intercept-url pattern="/pages/Settings/**" access="hasRole('SETTINGS')"/> 
      <security:intercept-url pattern="/pages/Home/*" access="hasRole('HOME')"/>    
      <security:intercept-url pattern="/pages/Admin/**" access="hasRole('ADMINISTRATOR')"/> 
      <security:intercept-url pattern="/servlet/Download" access="hasAnyRole('DOWNLOAD','PREMIUM_ACCOUNT')"/> 

      <security:intercept-url pattern="/**" access="isAuthenticated()"/> 
     </security:filter-security-metadata-source> 
    </property> 
</bean> 

<!-- webInvocationPrivilegeEvaluator necessary to use <sec:authorized url="xx"> --> 
<bean id="webInvocationPrivilegeEvaluator" class="org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator"> 
    <constructor-arg ref="filterSecurityInterceptor"></constructor-arg> 
</bean> 

</beans> 

我已經來回切換「秒」到「安全」等一些事情,但無法讓它工作。預先感謝您的幫助。

+0

搜索添加這種依賴在你的pom.xml的'找不到標籤庫descriptor' –

回答

2

添加彈簧安全標籤庫,3.1.3.release.jar到您的項目的類路徑

如果您正在使用Maven的

<dependency> 
    <groupId>org.springframework.security</groupId> 
    <artifactId>spring-security-taglibs</artifactId> 
    <version>3.1.3.RELEASE</version> 
</dependency> 
+0

您可以糾正,讓我更新我的答案。 –