---
title: "Security Service--安全服务"
author: "leves"
date: "2009-03-24T21:18:17Z"
tags: ["Security", "配置管理"]
categories: ["企业架构"]
slug: "security-service"
draft: false
lastmod: "2009-03-24T21:19:46Z"
id: 354466
url: "https://leves.iteye.com/blog/354466"
---

大多数的安全模块是针对具体的应用的，显著的缺点就是，每次新的程序都需要重新开发安全模块。所以通用的安全组件服务就显的必须了。JAAX（java authentication  and authorization service，JAVA 验证授权服务）就为我们提供了这样的服务。

     JAAX 建立在可插入的认证模块（Pluggable Authentication Module，PAM）的安全体系结构之上，也就是说可以无缝集成到任何的系统中。

    JAAX的两个特性：验证（authentication） 和 授权(authorization) 。

   验证，也称为认证，EJB3.0 中验证需要两个参数,principal （本人）和 credentials（凭证） ，如在登录过程中，principal 是账户 ，credentials 是密码。

 

```
Properise props=new Propertise();
props.serProperty(Context.SECURITY_PRINCIPA,user);
props.serProperty(Context.SECURITY_CREDENTIALS,password);
```

 

登陆验证这是最常见的验证，当然验证的凭证不一定是密码，在实现上还可以是 密钥，指纹识别，红角膜等。这是PAM的体系结构的优点。

 

 

验证是授权的前提，不同的角色根据已定义的 user.properties 和 roles.properties 配置文件 进行不同权限的授权。

在ejb3.O 中 权限可以定位到 同个类中的不同方法。

```
@javax.annotation.security.RolesAllowed({AdminRole});
public String Admin()
{
   return "通过在方法上的@RolesAllowed 注释 ，标记只有AdminRole 才可以访问Admin()"；
}

@javax.annotation.security.PermitAll
public String AllAllowed()
{
   return "任何已验证的角色都可以访问此方法";
}
```