在使用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>