public class MyFilter implements Filter {
private String myLoginUrl = "/error/4031";
private final String default_content_type = "application/x-www-form-urlencoded";
public void setMyLoginUrl(String myLoginUrl) {
this.myLoginUrl = myLoginUrl;
}
public String getMyLoginUrl() {
return myLoginUrl;
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String contentType = request.getContentType();
Subject currentUser = SecurityUtils.getSubject();
if (!currentUser.isAuthenticated()) {
HttpServletResponse res = (HttpServletResponse)response;
HttpServletRequest req = (HttpServletRequest)request;
if(contentType == null){
res.sendRedirect(req.getContextPath() + "/xx/xx");
return;
}else{
PrintWriter out = res.getWriter();
out.print("EXPIRED");
out.flush();
return;
}
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
applicationContext-shiro.xml
<!-- Shiro Filter -->
<bean id="bobingFilter" class="com.hymake.bobing.util.BobingSessionTimeOut" />
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" depends-on="myFilter">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/xx/xx/" />
<property name="filters">
<map>
<entry key="myFilter" value-ref="myFilter"/>//1官网说是//1 地方是不用配置的 shiro会自动添加实现了Filter接口的类
</map>
</property>
<property name="filterChainDefinitions">
<value>
/someindex = anon
/login = authc
/logout = logout
/static/** = anon
/admin/** = user
/account/** = user
/some/** = myFilter
</value>
</property>
</bean>