编程式安全策略配置

本章定义的注解和API提供用于配置 Servlet 容器强制的安全约束。

@ServletSecurity 注解

@ServletSecurity 提供了用于定义访问控制约束的另一种机制,相当于那些通过在便携式部署描述符中声明式或通过 ServletRegistration 接口的 setServletSecurity 方法编程式表示。Servlet 容器必须支持在实现 javax.servlet.Servlet 接口的类(和它的子类)上使用@ServletSecurity 注解。

package javax.servlet.annotation;

@Inherited

@Documented

@Target(value=TYPE)

@Retention(value=RUNTIME)

public @interface ServletSecurity {

HttpConstraint value();

HttpMethodConstraint[] httpMethodConstraints();

}

TABLE 13-1 The ServletSecurity Interface

元素

描述

默认

value

HttpConstraint 定义了应用到没有在httpMethodConstraints 返回的数组中表示的所有HTTP方法的保护。

@HttpConstraint

httpMethodConstraints

HTTP方法的特定限制数组

{}

@HttpConstraint

@HttpConstraint 注解用在 @ServletSecurity 中表示应用到所有 HTTP 协议方法的安全约束,且 HTTP 协议方法对应的@HttpMethodConstraint 没有出现在 @ServletSecurity 注解中。

对于一个 @HttpConstraint 返回所有默认值发生在与至少一个@HttpMethodConstraint 返回不同于所有默认值的组合的特殊情况,@HttpMethodConstraint 表示没有安全约束被应用到任何 HTTP 协议方法,否则一个安全约束将应用。这个例外是确保这些潜在的非特定@HttpConstraint 使用没有产生约束,这将明确建立不受保护的访问这些方法;因为,它们没有被约束覆盖。

package javax.servlet.annotation;

@Documented

@Retention(value=RUNTIME)

public @interface HttpConstraint {

ServletSecurity.EmptyRoleSemantic value();

java.lang.String[] rolesAllowed();

ServletSecurity.TransportGuarantee transportGuarantee();

}

元素

描述

默认

value

当rolesAllowed返回一个空数组,(只)应用的默认授权语义。

PERMIT

rolesAllowed

包含授权角色的数组

{}

transportGuarantee

在连接的请求到达时必须满足的数据保护需求。

NONE

@HttpMethodConstraint

@HttpMethodConstraint 注解用在 @ServletSecurity 注解中表示在特定 HTTP 协议消息上的安全约束。

package javax.servlet.annotation;

@Documented

@Retention(value=RUNTIME)

public @interface HttpMethodConstraint {

ServletSecurity.EmptyRoleSemantic value();

java.lang.String[] rolesAllowed();

ServletSecurity.TransportGuarantee transportGuarantee();

}

TABLE 13-3 The HttpMethodConstraint Interface

元素

描述

默认

value

HTTP协议方法名

emptyRoleSemantic

当rolesAllowed返回一个空数组,(只)应用的默认授权语义。

PERMIT

rolesAllowed

包含授权角色的数组

{}

transportGuarantee

在连接的请求到达时必须满足的数据保护需求。

NONE

@ServletSecurity 注解可以指定在(更准确地说,目标是) Servlet 实现类上,且根据 @Inherited 元注解定义的规则,它的值是被子类继承的。至多只有一个 @ServletSecurity 注解实例可以出现在 Servlet 实现类上,且 @ServletSecurity 注解必须不指定在(更准确地说,目标是) Java 方法上。

当一个或多个 @HttpMethodConstraint 注解定义在 @ServletSecurity注解中时,每一个 @HttpMethodConstraint 定义的 security-constraint,其应用到 @HttpMethodConstraint 中标识的 HTTP 协议方法。除了它的 @HttpConstraint 返回所有默认值、和它包含至少一个返回不同于所有默认值的 @HttpMethodConstraint 的情况之外,@ServletSecurity 注解定义另一个 security-constraint 应该到所有还没有定义相关 @HttpMethodConstraint 的 HTTP 协议方法。

定义在便携式部署描述符中的 security-constraint 元素用于对所有出现在该约束中的 url-pattern 授权。

当在便携式部署描述符中的一个 security-constraint 包含一个 url-pattern,其精确匹配一个使用@ServletSecurity注解的模式映射到的类,该注解必须在由容器在该模式上强制实施的约束上没有效果。

当为便携式部署描述符定义了 metadata-complete=true 时,@ServletSecurity 注解不会应用到部署描述符中的任何 url-pattern 映射到(任何servlet映射到)的注解类。

