Spring基本身份验证不起作用

时间:2014-01-20 01:46:09

标签: spring spring-security

尝试使用spring在Web应用程序中设置基本身份验证。我正在模仿我在这个例子中做的事情:http://howtodoinjava.com/2013/04/16/custom-userdetailsservice-example-for-spring-3-security/。我正在使用Spring 4.

行为是我的HttpRequestHandler运行,从不挑战密码。 LoginDao永远不会运行。

春天配置:     

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

<security:authentication-manager>
    <security:authentication-provider user-service-ref="loginDao" />
</security:authentication-manager>

<bean id="loginDao" class="weight.dao.LoginDao" />

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/wt.properties" />
</bean>

1 个答案:

答案 0 :(得分:2)

本教程中缺少的是您必须添加到web.xml的条目。这些将加载您的安全上下文并将安全过滤器链添加到您的整个应用程序(这是Spring Security的主要支柱):

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:/your-security-context-file.xml
        classpath:/your-other-context-files.xml
        ...
    </param-value>
</context-param>

<filter>
    <display-name>Spring Security Filter</display-name>
    <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>