怎么让JSP 页面配置<security:authorize access="has('formDialog:edit')">有效,并且后台方法控制权限的地方也生效
时间: 2023-06-18 12:05:57 浏览: 169
要使JSP页面中的`<security:authorize>`标签有效,需要在Spring Security配置中启用Spring Security标记库的支持。在Spring Security配置文件中添加以下代码:
```
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
```
在JSP页面中使用`<security:authorize>`标签来限制页面元素的访问权限,例如:
```
<security:authorize access="hasRole('ROLE_USER')">
<span>Welcome, user!</span>
</security:authorize>
```
这将只允许具有ROLE_USER角色的用户看到欢迎消息。
要在后台方法中控制权限,可以使用Spring Security的注解来标记方法的访问权限。例如,要限制方法的访问权限只能由具有ROLE_ADMIN角色的用户调用:
```
@PreAuthorize("hasRole('ROLE_ADMIN')")
public void deleteOrder(Order order) {
// delete the order
}
```
这将确保只有具有ROLE_ADMIN角色的用户才能删除订单。
相关问题
怎么让JSP 页面配置<security:authorize access="has('formDialog:edit')">的自定义方法有效,并且后台方法控制权限的地方也生效
要让JSP页面配置的<security:authorize access="has('formDialog:edit')">的自定义方法有效,需要在Spring Security的配置文件中定义自定义的表达式处理器。可以通过实现`org.springframework.security.access.expression.method.MethodSecurityExpressionHandler`接口来实现自定义表达式处理器,并在配置文件中引用该处理器。
例如,定义一个名为`customMethodSecurityExpressionHandler`的自定义表达式处理器:
```
<bean id="customMethodSecurityExpressionHandler" class="com.example.CustomMethodSecurityExpressionHandler">
<property name="permissionService" ref="permissionService" />
</bean>
```
然后在Spring Security的配置文件中引用该处理器:
```
<security:global-method-security pre-post-annotations="enabled"
secured-annotations="enabled"
jsr250-annotations="enabled"
access-decision-manager-ref="accessDecisionManager"
authentication-manager-ref="authenticationManager"
method-security-expression-handler-ref="customMethodSecurityExpressionHandler" />
```
接下来,在自定义表达式处理器`CustomMethodSecurityExpressionHandler`中实现自定义的表达式方法,例如:
```
public class CustomMethodSecurityExpressionHandler extends DefaultMethodSecurityExpressionHandler {
private PermissionService permissionService;
public CustomMethodSecurityExpressionHandler() {
super();
}
@Override
protected MethodSecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, MethodInvocation invocation) {
CustomMethodSecurityExpressionRoot root = new CustomMethodSecurityExpressionRoot(authentication);
root.setPermissionService(permissionService);
root.setThis(invocation.getThis());
root.setTarget(invocation.getMethod().getDeclaringClass());
return root;
}
public void setPermissionService(PermissionService permissionService) {
this.permissionService = permissionService;
}
}
```
在`CustomMethodSecurityExpressionRoot`类中实现自定义的表达式方法,例如:
```
public class CustomMethodSecurityExpressionRoot extends MethodSecurityExpressionOperations {
private PermissionService permissionService;
public CustomMethodSecurityExpressionRoot(Authentication authentication) {
super(authentication);
}
public boolean hasPermission(String permission) {
return permissionService.hasPermission(permission);
}
public void setPermissionService(PermissionService permissionService) {
this.permissionService = permissionService;
}
}
```
现在,可以在JSP页面中使用自定义的表达式方法,例如:
```
<security:authorize access="hasPermission('formDialog:edit')">...</security:authorize>
```
同时,在后台方法中也可以使用相同的表达式,例如:
```
@PreAuthorize("hasPermission('formDialog:edit')")
public void editFormDialog() {
// ...
}
```
怎么让JSP 页面配置<security:authorize access="has('formDialog:edit')">,怎么配置access方法为自定义方法
在 JSP 页面中使用 `security:authorize` 标签需要配置 Spring Security 的支持,具体步骤如下:
1. 在 `web.xml` 中配置 Spring Security 的过滤器。
```
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
```
2. 在 Spring Security 的配置文件中配置相关的权限控制规则。
```
<http>
<intercept-url pattern="/edit/**" access="hasRole('ROLE_ADMIN')" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin" password="admin" authorities="ROLE_ADMIN" />
</user-service>
</authentication-provider>
</authentication-manager>
```
3. 在 JSP 页面中使用 `security:authorize` 标签进行权限控制。
```
<security:authorize access="hasRole('ROLE_ADMIN')">
<a href="/edit">Edit</a>
</security:authorize>
```
如果需要自定义方法作为 `access` 参数,可以在 Spring Security 的配置文件中创建一个自定义的表达式,例如:
```
<bean id="customExpressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
<property name="permissionEvaluator" ref="customPermissionEvaluator"/>
</bean>
<bean id="customPermissionEvaluator" class="com.example.CustomPermissionEvaluator"/>
<global-method-security pre-post-annotations="enabled" expression-handler-ref="customExpressionHandler"/>
```
其中 `com.example.CustomPermissionEvaluator` 是自定义的权限判断类,可以在该类中定义自己的权限判断方法。例如:
```
public class CustomPermissionEvaluator implements PermissionEvaluator {
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
if (targetDomainObject instanceof FormDialog && "edit".equals(permission)) {
return hasEditPermission(authentication, (FormDialog) targetDomainObject);
}
return false;
}
private boolean hasEditPermission(Authentication authentication, FormDialog formDialog) {
// 自定义权限判断逻辑
}
}
```
然后在 JSP 页面中使用自定义的权限判断方法:
```
<security:authorize access="@customPermissionEvaluator.hasPermission(authentication, formDialog, 'edit')">
<a href="#">Edit</a>
</security:authorize>
```
阅读全文