使用shiro框架,註銷問題的解決
在使用shiro框架的時候,有時候會因為登錄問題找不到註銷的controller。所以會報404的錯誤,下面是解決辦法:
1.首先寫一個類SystemLogoutFilter繼承LogoutFilter類,具體代碼如下,註意要貼@Service標簽:
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.shiro.session.SessionException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.springframework.stereotype.Service;
/**
* @author Abby
*
*/
@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);
String redirectUrl="/login.jsp";
System.out.println(redirectUrl);
try {
subject.logout();
} catch (SessionException ise) {
ise.printStackTrace();
}
issueRedirect(request, response, redirectUrl);
//返回false表示不執行後續的過濾器,直接返回跳轉到登錄頁面
return false;
}
}
2.需要在shiro配置文件裏面添加一些新的配置,具體配置如下:
<property name="filters">
<map>
<entry key="logout" value-ref="systemLogoutFilter" />
</map>
</property>
<property name="filterChainDefinitions">
<value>
/logout=logout
</value>
</property>
使用shiro框架,註銷問題的解決