java - Spring 新手 - 使用 sec 标签不起作用

标签 java spring jsp spring-mvc

嗨,我正在为一个项目学习 Spring,并试图自学 sec 标签,以便在不同用户登录时创建多个 View 。我在设置标签时不断遇到错误。

这是我的 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>

我来回切换“秒”到“安全”和其他一些东西,但无法让它工作。预先感谢您的帮助。

最佳答案

spring-security-taglibs-3.1.3.release.jar添加到项目的类路径中。

如果您使用Maven,请在 pom.xml 中添加此依赖项

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

关于java - Spring 新手 - 使用 sec 标签不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42848117/

相关文章:

java - 从 Java 代码向 Yarn 提交 Python 应用程序时出现问题

java - 为什么我没有操作的对象在Java中发生了变化?

java - 如何打印带有缺少反斜杠的unicode字符的字符串?

java - 如何 Autowiring Spring Stream 云绑定(bind)

java - 我们必须关闭 session 对象吗?

spring - 客户端发送的请求语法错误。-Spring MVC + JDBC Template

java - 如何在没有实体管理器的情况下使用 JPA setMaxResults() (LIMIT 20)?

java - 将 xml 定义中的附加 bean 注册到已初始化的应用程序上下文中

javascript - 忽略验证中隐藏的 div

java - JSP 数据到 Servlet 使用 EL