首页 文章

Spring - 安全性:如何将登录用户名和密码绑定到身份验证提供程序?

提问于
浏览
6

我是 Spring 天和 Spring 天安全的新手,

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

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

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

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

内部元素 .

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

现在我面临的问题是:

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

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

我试过这个:

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

其中CustomAuthenticationProvider实现AuthenticationProvider

但代码抛出一个错误:创建名为'_filterChainProxy'的bean时出错....没有UserDetailsService注册

请帮忙

2 回答

  • 1

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

    在用于Spring Security的web.xml中设置标准Spring Filter之后,使用 <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-–>
      <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 -–>
    </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 /> 是让Spring知道这是自定义提供程序的标记,并且Authentication Manager应该在其提供程序链中使用它 .

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

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

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

  • 7

    我自己对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>
    

    你可能不需要角色heirarchy .

    您在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";
    

    如果你仍然遇到麻烦,我可能会错过一些评论 .

相关问题