没有合适的资源?快使用搜索试试~ 我知道了~
基于JAAS的用户验证和控制模型的一般设计.doc
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 145 浏览量
2023-06-20
15:35:59
上传
评论
收藏 175KB DOC 举报
温馨提示


试读
27页
基于JAAS的用户验证和控制模型的一般设计.doc
资源推荐
资源详情
资源评论












基于 JAAS 的用户验证和控制模型的一般设计
客户端通过 a LoginContext 对象与 JAAS 交互, 该对象提供一个方法来开发与下层认证
无关的应用. LoginContext 类,是 javax.security.auth.login 包的一部分, 描述用
于认证对象的方法. A subject 是在一个系统内的你想认证和分配访问权限的对象的标识。A
subject 可以是一个用户、或一个机器并用 javax.security.auth.Subject 类表示.
因为一个实体(subject)也许回与多个授权交互 authorities (一个口令用于在线银行而另一
个用于电子邮件系统), a java.security.Principal is used to represent the identity in
those interactions. In other words, the Principal interface is an abstract notion that can
be used to represent an entity, a company, or a login ID.一个 Subject 可以包含多个
Principles.
LoginContext 对象调用 LoginModules that 负责完成认证. LoginModule 接口 , 是
javax.security.auth.spi 包的一部分, must be implemented by authentication
technology providers and can be specified by applications to provide a specific type of
authentication. The LoginContext 读取 Configuration 并 instantiates the specified
LoginModules.
一个配置文件用来指定认证技术, or LoginModule, to be used with a particular
application. 这样一来, 可以在一个应用中挂接多个不同的 LoginModule 而不用对应用代
码作任何修改.
Code Sample 1 是一个 JAAS 客户的例子. I have used the LoginContext that will invoke
the LoginModules specified in the configuration to 完成认证, 并使用 the LoginContext
with a name "WeatherLogin"进行初始化, and callback handler "MyCallbackHandler"
whose implementation is shown in Code Sample 2. The name will be used as the index in
the Configuration file to determine which LoginModule should be used. 当你看到配置文
件的时候这会变得更清晰. The callback handler is passed to the underlying LoginModule
so that they can communicate and interact with the user to prompt for a
username/password, for example, through a textual or graphical user interface. Once the
LoginContext has been initialized, the login method is called to login.
Code Sample 1: MyClient.java
import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
public class MyClient {
public static void main(String argv[]) {
LoginContext ctx = null;
try {
ctx = new LoginContext("WeatherLogin", new
MyCallbackHandler());

} catch(LoginException le) {
System.err.println("LoginContext cannot be created. "+
le.getMessage());
System.exit(-1);
} catch(SecurityException se) {
System.err.println("LoginContext cannot be created. "+
se.getMessage());
}
try {
ctx.login();
} catch(LoginException le) {
System.out.println("Authentication failed. " + le.getMessage());
System.exit(-1);
}
System.out.println("Authentication succeeded.");
System.exit(-1);
}
}
一个基于 JAAS 的应用实现 CallbackHandler 接口因此它可以与用户交互以进入指定的
认证数据, 比如用户名和密码, 或显示错误和警告消息. The underlying security service may
request different types of information via passing individual callbacks to the callback
handler. Based on the callbacks passed, the callback handler decides how to retrieve and
display information. For example, if the underlying service needs a username and a
password to authenticate a user, it uses a NameCallback and PasswordCallback.
Other callbacks, which are part of the javax.security.auth.callback class,
include:
▪ ChoiceCallback (显示一系列选项)
▪ ConfirmationCallback (ask for YES/NO, OK/CANCEL)
▪ LanguageCallback (the Locale used for localizing text
▪ TextInputCallback (retrieve generic text information)
▪ TextOutputCallback (display information, warning, and error messages)
实现 CallbackHandler 接口意味着 that you need to provide implementation to the
handle method to 取回或显示 the information requested in the provided callbacks. 一个范
例实现如例 2 所示. Note that here I am using the NameCallback to interact with the
user.
Code Sample 2: MyCallbackHandler.java
import java.io.*;

import javax.security.auth.*;
import javax.security.auth.callback.*;
public class MyCallbackHandler implements CallbackHandler {
public void handle(Callback callbacks[]) throws IOException,
UnsupportedCallbackException {
for(int i=0;i<callbacks.length;i++) {
if(callbacks[i] instanceof NameCallback) {
NameCallback nc = (NameCallback) callbacks[0];
System.err.print(nc.getPrompt());
System.err.flush();
String name = (new BufferedReader(new
InputStreamReader(System.in))).readLine();
nc.setName(name);
} else {
throw(new UnsupportedCallbackException(callbacks[i],
"Callback handler not support"));
}
}
}
}
现在,让我们研究一个 LoginModule 实现的样例. 注意到事实上应用程序开发者不需要自
己实现 LoginModules; they can use login modules and plug them into their applications.
For example, Sun Microsystems ships several LoginModules including:
JndiLoginModule, KeyStoreLoginModule, Krb5LoginModule, NTLoginModule,
UNIXLoginModule. If you'd like to learn how to use any of these login modules, please
refer to the documentation in the For More Information section at the end of this article.
这个例子非常简单,它仅识别一个 authentication string 和一个 Principal "SunnyDay",
both of which are hard-coded. To login, the system displays "What is the weather like
today?", if the answer is "Sunny", the user is logged in. Note how the
MyCallbackHandler is being used in the implementation of the login method. In
addition to the login method, you must provide implementation for four methods:
initialize, commit, abort, and logout. These methods will be used by the
LoginContext in the following order:
▪ initialize: The purpose of this method is to initialize this LoginModule with
the relevant information. The Subject passed in this method is used to store the
Principals and Credentials if login succeeds. Note that this method takes a
CallbackHanlder that can be used for entering authentication information. In
this example, I do not use the CallbackHandler. The CallbackHandler is
useful as it decouples the services provider from the specific input device being

used.
▪ login: Asks the LoginModule to authenticate the Subject. Note that the
Principal has not been assigned yet.
▪ commit: This method is called if the LoginContext's overall authentication
succeeded.
▪ abort: Informs the LoginModule that some other providers or modules have
failed to authenticate the subject. The whole login should fail.
▪ logout: Logout the Subject by removing the Principals and Credentials
from the Subject.
(Note that some of the lines shown in this and other code examples in this article have
been split for formatting purposes)
Code Sample 3: WeatherLoginModule.java
import java.io.*;
import java.util.*;
import java.security.Principal;
import javax.security.auth.Subject;
import javax.security.auth.callback.*;
import javax.security.auth.spi.LoginModule;
import javax.security.auth.login.LoginException;
public class WeatherLoginModule implements LoginModule {
private Subject subject;
private ExamplePrincipal entity;
private CallbackHandler callbackhandler;
private static final int NOT = 0;
private static final int OK = 1;
private static final int COMMIT = 2;
private int status;
public void initialize(Subject subject, CallbackHandler//
callbackhandler, Map state, Map options) {
status = NOT;
entity = null;
this.subject = subject;
this.callbackhandler = callbackhandler;
}
public boolean login() throws LoginException {
if(callbackhandler == null) {
throw new LoginException("No callback handler is available");

}
Callback callbacks[] = new Callback[1];
callbacks[0] = new NameCallback("What is the weather like today?");
String name = null;
try {
callbackhandler.handle(callbacks);
name = ((NameCallback)callbacks[0]).getName();
} catch(java.io.IOException ioe) {
throw new LoginException(ioe.toString());
} catch(UnsupportedCallbackException ce) {
throw new LoginException("Error:
"+ce.getCallback().toString());
}
if(name.equals("Sunny")) {
entity = new ExamplePrincipal("SunnyDay");
status = OK;
return true;
} else {
return false;
}
}
public boolean commit() throws LoginException {
if(status == NOT) {
return false;
}
if(subject == null) {
return false;
}
Set entities = subject.getPrincipals();
if(!entities.contains(entity)) {
entities.add(entity);
}
status = COMMIT;
return true;
}
public boolean abort() throws LoginException {
if((subject != null) && (entity != null)) {
Set entities = subject.getPrincipals();
if(entities.contains(entity)) {
entities.remove(entity);
}
剩余26页未读,继续阅读
资源评论


oligaga
- 粉丝: 37
- 资源: 2万+

下载权益

C知道特权

VIP文章

课程特权

开通VIP
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
