您的位置:首页 > 运维架构 > Apache

apache shiro 自定义过滤器及使用

2012-09-17 14:08 344 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息