在Spring Security的基础知识上挣扎 - 需要启用sec taglib

时间:2017-04-11 02:44:31

标签: java spring spring-security

我几天来一直在试着各种教程让Spring Security工作。我正在自学一个学校项目。我有一个教程的基本登录功能,现在我正在尝试实现sec taglib,因此我可以使用authorize标签根据用户的角色有选择地向用户显示内容。无论我尝试的教程/官方文档中的建议组合如何,我都无法使其工作(我确信它来自我对XML配置的愚蠢理解)。非常感谢任何帮助。

这是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.angels</groupId>
<artifactId>ccollier</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.3</version>
    </dependency>
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.2.RELEASE</version>
    </dependency>
    <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.0.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>1.11.1</version>
    </dependency>



    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.0.1.RELEASE</version>
    </dependency>

   <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.0.1.RELEASE</version>
    </dependency>   

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

</dependencies>


<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <source>1.8</source>
                    <target>1.8</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                    <contextReloadable>true</contextReloadable>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

我的web.xml:

<!-- webapp/WEB-INF/web.xml -->
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<display-name>To do List</display-name>

<welcome-file-list>
    <welcome-file>login.do</welcome-file>
</welcome-file-list>

 <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/todo-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-
class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
   </filter>


   <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
   </filter-mapping>

</web-app>

我的应用程序命名空间(todo-servlet.xml):

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.3.xsd">

    <context:component-scan base-package="com.angels" />
    <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>
<mvc:resources mapping="/resources/**" location="/resources/"
cache-period="31556926"/>
     <mvc:resources mapping="/webjars/**" location="/webjars/"/>
    <mvc:annotation-driven />
    <http auto-config="true">
        <intercept-url pattern="/**" access="ROLE_USER" />
    </http>

</beans>

现在导致问题的是来自应用程序命名空间的这一行。我直接从Spring文档http://docs.spring.io/spring-security/site/docs/3.1.x/reference/ns-config.html中取得了这一点。它表示需要这些行来启用Web安全性。

<http auto-config="true">
<intercept-url pattern="/**" access="ROLE_USER" />
</http>

当我将它悬停在Eclipse上时,我得到:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'http'. One of '{"http://
 www.springframework.org/schema/beans":import, "http://www.springframework.org/schema/beans":alias, "http://
 www.springframework.org/schema/beans":bean, WC[##other:"http://www.springframework.org/schema/beans"], "http://
 www.springframework.org/schema/beans":beans}'  is expected.

我在尝试运行我的应用程序时遇到类似的错误,导致它无法启动。感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

xml中的默认命名空间是beans

xmlns="http://www.springframework.org/schema/beans"

但是http属于spring security,所以你应该在http元素之前添加名称空间,你已经在xml中添加了名称空间定义

xmlns:security="http://www.springframework.org/schema/security"

所以你只需要将http更改为

<security:http auto-config="true">
    <security:intercept-url pattern="/**" access="ROLE_USER" />
</security:http>

有关名称空间的更多信息,请参阅here;