@ServletSecurity 注解不应用到 ServletRegistration 使用ServletContext 接口的 addServlet(String, Servlet) 方法创建的url-pattern,除非该 Servlet 是由 ServletContext 接口的createServlet 方法构建的。

除了上面列出的,当一个 Servlet 类注解了 @ServletSecurity,该注解定义的安全约束应用到所有 url-pattern 映射到的所有 Servlet 映射到的类。

当一个类没有加 @ServletSecurity 注解时,应用到从那个类映射到的Servlet 的访问策略是由合适的 security-constraint 元素确定的,如果有,在相关的便携式部署描述符,或者由约束禁止任何这样的标签,则如果有,为目标 servlet 通过 ServletRegistration 接口的setServletSecurity 方法编程式确定的。

示例

以下示例演示了 ServletSecurity 注解的使用。

CODE EXAMPLE 13-1 for all HTTP methods, no constraints

@ServletSecurity

public class Example1 extends HttpServlet {

}

CODE EXAMPLE 13-2 for all HTTP methods, no auth-constraint, confidential transport required

@ServletSecurity(@HttpConstraint(transportGuarantee =

TransportGuarantee.CONFIDENTIAL))

public class Example2 extends HttpServlet {

}

CODE EXAMPLE 13-3 for all HTTP methods, all access denied

@ServletSecurity(@HttpConstraint(EmptyRoleSemantic.DENY))

public class Example3 extends HttpServlet {

}

CODE EXAMPLE 13-4 for all HTTP methods, auth-constraint requiring membership in Role R1

@ServletSecurity(@HttpConstraint(rolesAllowed = "R1"))

public class Example4 extends HttpServlet {

}

CODE EXAMPLE 13-5 for All HTTP methods except GET and POST, no constraints; forethods GET and POST, auth-constraint requiring membership in Role R1; for POST, confidential transport required

