XML中的Spring Security 3配置

时间:2011-11-17 13:46:48

标签: spring-security

我已经尝试通过XML配置Spring Security一段时间了,但我似乎无法让它工作。以下是我到目前为止的情况:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                           http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/security
                           http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    [...]

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

    <security:authentication-manager>
        <security:authentication-provider>
            [???] <!-- What goes here? -->
        </security:authentication-provider>
    </security:authentication-manager>

</beans>

我发现的所有教程似乎都希望我将<user-service>放在占位符中,但NetBeans不会自动完成该元素。唯一类似于该元素的是any-user-service,据我所知,这是一个&#34;抽象&#34;元件。

我只想配置内存中的用户和密码列表。我如何在Spring Security版本3中执行此操作?

2 个答案:

答案 0 :(得分:2)

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

<bean id="userService" class="path.to.your.implementation.of.UserDetailsService" />

或者你可以有一个基本的内存认证(而不是,以及):

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

<security:user-service id="customAdmin">
 <security:user name="yourUserName" password="yourPassword" authorities="ROLE_USER, ROLE_ADMIN" />
 <security:user name="yourOtherUserName" password="yourOtherPassword" authorities="ROLE_USER, ROLE_ADMIN" />
</security:user-service>

官方春季文档总是best place to read,imho。

答案 1 :(得分:1)

编写自己的org.springframework.security.authentication.AuthenticationProvider,创建bean并提供对身份验证管理器的引用:

<authentication-manager>
    <authentication-provider ref="com.example.CustomAuthenticationProvider"/>
</authentication-manager>  

或者你可以只提供相关权限的用户名和密码(我在模拟时使用它)

<authentication-manager>
    <authentication-provider>
        <user-service>
          <user name="test" password="test" authorities="ROLE_AUTHENTICATED" />
        </user-service>
    </authentication-provider>
</authentication-manager>