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

 


	

		
		
		
		
			
				//1官网说是//1 地方是不用配置的  shiro会自动添加实现了Filter接口的类
			
		
		
			
				/someindex = anon
				/login = authc
				/logout = logout
				/static/** = anon
		    	/admin/** = user 
		    	/account/** = user
		    	/some/** = myFilter
		        
		
	

 

 

你可能感兴趣的:(学习点滴)