shiro自定义filter,实现同一个url对多个role

提出问题:

/crm_sale/opp/** = roles[“boos”,“管理者”,“销售经理”]
/crm_sale/dev/**=roles[“boos”,“管理者”,“客户经理”]

上面的那种形式同一个路径对应对个角色,是想要访问这个路径必须要有三种身份boos,管理,经理,才能访问/crm_sale/opp/**
要想只要有其中一个身份能访问,必须自定义实现过滤:

package com.my.shiro;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

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

//AuthorizationFilter抽象类事项了javax.servlet.Filter接口,它是个过滤器。  
public class CustomRolesAuthorizationFilter extends AuthorizationFilter {

	@Override
	protected boolean isAccessAllowed(ServletRequest req, ServletResponse resp, Object mappedValue) throws Exception {
		Subject subject = getSubject(req, resp);
		String[] rolesArray = (String[]) mappedValue;

		if (rolesArray == null || rolesArray.length == 0) { // 没有角色限制,有权限访问
			return true;
		}
		for (int i = 0; i < rolesArray.length; i++) {
		System.out.println("$$$$$$$$$$$$$");
			System.out.println(subject.hasRole("管理者"));
			if (subject.hasRole(rolesArray[i])) { // 若当前用户是rolesArray中的任何一个,则有权限访问
				return true;
			}
		}

		return false;
	}
}

还需要将上面的过滤器申明成一个bean,在spring_shiro.xml中将他声明成一个

beanclass="com.my.shiro.CustomRolesAuthorizationFilter">

然后在shiroFilter中添加filters属性,如下:


	
		
		
		
		

		
		

		
			
				
			
		


		
		
			
				
				/logout = logout
				
				/refuse.jsp = anon
				
				/crm_sale/opp/** = roles["boos","管理者","销售经理"]
				/crm_sale/dev/**=roles["boos","管理者","客户经理"]
				
				/js/** = anon
				/images/** = anon
				/styles/** = anon
				/fonts/** = anon
				/umeditor.config.js = anon
				/umeditor.js = anon
				/umeditor.min.js = anon
				/logins.jsp = anon
				/loginshiro = anon
				/datepicker = anon
				/dialogs =anon
				/css = anon
				/themes = anon
				/index.html=authc
				/left.jsp=authc
				
				/** = authc
			
		
	

你可能感兴趣的:(shiro自定义filter,实现同一个url对多个role)