apache shiro 自定义过滤器及使用

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>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值