大佬教程收集整理的这篇文章主要介绍了如何使用Spring安全性和spring安全对象的属性限制对URL的访问?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Spring 5.1和Spring security 4.2.我使用XML文件配置了访问规则.我的问题是,如何根据Spring安全上下文中的属性编写拦截规则(对URL的访问控制)?也就是说,我有一个变量
productList
在安全上下文中,类型为Java.util.ArrayList.如果此列表为空或null,我想限制对URL的访问.我怎么写这个?我有
<http name="defaultSecurity" security-context-repository-ref="myContextRepository"
auto-config="false" use-expressions="true" authentication-manager-ref="authenticationManager"
entry-point-ref="loginUrlAuthenticationEntryPoint">
...
但当然,上面
length(principal.productList) > 0
>在SecurityExpressionOperations上创建包装器并实现所需的操作:
class MySecurityExpressionOperations implements SecurityExpressionOperations {
private SecurityExpressionOperations delegate;
public MySecurityExpressionOperations(SecurityExpressionOperations delegatE) {
this.delegate = delegate;
}
public Boolean hasProducts() {
MyUser user = (MyUser) delegate.getAuthentication().getPrincipal();
return !user.getProductList().isEmpty();
}
// Other methods
}
>扩展org.springframework.security.web.access.expression.WebExpressionVoter并替换标准表达式处理程序:
class MyWebExpressionVoter extends WebExpressionVoter {
public MyWebExpressionVoter() {
setExpressionHandler(new DefaultWebSecurityExpressionHandler() {
@Override
protected SecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication,FilterInvocation fi) {
SecurityExpressionOperations delegate = super.createSecurityExpressionRoot(authentication,fi);
return new MySecurityExpressionOperations(delegatE);
}
});
}
}
>提供自定义访问决策管理器:
ecurity.access.Vote.AffirmativeBased">
Voter"/>
>应用自定义访问决策管理器:
以上是大佬教程为你收集整理的如何使用Spring安全性和spring安全对象的属性限制对URL的访问?全部内容,希望文章能够帮你解决如何使用Spring安全性和spring安全对象的属性限制对URL的访问?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。