Spring Security无法正常工作。我究竟做错了什么?

时间:2012-05-02 20:56:22

标签: spring spring-security

正如标题所暗示的那样,我遇到了一个简单的Spring Security Test的问题。这是我的项目结构(maven webapp 2.5):

main
 java
   de
     cochu
       spring
         controller
           HomeController
webapp
  WEB-INF
    jsp
      home.jsp
      index.jsp
    security-context.xml
    spring-servlet.xml
    web.xml

web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/security-context.xml
    </param-value>
</context-param>

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

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

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/spring/*</url-pattern>
</servlet-mapping>

弹簧servlet.xml中

<context:annotation-config/>
<context:component-scan base-package="de.cochu.spring.controller"/>

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

安全context.xml中

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

<security:authentication-manager>
    <security:authentication-provider>
        <security:user-service>
            <security:user name="test" password="test" authorities="ROLE_USER"/>
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>

HomeController.java

@Controller
@RequestMapping( "/" )
public class HomeController {

    @RequestMapping( method = RequestMethod.GET )
    public String show() {
        return "index";
    }

    @RequestMapping( value = "/secure", method = RequestMethod.GET )
    public String secure() {
        return "home";
    }
}

确切的问题:没有登录表单或任何打开。它只显示页面。我尝试了几乎每个url-pattern组合/拦截-l url组合,但没有反应。有什么问题?

1 个答案:

答案 0 :(得分:7)

FilterChainProxy bean已在别名springSecurityFilterChain注册,因此请尝试修改您的web.xml并更改此

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

到这个

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

这是我经常使用的配置(使用spring security 3.1.0.RELEASE)