思路:
1.shiro有許多過濾器,其中就有一個LogoutFilter的過濾器,只需要繼承並重寫preHandle方法
2.在配置文件中注入自定義實現的過濾器,在此配置當退出的時候應重定向到某個頁面
3.注入自定義filter,並且配置過濾器鏈
4.直接在頁面中配置相應退出鏈接
1.自定義實現LogoutFilter
@Service
public class SystemLogoutFilter extends LogoutFilter {
@Override
protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
//在這裏執行退出系統前需要清空的數據
Subject subject=getSubject(request,response);
String redirectUrl=getRedirectUrl(request,response,subject);
ServletContext context= request.getServletContext();
try {
subject.logout();
context.removeAttribute("error");
}catch (SessionException e){
e.printStackTrace();
}
issueRedirect(request,response,redirectUrl);
return false;
}
}
2.配置要重定向的路徑
<!--自定義LogoutFilter,退出-->
<bean id="logoutFilter" class="filter.SystemLogoutFilter">
<property name="redirectUrl" value="/admin/login"/>
</bean>
第三步:
<!--自定義的filter-->
<property name="filters">
<map>
<!-- 將自定義 的FormAuthenticationFilter注入shiroFilter中 -->
<entry key="authc" value-ref="formAuthenticationFilter"/>
<entry key="logout" value-ref="logoutFilter"/>
</map>
</property>
<!-- 過慮器鏈定義,從上向下順序執行,一般將/**放在最下邊 -->
<property name="filterChainDefinitions">
<value>
<!--對靜態資源設置匿名訪問-->
/resources/images/**=anon
/resources/style/**=anon
/resources/bootstrap/**=anon
/resources/fonts/**=anon
/resources/jquery/**=anon
/resources/layui/**=anon
/resources/js/**=anon
/favicon.ico=anon
/front/**=anon
<!--/kaptcha**=anon-->
/admin/**=authc
<!-- 請求logout.action地址,shiro去清除session -->
/logout=logout //這纔是對退出的配置
<!-- /**=authc 表示所有的url都必須認證通過纔可以訪問-->
/**=anon
</value>
</property>
4.:直接在頁面相應的退出操作中配置鏈接:/logout 即可,並不需要實現controller層