【问题标题】:Spring security Pre authentication success handlerSpring security 预认证成功处理程序
【发布时间】:2014-10-26 16:13:45
【问题描述】:

我有一个网络应用程序,您可以在其中使用 form-login 登录,或者您可以进行预身份验证并像这样登录。两种方法都运行良好,但我只能找到使用 authentication-success-handler-ref 属性将成功处理程序与表单登录一起使用的方法。

我的问题是,如何在我的 security-app-context 中为 PRE_AUTH_FILTER 调用成功处理程序“mySuccessHandler”?我想我可以将它称为 PreAuthenticatedProcessingFilter、preauthAuthProvider 或自定义过滤器下的属性或其他东西。

如果用户具有教师或学生角色,只需要转到不同的页面。

<beans:beans xmlns="http://www.springframework.org/schema/security"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/security
                http://www.springframework.org/schema/security/spring-security-3.2.xsd">


<http pattern="/**" use-expressions="true" create-session="always">
    <intercept-url pattern="/login.jsp*" access="permitAll" />
    <intercept-url pattern="/**/ErrorPages/**" access="permitAll" />
    <intercept-url pattern="/**/Students/**" access="hasAnyRole('STUDENT, TEACHER')" />
    <intercept-url pattern="/**/Teacher/**" access="hasRole('TEACHER')" />
    <intercept-url pattern="/**/Login/**" access="hasRole('ROLE_USER')" />
    <intercept-url pattern="/**/Js/**" access="hasRole('ROLE_USER')" />
    <intercept-url pattern="/**/Css/**" access="permitAll" />
    <intercept-url pattern="/**/Img/**" access="permitAll" />
    <intercept-url pattern="/**/api/**" access="hasRole('ROLE_USER')" />
    <intercept-url pattern="/**" access="denyAll" />
    <custom-filter position="PRE_AUTH_FILTER" ref="PreAuthenticatedProcessingFilter" />
    <access-denied-handler
    <form-login
            username-parameter="idnumber"
            password-parameter="password" login-processing-url="/athuga_innskraningu"
            login-page='/login.jsp'
            authentication-failure-handler-ref="myAuthErrorHandler"
            authentication-success-handler-ref="mySuccessHandler"
            always-use-default-target='true'
            authentication-failure-url="/login.jsp?login_error=true"/>
    <logout logout-url="/utskra/" logout-success-url="/login.jsp"/>
</http>


<beans:bean id="mySuccessHandler" class="is.inna.rest.login.AuthenticationSuccess"/>
<beans:bean id="myAuthErrorHandler" class="is.inna.rest.login.AuthenticationFailure"/>
<beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

<beans:bean name="myUserDetailsService" class="is.inna.rest.login.UserDetailServiceLogin" />
<beans:bean id="userDetailsServiceWrapper"  class="is.inna.rest.login.UserDetailServicePreAuth" />

<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="myUserDetailsService">
        <password-encoder ref="passwordEncoder" />
    </authentication-provider>
    <authentication-provider ref="preauthAuthProvider" />
</authentication-manager>


<beans:bean id="preauthAuthProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
    <beans:property name="preAuthenticatedUserDetailsService" ref="userDetailsServiceWrapper"/>
</beans:bean>
<beans:bean id="PreAuthenticatedProcessingFilter" class="is.inna.rest.login.PreAuthenticatedProcessingFilter">
    <beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>

【问题讨论】:

    标签: java spring spring-security


    【解决方案1】:

    您的要求是根据角色将用户重定向到不同的页面。您也可以使用身份验证成功处理程序来执行此操作。请参阅我编写的示例success handler 课程。您始终可以在覆盖的 onAuthenticationSuccess 方法中访问 Authentication 对象。您可以获得登录用户的权限和角色,并且根据它,您可以随时将用户重定向到适当的页面。

    希望这会有所帮助。

    【讨论】:

    • 感谢您的评论,我制作的成功处理程序与您一模一样。并在我的 security-context.xml 文件中添加了 。但是成功处理程序中的代码从未被调用,知道为什么会这样吗?
    • 是的,您正在调用使用 SavedRequestAwareAuthenticationSuccessHandler 的 authentication-success-handler-ref="authHandler",但是当我使用预授权过滤器时,我需要知道如何调用它。
    • 为什么在认证完成之前调用处理程序?只有当用户被证明是有效用户时,才将用户重定向到不同的页面是有意义的。所以在这里使用身份验证成功处理程序是一种正确的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2012-08-15
    • 2011-11-15
    • 1970-01-01
    • 2012-09-25
    • 2016-08-20
    • 2016-02-03
    相关资源
    最近更新 更多