shiro (八) spring结合 --自定义过滤器(角色篇)

package com.miv.shiro.common;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import org.apache.shiro.SecurityUtils;

import org.apache.shiro.subject.Subject;

import org.apache.shiro.web.filter.authz.AuthorizationFilter;

public class MIVshiroFilter extends AuthorizationFilter {

    @Override

    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object weaving)

            throws Exception {

        // TODO Auto-generated method stub

        Subject subject = SecurityUtils.getSubject();

        String[] weaving_ = (String[]) weaving;

        String[] rolesArray = weaving_[0].split(",");

        if (rolesArray == null || rolesArray.length == 0) {

            return true;

        }

        for (int i = 0; i < rolesArray.length; i++) {

            if (subject.hasRole(rolesArray[i])) {

                return true;

            }

        }

        if (subject.isAuthenticated()) {// 认证

            // subject.getSession().;

            // ((HttpServletRequest) request).getSession().invalidate();

            subject.logout();

        } else if (subject.isRemembered()) {// 记住我

            subject.logout();

            // ((HttpServletRequest) request).getSession().invalidate();

        } else {// 其他

        }

        subject.getSession().setAttribute("role_code", rolesArray);

        return false;

    }

}

相关推荐