---
title: "<shrio学习>-扩展FormAuthenticationFilter实现动态改变LoginUrl"
author: "longload"
date: "2016-02-24T12:41:08Z"
tags: []
categories: ["编程语言"]
slug: "shrio-formauthenticationfilter-loginurl"
draft: false
lastmod: "2016-06-03T14:37:45Z"
id: 2278453
url: "https://longload.iteye.com/blog/2278453"
---

1.修改配置文件

```
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
        ...  
        <property name="filters">  
            <util:map>  
                 <entry key="authc" value-ref="formAuthenticationFilter"/>  
            </util:map>  
        </property>  
...  
</bean>  
<!-- 自定义验证拦截器 -->  
    <bean id="formAuthenticationFilter" class="****.LZFormAuthenticationFilter"/>  
```

 

**2.扩展FormAuthenticationFilter** 

```
public class LZFormAuthenticationFilter extends  FormAuthenticationFilter {
	/**
	 * 重写登录地址
	 */
	protected void redirectToLogin(ServletRequest request,ServletResponse response) throws IOException {
		//request.getLocalAddr()
		HttpServletRequest req=(HttpServletRequest) request;
	String loginUrl = getLoginUrl();
	String url=req.getRequestURI();
	if(url.contains("/mine/")){
		loginUrl="/web/teacher/list.htm";
	}
	WebUtils.issueRedirect(request, response, loginUrl);
	}
}
```

**3. 动态增加shiro的filter地址，可以继承**DefaultFilterChainManager 

```
<property name="filterChainDefinitions">
			<value>
	/favicon.ico = anon
				/resources/**=anon
				/**/*.jpg = anon
</value>
		</property>
```