2011-09-09 56 views
1

我必須實現LoginController來登錄用戶,驗證密碼並保護一些資源或方法。詢問彈簧安全中登錄用戶的密碼

方案1) 可以說,用戶沒有用系統登錄。他嘗試訪問一個方法,那個時候我需要重定向到login.jsp。登錄過程後需要重定向到原始位置來自哪個正確的網址。

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。

請給我一個建議。

回答

0

在這種情況下,春季安全沒有開箱即用的解決方案,因此您必須自行實施。一個解決方案的想法可能是這樣的:定義一個新的角色「ROLE_VERIFIEDPIN」。將此角色添加到您的方法的安全註釋中。將此角色的手寫(if語句)檢查添加到調用受保護方法的Web控制器方法。如果使用具有已驗證的引腳角色,則調用受保護的方法,如果不將其重定向到驗證頁面。如果驗證成功,則授予他角色並調用「攔截」控制小程序方法。

+0

Ralph,謝謝你的迴應。我仍然不清楚這個答案。你可以請一些示例代碼或僞代碼來理解。謝謝 – Steve