Spring安全性预认证成功处理程序

时间:2014-09-02 11:47:17

标签: java spring spring-security

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

我的问题是,如何调用成功处理程序" mySuccessHandler"对于我的security-app-context中的PRE_AUTH_FILTER?我猜我可以将它称为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>

1 个答案:

答案 0 :(得分:3)

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

希望这有帮助。