6

我是弹簧和弹簧安全的新手,

我已经了解 bean 是如何在 xml 文件中创建和引用的,我需要在我的应用程序中使用 spring 来提供安全性。

我在 web.xml 中包含了一个自定义 applicationContext-security.xml 文件: contextConfigLocation

在这个文件中,我使用截获了 url 模式

<intercept-url pattern='/**.something' access="IS_AUTHENTICATED_FULLY"/>

内部元素。

我现在已经设置了登录表单,如果页面未经授权,它会显示我的自定义 Login.html 页面。

现在对于我面临的问题:

  1. 如何指定我的登录表单以将其值传递给 spring ?
  2. 如何使用我自己的 authentication-provider ?

我试过这个:

<authentication-provider user-service-ref="userDetailsService"/>
<beans:bean id = "userDetailsService" class ="com.somepath.CustomAuthenticationProvider">
        <custom-authentication-provider/>
    </beans:bean>

其中 CustomAuthenticationProvider 实现 AuthenticationProvider

但代码抛出一个错误:Error Creating bean with name '_filterChainProxy' .... No UserDetailsS​​ervice registered

请帮忙

4

2 回答 2

7

1:如何指定我的登录表单以将其值传递给 spring ?

在 web.xml 中为 Spring Security 设置标准 Spring 过滤器后,使用<http>标签配置的一些默认设置。为您创建一个实例AuthenticationProcessingFilter 作为过滤器链的一部分。

我的默认AuthenticationProcessingFilter设置为读取 j_username 和 j_password 作为用户名/密码令牌。

为了覆盖这一点,通过执行以下操作将您的自定义 AuthenticationProcessingFilter 替换为默认值:

<bean id=“myAuthFilter” class=“org.springframework.security.ui.webapp.AuthenticationProcessingFilter” >
<security:custom-filter position=“AUTHENTICATION_PROCESSING_FILTER”/><!–-replace the default one-–&gt;
  <property name=“usernameParameter” value=“myUsername”/><!-- myUsername is the name of the input tag where user enter their username on the HTML page -->
  <property name=“passwordParameter” value=“myPassword” /><!–- myPassword is the name of the input tag where user enter their password on the HTML page -–&gt;
</bean>

另请参阅 AuthenticationProcessingFilter 的 JavaDoc 了解更多详细信息: http ://static.springsource.org/spring-security/site/apidocs/org/springframework/security/ui/webapp/AuthenticationProcessingFilter.html

2:如何使用自己的身份验证提供程序?

使用以下代码:

<bean id="myAuthenticationProvider" class="com.something.MyAuthenticationProvider">
    <security:custom-authentication-provider />
</bean>

<security:custom-authentication-provider />是让我们知道这是一个自定义提供程序的标签,并且身份验证管理器应该在其提供程序链中使用它。

来源http ://static.springsource.org/spring-security/site/docs/2.0.x/reference/appendix-namespace.html#d4e3379

3:关于代码抛出'_filterChainProxy'的问题......没有注册UserDetailsS​​ervice......'

是否com.somepath.CustomAuthenticationProvider实现了 UserDetailService 接口?

于 2009-10-21T13:42:30.757 回答
1

我自己对 Spring 有点陌生,但我会尽力帮助你。拦截网址看起来不错。

我不认为身份验证提供者是正确的。看看我的代码:

  <beans:bean id="MyUserDetailsService" class="path.to.MyAuthenticationService"/>

<beans:bean id="userDetailsService" class="org.springframework.security.userdetails.hierarchicalroles.UserDetailsServiceWrapper" >
    <beans:property name="roleHierarchy" ref="roleHierarchy" />
    <beans:property name="userDetailsService">
      <beans:ref bean="MyUserDetailsService"/>
    </beans:property>
  </beans:bean>
 <authentication-provider user-service-ref="userDetailsService">
   <password-encoder hash="md5"/>
 </authentication-provider>

您可能不需要角色层次结构。

您在 jsp 页面上有一个登录表单。表格应该像这样开始:

<form:form modelAttribute="login">

此外,您必须映射适当的字段。

<form:input path="login">
<form:password path="password">

在您的 applicationContext-security.xml 设置登录页面:

<form-login login-page="/login.jsp" default-target-url="/login.html" always-use-default-target="true" authentication-failure-url="/login.jsp?login_error=1"/>

login.html 应该映射到您的 LoginController.java,它扩展了 BaseController 并实现了一个登录方法,该方法至少需要一个 HttpServletRequest 和 Model 作为参数。我的然后通过调用以下 Spring 类/方法来工作:

String userlogin = SecurityContextHolder.getContext().getAuthentication().getName();

如果您的 CustomAuthenticationProvider 实现正确,您可以(希望)从您的模型中获取用户的详细信息,最后:

return "redirect:homepage.html";

如果您仍然遇到问题,我可能错过了一些东西,请在评论中告诉我。

于 2009-10-21T13:26:30.320 回答