在spring security中为登录用户询问密码

时间:2011-09-09 16:50:53

标签: spring spring-security

我必须实现LoginController来登录用户,验证密码并保护一些资源或方法。

方案1) 比方说,用户没有使用系统登录。他试图访问一个方法,那时我需要重定向到login.jsp。登录过程后需要重定向到原始位置来自的正确URL。

scenario2) 让我们说用户已经登录并尝试访问一些受保护的方法。现在我需要重定向到verifyPassword.jsp以再次验证密码。

方案1对我来说很好。

我在我的security.xml中使用

<security:global-method-security
        secured-annotations="enabled" 
        jsr250-annotations="disabled" 
        access-decision-manager-ref="accessDecisionManager"
    />




<security:http entry-point-ref="authEntryPoint" access-denied-page="/accessdenied.action" access-decision-manager-ref="accessDecisionManager" >
        <security:intercept-url pattern="/js/**" filters="none" />

        <security:anonymous/>

        <security:http-basic />
        <security:port-mappings >
            <security:port-mapping http="8080" https="443"/>
        </security:port-mappings>
        <security:logout invalidate-session="true" />
        <security:custom-filter position="FORM_LOGIN_FILTER" ref="customizedFormLoginFilter"/>
    </security:http>


<bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
        <constructor-arg index="0" value="256" />
    </bean>



<bean id="roleHierarchy" class="org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl">
        <property name="hierarchy">
            <value>
                ROLE_PORTAL_RESTRICTED_USER > ROLE_USER
            </value>
        </property>
    </bean>

<bean id="userDetailsService" class="org.springframework.security.access.hierarchicalroles.UserDetailsServiceWrapper">
        <property name="userDetailsService">
            <bean class="com.java.CustomeUserDetails" />
        </property>
        <property name="roleHierarchy" ref="roleHierarchy" /> 
    </bean>

<bean id="authEntryPoint" class="org.springframework.security.web.authentication.AuthenticationProcessingFilterEntryPoint">
        <property name="forceHttps" value="false" />
        <property name="loginFormUrl" value="/login.action" />

        <property name="portMapper">
            <bean class="org.springframework.security.web.PortMapperImpl">
                <property name="portMappings">
                    <map>
                        <entry key="8080" value="443"/>
                    </map>
                </property>
            </bean>
        </property>
    </bean>


<bean id="customizedFormLoginFilter"

         class ="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter" >
        <property name="allowSessionCreation" value="true" />
        <property name="filterProcessesUrl" value="/j_spring_security_check" />
        <property name="authenticationManager" ref="authenticationManager" />
        <property name="authenticationFailureHandler" ref="failureHandler" />
        <property name="authenticationSuccessHandler" ref="successHandler" />
    </bean> 

    <bean id="successHandler"
        class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
            <property name="alwaysUseDefaultTargetUrl" value="false"/>
        <property name="defaultTargetUrl" value="/loginsuccess.action" />
    </bean>

    <bean id="failureHandler"
        class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
        <property name="defaultFailureUrl" value="/loginfailed.action" />
    </bean>



    <security:authentication-manager  alias="authenticationManager">
        <security:authentication-provider user-service-ref="userDetailsService">
          <security:password-encoder ref="passwordEncoder">
                <security:salt-source user-property="username" />
            </security:password-encoder>

        </security:authentication-provider>

    </security:authentication-manager>

我使用注释方法来请求登录。

@Secured("ROLE_USER")
    public ModelAndView protectedMethod()

我猜想我提供了所有信息。如何为登录用户重定向到verifypin.jsp。

请给我一个建议。

1 个答案:

答案 0 :(得分:0)

在这种情况下,Spring安全性没有开箱即用的解决方案,因此您必须自己实现它。解决方案的想法可能是:定义一个新角色“ROLE_VERIFIEDPIN”。将此角色添加到方法的安全注释中。将此角色的手写(if语句)检查添加到调用受保护方法的Web控制器方法。如果用户具有经过验证的pin角色,则调用安全方法,如果没有将其重定向到验证页面。如果验证成功,则授予他角色并调用“截获的”控制方法。

相关问题