<security:authorize access="hasPermission( 'formDialog', 'edit')">,xml如何配置点击hasPermission方法直接调用MyPermissionEvaluator中的方法
时间: 2024-01-22 20:03:56 浏览: 132
要在XML配置文件中使用自定义的PermissionEvaluator,需要进行以下步骤:
1. 创建一个继承自DefaultWebSecurityExpressionHandler的类,例如CustomWebSecurityExpressionHandler。
2. 在CustomWebSecurityExpressionHandler类中重写createSecurityExpressionRoot方法,并且在该方法中创建一个继承自SecurityExpressionRoot的类的实例,例如CustomSecurityExpressionRoot。
3. 在CustomSecurityExpressionRoot类中重写hasPermission方法,并且在该方法中调用自定义的PermissionEvaluator中的hasPermission方法。
4. 在XML配置文件中使用<sec:global-method-security>元素来启用全局方法安全性,同时指定自定义的PermissionEvaluator和SecurityExpressionHandler。
下面是示例代码:
CustomWebSecurityExpressionHandler.java
```
public class CustomWebSecurityExpressionHandler extends DefaultWebSecurityExpressionHandler {
@Override
protected SecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, FilterInvocation fi) {
CustomSecurityExpressionRoot root = new CustomSecurityExpressionRoot(authentication);
root.setPermissionEvaluator(new MyPermissionEvaluator());
return root;
}
}
```
CustomSecurityExpressionRoot.java
```
public class CustomSecurityExpressionRoot extends SecurityExpressionRoot {
public CustomSecurityExpressionRoot(Authentication authentication) {
super(authentication);
}
public boolean hasPermission(Object target, Object permission) {
return getPermissionEvaluator().hasPermission(authentication, target, permission);
}
}
```
applicationContext.xml
```
<sec:global-method-security pre-post-annotations="enabled" proxy-target-class="true" >
<sec:expression-handler ref="expressionHandler"/>
</sec:global-method-security>
<bean id="expressionHandler" class="com.example.CustomWebSecurityExpressionHandler"/>
```
这样配置后,在XML中使用如下代码即可直接调用MyPermissionEvaluator中的hasPermission方法:
```
<security:authorize access="hasPermission( 'formDialog', 'edit')">
...
</security:authorize>
```
阅读全文