1. 程式人生 > >shiro 登入成功後 不跳轉到 successUrl 的問題解決

shiro 登入成功後 不跳轉到 successUrl 的問題解決

1.重寫 FormAuthenticationFilter 父類的  issueSuccessRedirect 方法

import javax.servlet.ServletRequest;                                                      

import javax.servlet.ServletResponse;                                                     
                                                                                          
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;                        
import org.apache.shiro.web.util.WebUtils;                                                
                                                                                          
public class loginFormAuthenticationFilter extends FormAuthenticationFilter {             
                                                                                          
@Override                                                                             
protected void issueSuccessRedirect(ServletRequest request, ServletResponse response) 
throws Exception {                                                            
WebUtils.issueRedirect(request, response,getSuccessUrl(), null, true);            
}                                                                                     
}                                                                                         

2. 在 applicationContext-shiro.xml 中配置   

                                                                                                           

<!-- 定義shiro 的 web 過濾器 -->                                                                                   
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">                           
<!-- 認證提交地址,如果沒有認證將會請求此地址進行認證 ,請求此地址將由 formAuthenticationFilter 進行表單認證 -->                               
<property name="loginUrl" value="/user/login"></property>                                                
<!-- 沒有許可權  將會跳轉去的 頁面 -->                                                                                 
<property name="unauthorizedUrl" value="/refuse.jsp"></property>                                         
<!-- 認證通過會 要跳轉 的頁面 -->                                                                                   
<property name="successUrl" value="/user/main"></property>                                               
<!-- 安全管理器  -->                                                                                          
<property name="securityManager" ref="securityManager"></property>                                       
<!-- 配置 重寫的 父類方法 issueSuccessFilter  讓自己重寫的方法起到作用  以防止 登入成功後 不調到 successUrl 的問題 -->                      
<property name="filters">                                                                               


         <map>                                                                                                
             <entry key="authc" value-ref="loginFormAuthenticationFilter"/>                                   
         </map>   
     </property>                                                                                              
<!-- shiro 過濾器 鏈配置  -->                                                                                  
<property name="filterChainDefinitions">                                                                 
<value>                                                                                              
<!-- 配置 登出的地址  -->                                                                               
/user/logout = logout                                                                            
<!-- 配置匿名訪問  -->                                                                                 
/js/** = anon                                                                                    
/images/** = anon                                                                                
/css/** = anon                                                                                   
/refuse.jsp = anon                                                                               
/fonts/** = anon                                                                                 
<!-- 所有地址 都需要 身份認證 -->                                                                           
/** = authc                                                                                      
</value>                                                                                             
</property>                                                                                              
</bean>                                                                                                      
                                                                                                             
<!-- 配置  -->                                                                                                 
<bean id="loginFormAuthenticationFilter" class="org.aptech.own.shiro.utils.loginFormAuthenticationFilter"/>