@ServletSecurity((httpMethodConstraints = {

@HttpMethodConstraint(value = "GET", rolesAllowed = "R1"),

@HttpMethodConstraint(value = "POST", rolesAllowed = "R1",

transportGuarantee = TransportGuarantee.CONFIDENTIAL)

})

public class Example5 extends HttpServlet {

}

CODE EXAMPLE 13-6 for all HTTP methods except GET auth-constraint requiring membership in Role R1; for GET, no constraints

@ServletSecurity(value = @HttpConstraint(rolesAllowed = "R1"),

httpMethodConstraints = @HttpMethodConstraint("GET"))

public class Example6 extends HttpServlet {

}

CODE EXAMPLE 13-7 for all HTTP methods except TRACE, auth-constraint requiring membership in Role R1; for TRACE, all access denied

@ServletSecurity(value = @HttpConstraint(rolesAllowed = "R1"),

httpMethodConstraints = @HttpMethodConstraint(value="TRACE",

emptyRoleSemantic = EmptyRoleSemantic.DENY))

public class Example7 extends HttpServlet {

}

映射 @ServletSecurity 为 security-constraint

本节将介绍 @ServletSecurity 注解映射为它等价表示,security-constraint元素。这提供了使用已存在容器的 security-constraint 实施机制来简化实施。由 Servlet 容器实施的 @ServletSecurity 注解必须在实施的效果上是等价的,由容器从在本节中定义的映射产生security-constraint 元素。

@ServletSecurity 注解用于定义一个方法无关的 @HttpConstraint,且紧跟着一个包含零个或多个 @HttpMethodConstraint 规格的列表。方法无关的约束应用到那些没有定义 HTTP 特定方法约束的所有 HTTP 方法。

当没有包含 @HttpMethodConstraint 元素,@ServletSecurity 注解相当于包含一个 web-resource-collection 的单个 security-constraint 元素,且 web-resource-collection 不包含 http-method 元素,因此涉及到所有 HTTP 方法。

下面的例子展示了把一个不包含 @HttpMethodConstraint 注解的@ServletSecurity 注解表示为单个 security-constraint 元素。相关的 servlet(registration)定义的 url-pattern 元素将被包含在web-resource-collection 中, 任何包含的 auth-constraint 和 user-data-constraint 元素的存在和值,将由定义在13.4.1.3节的“映射 @HttpConstraint 和 @HttpMethodConstraint 为XML”的映射的@HttpConstraint 的值确定。

CODE EXAMPLE 13-8 mapping @ServletSecurity with no contained

@HttpMethodConstraint

@ServletSecurity(@HttpConstraint(rolesAllowed = "Role1"))

...

Role1

当指定了一个或多个 @HttpMethodConstraint 元素,方法无关的约束关联一个单个 security-constraint 元素,其,web-resource-collection 包含了为每一个 HTTP 方法命名在 @HttpMethodConstraint 元素中的 http-method-omission 元素。如果方法无关的约束返回所有默认值和至少一个 @HttpMethodConstraint 不是,包含 http-method-omission 元素的 security-constraint 必须不被创建。每一个 @HttpMethodConstraint 与另一种包含一个 web-resource-collection 的 security-constraint 关联,web-resource-collection 包含一个使用相应 HTTP 方法命名的 http-method 元素。

下面的例子展示了映射带有单个 @HttpMethodConstraint 的@ServletSecurity 注解为两种 security-constraint 元素。相应的Servlet(registration)定义的 url-pattern 元素将被包含在两种约束的 web-resource-collection 中,且任何包含的 auth-constraint 和 user-data-constraint 元素的存在和值,将由定义在13.4.1.3节的“映射 @HttpConstraint 和 @HttpMethodConstraint 为XML”的映射关联的 @HttpConstraint 和 @HttpMethodConstraint 的值确定。

CODE EXAMPLE 13-9 mapping @ServletSecurity with contained @HttpMethodConstraint

@ServletSecurity(value=@HttpConstraint(rolesAllowed = "Role1"),

httpMethodConstraints = @HttpMethodConstraint(value = "TRACE",

emptyRoleSemantic = EmptyRoleSemantic.DENY))

...

TRACE

Role1

...

TRACE

映射 @HttpConstraint 和 @HttpMethodConstraint 为 XML

本节将介绍映射映射 @HttpConstraint 和 @HttpMethodConstraint 注解值(在 @ServletSecurity 中定义使用的)为它们等价的 auth-constraint 和 user-data-constraint 表示,这些注解共用一个通用模型用于表示用在便携式部署描述符中的 auth-constraint 和 user-data-constraint 元素的等价形式。该模型包括以下3种元素:

emptyRoleSemantic

授权语义,PERMIT或DENY,适用于在rolesAllowed中没有指定的角色时。此元素的默认值为PERMIT,且DENY不支持与非空的rolesAllowed列表结合使用。

rolesAllowed

一个包含授权角色的名字列表。当该列表为空时,其含义取决于emptyRoleSemantic的值。当角色名字“*”包含在允许的角色列表中时是没有特别的含义的。当特殊的角色名字“**”出现在rolesAllowed中时,它表示用户认证,不受约束的角色,是必需的和足够的。该元素的默认值是一个空列表。

transportGuarantee

数据保护需求,NONE 或 CONFIDENTIAL,在连接的请求到达时必须满足。该元素与一个包含一个使用相应值的transport-guarantee的user-data-constraint是等价的。该元素的默认值是NONE。

下面的例子展示了上述的 @HttpConstraint 模型和 web.xml 中的 auth-constraint 和 user-data-constraint 元素之间的对应关系。

CODE EXAMPLE 13-10 emptyRoleSemantic=PERMIT, rolesAllowed={}, transportGuarantee=NONE

没有 constraint

CODE EXAMPLE 13-11 emptyRoleSemantic=PERMIT, rolesAllowed={}, transportGuarantee=CONFIDENTIAL

CONFIDENTIAL

CODE EXAMPLE 13-12 emptyRoleSemantic=PERMIT, rolesAllowed={Role1},transportGuarantee=NONE

Role1

CODE EXAMPLE 13-13 emptyRoleSemantic=PERMIT, rolesAllowed={Role1},transportGuarantee=CONFIDENTIAL

Role1

CONFIDENTIAL

CODE EXAMPLE 13-14 emptyRoleSemantic=DENY, rolesAllowed={}, transportGuarantee=NONE

CODE EXAMPLE 13-15 emptyRoleSemantic=DENY, rolesAllowed={}, transportGuarantee=CONFIDENTIAL

CONFIDENTIAL

ServletRegistration.Dynamic 的 setServletSecurity

ServletContextListener 内的 setServletSecurity 方法用于定义应用到 ServletRegistration 定义的映射的安全约束。

Collection setServletSecurity(ServletSecurityElement arg);

setServletSecurity 的 javax.servlet.ServletSecurityElement 参数与 ServletSecurity 接口的 @ServletSecurity 注解在结构和模型上是类似的。因此,定义在13.4.1.2节的“映射@ServletSecurity为security-constraint”的映射,应用类似的包含HttpConstraintElement 和 HttpMethodConstraintElement 值的ServletSecurityElement 映射为其等价的 security-constraint 表示。

setServletSecurity 方法返回一组 URL pattern(可能空),其已是便携式部署描述符中的 security-constraint 元素的精确目标(因此,调用是不影响的)。

如果 ServletContext 中得到的 ServletRegistration 已经被初始化了,该方法抛出 IllegalStateException。

当便携式部署描述符中的 security-constraint 包含一个 url-pattern 其精确匹配 ServletRegistration 映射的 pattern,调用ServletRegistration 的 setServletSecurity 必须对 Servlet 容器对 pattern 实施的约束没有任何影响。

除了上面列出的,包括当 Servlet 类注解了 @ServletSecurity,当调用了 ServletRegistration 的 setServletSecurity,它制定应用到registration 的 url-pattern 的安全约束。

java 安全策略,编程式安全策略配置相关推荐

  1. Spring4.0编程式定时任务配置

    看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...

  2. java编程式事务_Spring编程式和声明式事务实例讲解

    Spring事务管理 Spring支持两种方式的事务管理: 编程式事务管理: 通过Transaction Template手动管理事务,实际应用中很少使用, 使用XML配置声明式事务: 推荐使用(代码 ...

  3. Vue.js-Day05【安装路由(vue-router)、如何使用vue-router、404配置、激活class、动态路由、编程式导航、路由嵌套、路由元信息、导航拦截】

    Vue.js实训[基础理论(5天)+项目实战(5天)]博客汇总表[详细笔记] 目   录 1.单页面应用 1.1.多页面应用 1.2.单页面应用 1.3.vue-router 2.安装vue-rout ...

  4. Spring→事务、隔离级别、事务传播行为、编程式事务控制、XML配置声明式事务(原始方式)、XML配置声明式事务(基于tx/aop)、@注解配置声明式事务、优势总结

    事务 Spring事务管理 不考虑隔离引发问题 隔离级别 事务传播行为 演示环境搭建 编程式事务控制 XML配置声明式事务(原始方式) XML配置声明式事务(基于tx/aop) @注解配置声明式事务 ...

  5. linux 禁止app权限,SELinux文件访问安全策略和app权限配置

    SELinux文件访问安全策略和app权限配置 Android开发 基于android6.0版本的SELinux文件访问安全策略 在android6.0以后的版本,google采用了SELinux的文 ...

  6. Spring编程式和声明式事务实例讲解

    历史回顾: 可能是最漂亮的Spring事务管理详解 Spring事务管理 Spring支持两种方式的事务管理: 编程式事务管理: 通过Transaction Template手动管理事务,实际应用中很 ...

  7. 编程式事务与声明式事务

    编程式事务 1.加入jar包 com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar co ...

  8. 全面分析 Spring 的编程式事务管理及声明式事务管理(转)

    摘要 Spring 的事务管理是 Spring 框架中一个比较重要的知识点,该知识点本身并不复杂,只是由于其比较灵活,导致初学者很难把握.本教程从基础知识开始,详细分析了 Spring 事务管理的使用 ...

  9. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

最新文章

  1. 云时代架构阅读笔记二——一次CPU负载超高的分析
  2. java填空题答案_JAVA求填空题答案
  3. Servlet_执行原理
  4. Linux线程(二)
  5. 2.x最终照着教程,成功使用OpenGL ES 绘制纹理贴图,添加了灰度图
  6. SFML 与 VS2015 的安装配置
  7. LinkedBlockingQueue1.8源码
  8. 程序员的大恩人永远地离开了
  9. HALCON 21.11:深度学习笔记---分类(10)
  10. display:inline-block间隙产生的原因以及解决方案
  11. 局域网的传输介质、网线水晶头制作图解教程
  12. Impala 的特点
  13. 如何查看CSDN积分
  14. 移动端产品比较分析:APP、小程序、H5
  15. 一个20岁工作了4年男网管真情自白书
  16. 记一次用Python统计全国女性Size
  17. 抖音SEO,抖音seo优化
  18. 几个在线文档接口生成工具
  19. php实现展现量cookie,[转载]展现量、点击量、点击率;访客数、访问次数、浏览量的区别与作用...
  20. Ubuntu 配置WebDav服务器

热门文章

  1. seafile私有网盘搭建
  2. SpringBoot+Redis 搞定搜索栏热搜、不雅文字过滤功能
  3. 面试必会之ArrayList源码分析手写ArrayList
  4. Windows 7系统快捷键汇总
  5. AKKA Router路由
  6. 【bzoj4518】[Sdoi2016]征途 斜率优化dp
  7. ScrollView各属性,及代理方法汇总
  8. 解决outlook不能显示鼠标问题
  9. 35款非常有创意的透明名片设计作品
  10. 一个CSharp类代码,让你的窗体显示的更酷(转)