SpringSecurity 投票器

版权声明:转载请随意! https://blog.csdn.net/qq_41723615/article/details/89518290

访问控制

在现在的一些软件设计操作上,考虑安全性以及操作性的平衡点,所以一般在本地服务器上(你现在使的电脑就是你发布的服务器),

本地服务器可以不受用户登录的限制。

在springSecurity之中提供有一个投票器的概念,投票器有以下几种:

     支持:如果在本地,就不受限制

     反对:不是在本地,就反对

     弃权:一切照旧

范例:实现投票器

package jcn;

import java.util.Collection;
import java.util.Iterator;

import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

public  class IPAddressVote implements AccessDecisionVoter<Object> {

	//判断是否支持投票操作
	@Override
	public boolean supports(ConfigAttribute attribute) {
		//有一个属性是"IP_LOCAL_HOST"
		return attribute.getAttribute() != null && attribute.getAttribute().startsWith("IP_");
	}
	//根据类型判断
	@Override
	public boolean supports(Class<?> arg0) {
		return false;
	}
	//投票过程
	@Override
	public int vote(Authentication aut, Object object,
			Collection<ConfigAttribute> attributes ){
		//现在根据ip地址判断,并且项目不是通过WEB认证进行的
		if (!(aut.getDetails() instanceof WebAuthenticationDetails)) {
			return AccessDecisionVoter.ACCESS_DENIED; //拒绝你操作
		}
		//如果现在是来自WEB的认证授权操作
		WebAuthenticationDetails datails = (WebAuthenticationDetails) aut.getDetails();
		String ip = datails.getRemoteAddress();//取得IP地址
		Iterator<ConfigAttribute> iter = attributes.iterator();
		while (iter.hasNext()) {
			ConfigAttribute caAttribute = iter.next();
			//如果是本地ip地址
			if ("IP_LOCAL_HOST".equals(caAttribute.getAttribute())) {
				if ("0:0:0:0:0:0:0:1".equals(ip)) {//是本机的ip
					return AccessDecisionVoter.ACCESS_DENIED;
				}
			}
		}
		return AccessDecisionVoter.ACCESS_ABSTAIN;
	}

}

在安全框架中配置投票器:

<!-- 定义投票器 -->
<bean id="accessDes" class = "org.springframework.security.access.vote.AffirmativeBased">
	<property name="decisionVoters">
		<list>
			<bean class="org.springframework.security.access.vote.RoleVoter"/>
			<bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
			<bean class="jcn.IPAddressVote"/>
		</list>
	</property>
</bean>
<!-- 启动Spring安全验证 -->
<security:global-method-security jsr250-annotations="enabled"
access-decision-manager-ref="accessDes"/>

此时就可以利用投票器的方式来处理操作。

修改Action程序类,增加本地认证服务:

由于操作者是处于服务器本地ip服务上,所以此时可以直接进行数据的操作。

猜你喜欢

转载自blog.csdn.net/qq_41723615/article/details/89518290