声明 Taglib

要使用任何标签,必须在 JSP 中声明安全标签库:Spring中文文档

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

authorize 标签

此标记用于确定是否应评估其内容。 在 Spring Security 3.0 中,它可以通过两种方式使用。Spring中文文档

Spring Security 2.0 中的旧选项也受支持,但不鼓励使用。Spring中文文档

第一种方法使用 Web 安全表达式,该表达式在标记的属性中指定。 表达式计算被委托给应用程序上下文中定义的表达式(应在命名空间配置中启用 Web 表达式,以确保此服务可用)。 因此,例如,您可能有:accessSecurityExpressionHandler<FilterInvocation><http>Spring中文文档

<sec:authorize access="hasRole('supervisor')">

This content will only be visible to users who have the "supervisor" authority in their list of <tt>GrantedAuthority</tt>s.

</sec:authorize>

当与 Spring Security 结合使用时,该标签还可用于检查权限:PermissionEvaluatorSpring中文文档

<sec:authorize access="hasPermission(#domain,'read') or hasPermission(#domain,'write')">

This content will only be visible to users who have read or write permission to the Object found as a request attribute named "domain".

</sec:authorize>

一个常见的要求是只显示一个特定的链接,假设用户实际上被允许点击它。 我们如何提前确定某事是否被允许?此代码还可以在替代模式下运行,允许您将特定网址定义为属性。 如果允许用户调用该 URL,则会评估标记正文。否则,将跳过它。 所以你可能会有这样的情况:Spring中文文档

<sec:authorize url="/admin">

This content will only be visible to users who are authorized to send requests to the "/admin" URL.

</sec:authorize>

若要使用此标记,还必须在应用程序上下文中具有 的实例。 如果使用的是命名空间,则会自动注册一个命名空间。 这是 的一个实例,它为提供的 URL 创建一个虚拟 Web 请求,并调用安全拦截器来查看请求是成功还是失败。 这样一来,您就可以委托给通过使用命名空间配置中的声明定义的访问控制设置,并省去了在 JSP 中复制信息(例如所需角色)的麻烦。 还可以将此方法与属性(提供 HTTP 方法,例如 )结合使用,以获得更具体的匹配。WebInvocationPrivilegeEvaluatorDefaultWebInvocationPrivilegeEvaluatorintercept-url<http>methodPOSTSpring中文文档

通过将属性设置为变量名称,可以将评估标记的布尔结果(无论是否授予或拒绝访问)存储在页面上下文范围变量中,从而避免在页面中的其他点复制和重新评估条件。varSpring中文文档

禁用测试的标签授权

在页面中为未经授权的用户隐藏链接不会阻止他们访问该 URL。 例如,他们可以直接将其输入到浏览器中。 作为测试过程的一部分,您可能希望显示隐藏区域,以检查链接是否确实在后端得到保护。 如果将 system 属性设置为 ,则标记仍会运行,但不会隐藏其内容。 默认情况下,它还使用标签将内容包围起来。 这使您可以显示具有特定 CSS 样式(例如不同的背景颜色)的“隐藏”内容。 例如,尝试在启用此属性的情况下运行“教程”示例应用程序。spring.security.disableUISecuritytrueauthorize<span class="securityHiddenUI">…​</span>Spring中文文档

如果要更改默认标签中的周围文本(或使用空字符串将其完全删除),也可以设置 and 属性。spring.security.securedUIPrefixspring.security.securedUISuffixspanSpring中文文档

Spring Security 2.0 中的旧选项也受支持,但不鼓励使用。Spring中文文档

身份验证标记

此标记允许访问存储在安全上下文中的当前对象。 它直接在 JSP 中呈现对象的属性。 因此,例如,如果 的属性是 Spring Security 对象的实例,则 using 会呈现当前用户的名称。AuthenticationprincipalAuthenticationUserDetails<sec:authentication property="principal.username" />Spring中文文档

当然,这种事情没有必要使用 JSP 标签,有些人更喜欢在视图中保留尽可能少的逻辑。 您可以访问 MVC 控制器中的对象(通过调用 ),并将数据直接添加到模型中,以便由视图呈现。AuthenticationSecurityContextHolder.getContext().getAuthentication()Spring中文文档

accesscontrollist 标记

