【问题标题】:"Link doesn't work" after adding basic Spring Security support添加基本​​ Spring Security 支持后“链接不起作用”
【发布时间】:2012-09-26 23:29:48
【问题描述】:

我有基本的 Spring MVC + Hibernate 应用程序。这是我的web.xml

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

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

一切都很好。 然后我尝试通过将以下内容添加到web.xml,为应用程序添加基本的Spring Security支持:

<filter>
   <filter-name>springSecurityFilterChain</filter-name>
   <filter-class>org.springframework.web.filter.DelegatingFilterProxy
   </filter-class>
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/security-context.xml</param-value>
   </init-param>
</filter>

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

我的/WEB-INF/security-context.xml 如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="http://www.springframework.org/schema/beans"
             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-3.0.xsd 
             http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 

    <http>
        <intercept-url pattern="/index*" access="ROLE_USER"/>
        <form-login login-page="/login.jsp" default-target-url="/index" 
             authentication-failure-url="/login.jsp?error=true"/>
        <logout logout-url="/logout" logout-success-url="/index"/>
        <remember-me/>
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="user" password="pass" authorities="ROLE_USER"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

添加这些内容后,应用程序崩溃了。它只是在 Chrome 中显示“链接不起作用。尝试在 Google 中搜索它。”。 我错过了什么?有任何想法吗?提前致谢。

【问题讨论】:

  • 你在 web.xml 中哪里添加 spring 安全过滤器?
  • 日志中是否有任何异常/错误?控制台?
  • 当显示“链接不起作用”时,您尝试访问的 URL 是什么?
  • @GPS 1) 过滤器添加在 web.xml 的开头,紧跟在 之后; 2) 是的,抛出的异常告诉我,我的一些 xml 格式不正确(Digester 失败),但关键是这些异常甚至在我的应用程序正常运行之前就被抛出了。
  • @Xaerxess 我正在尝试访问 'localhost:8080/index' 因为我的 '/index' url 模式已映射到查看所需信息。没有弹簧安全它可以正常工作。

标签: java spring spring-mvc spring-security


【解决方案1】:
 <?xml version="1.0" encoding="UTF-8" ?> 
 <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    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-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.0.xsd">
   <security:http>         
        <security:intercept-url pattern="/index*" access="ROLE_USER"/>         
        <security:form-login login-page="/login.jsp" default-target-url="/index"               authentication-failure-url="/login.jsp?error=true"/>         
    <security:logout logout-url="/logout" logout-success-url="/index"/>         <security:remember-me/>     
    </security:http>      
    <security:authentication-manager>         
    <security:authentication-provider>             
    <security:user-service>                 
    <security:user name="user" password="pass" authorities="ROLE_USER"/>             </security:user-service>         
    </security:authentication-provider>     
    </security:authentication-manager>  
    </beans> 

尝试使用上面的 security-context.xml 代码。

【讨论】:

    【解决方案2】:

    谢谢大家。刚刚解决了。问题是我在 /WEB-INF/security-context.xml 命名空间中使用了 http://www.springframework.org/schema/security/spring-security-3.0.xsd,但使用了 3.1.2.RELEASE 版本的 spring-security 库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-28
      • 2015-08-19
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      • 2023-03-17
      • 2015-08-27
      相关资源
      最近更新 更多