此标记仅在与 Spring Security 的 ACL 模块一起使用时有效。 它检查指定域对象所需权限的逗号分隔列表。 如果当前用户具有所有这些权限,则会评估标记正文。 如果他们不这样做,则跳过它。Spring中文文档

通常,此标记应被视为已弃用。 请改用 The authorize 标记Spring中文文档

以下列表显示了一个示例:Spring中文文档

<sec:accesscontrollist hasPermission="1,2" domainObject="${someObject}">

<!-- This will be shown if the user has all of the permissions represented by the values "1" or "2" on the given object. -->

</sec:accesscontrollist>

权限将传递给应用程序上下文中定义的权限,并将其转换为 ACL 实例,因此它们可以是工厂支持的任何格式。它们不必是整数。它们可以是字符串,例如 或 。 如果未找到,则使用 的实例。 from 应用程序上下文用于加载所提供对象的实例。 使用所需的权限调用 以检查是否授予了所有这些权限。PermissionFactoryPermissionREADWRITEPermissionFactoryDefaultPermissionFactoryAclServiceAclAclSpring中文文档

此标记也支持该属性,其方式与标记相同。varauthorizeSpring中文文档

通常,此标记应被视为已弃用。 请改用 The authorize 标记Spring中文文档

csrfInput 标记

如果启用了 CSRF 保护,则此标记会插入一个隐藏的表单字段,其中包含 CSRF 保护令牌的正确名称和值。 如果未启用 CSRF 保护,则此标记不会输出任何内容。Spring中文文档

通常,Spring Security 会自动为您使用的任何标签插入 CSRF 表单字段,但如果由于某种原因您无法使用 ,则是一个方便的替代品。<form:form><form:form>csrfInputSpring中文文档

您应该将此标记放置在 HTML 块中,您通常会在该块中放置其他输入字段。 不要将此标签放置在 Spring 块中。 Spring Security 自动处理 Spring 表单。 以下列表显示了一个示例:<form></form><form:form></form:form>Spring中文文档

	<form method="post" action="/do/something">
		<sec:csrfInput />
		Name:<br />
		<input type="text" name="name" />
		...
	</form>

csrfMetaTags 标记

如果启用了 CSRF 保护,则此标记将插入包含 CSRF 保护令牌表单字段和标头名称以及 CSRF 保护令牌值的元标记。 这些元标记对于在应用程序的 JavaScript 中使用 CSRF 保护非常有用。Spring中文文档

您应该放置在 HTML 块中,通常放置其他元标记。 使用此标记后,您可以使用 JavaScript 访问表单字段名称、标题名称和令牌值。 此示例中使用 JQuery 来简化任务。 以下列表显示了一个示例:csrfMetaTags<head></head>Spring中文文档

<!DOCTYPE html>
<html>
	<head>
		<title>CSRF Protected JavaScript Page</title>
		<meta name="description" content="This is the description for this page" />
		<sec:csrfMetaTags />
		<script type="text/javascript" language="javascript">

			var csrfParameter = $("meta[name='_csrf_parameter']").attr("content");
			var csrfHeader = $("meta[name='_csrf_header']").attr("content");
			var csrfToken = $("meta[name='_csrf']").attr("content");

			// using XMLHttpRequest directly to send an x-www-form-urlencoded request
			var ajax = new XMLHttpRequest();
			ajax.open("POST", "https://www.example.org/do/something", true);
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded data");
			ajax.send(csrfParameter + "=" + csrfToken + "&name=John&...");

			// using XMLHttpRequest directly to send a non-x-www-form-urlencoded request
			var ajax = new XMLHttpRequest();
			ajax.open("POST", "https://www.example.org/do/something", true);
			ajax.setRequestHeader(csrfHeader, csrfToken);
			ajax.send("...");

			// using JQuery to send an x-www-form-urlencoded request
			var data = {};
			data[csrfParameter] = csrfToken;
			data["name"] = "John";
			...
			$.ajax({
				url: "https://www.example.org/do/something",
				type: "POST",
				data: data,
				...
			});

			// using JQuery to send a non-x-www-form-urlencoded request
			var headers = {};
			headers[csrfHeader] = csrfToken;
			$.ajax({
				url: "https://www.example.org/do/something",
				type: "POST",
				headers: headers,
				...
			});

		<script>
	</head>
	<body>
		...
	</body>
</html>

如果未启用 CSRF 保护,则不输出任何内容。csrfMetaTagsSpring中文文档