📄 sampleclient.java
字号:
/*
* SampleClient.java
*
* Jaas 客户端
*
*/
//声明本类所在的包
package examples.security.jaas;
//本类引入的包和类
import java.io.*;
import java.util.*;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.TextOutputCallback;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import javax.security.auth.login.FailedLoginException;
import javax.security.auth.login.AccountExpiredException;
import javax.security.auth.login.CredentialExpiredException;
/**
* JAAS 用户认证的客户端示例程序
*/
public class SampleClient
{
/**
* 进行用户认证
*/
public static void main(String[] args)
{
if (args.length != 1) {
System.out.println("用法: java examples.security.jaas.SampleClient URL");
System.out.println("例如: java examples.security.jaas.SampleClient t3://localhost:7001");
System.exit(0);
}
LoginContext loginContext = null;
/**
* 设置JAAS服务器url系统属性和,创建LoginContext
*/
try
{
// 设置服务器 url
Properties property = new Properties(System.getProperties());
property.put("weblogic.security.jaas.ServerURL", args[0]);
System.setProperties(property);
// 设置配置类
property = new Properties(System.getProperties());
property.put("weblogic.security.jaas.Configuration", "examples.security.jaas.SampleConfig");
System.setProperties(property);
// 设置配置文件名,加载配置策略文件
property = new Properties(System.getProperties());
property.put("weblogic.security.jaas.Policy", "Sample.policy");
System.setProperties(property);
// 创建 LoginContext
loginContext = new LoginContext("SampleLoginModule", new MyCallbackHandler());
}
catch(SecurityException se)
{
se.printStackTrace();
System.exit(-1);
}
catch(LoginException le)
{
le.printStackTrace();
System.exit(-1);
}
/**
* 认证
*/
try
{
// 如果没有异常,正常返回,则认证成功
loginContext.login();
}
catch(FailedLoginException fle)
{
System.out.println("认证失败, " + fle.getMessage());
System.exit(-1);
}
catch(AccountExpiredException aee)
{
System.out.println("认证失败: 帐号过期");
System.exit(-1);
}
catch(CredentialExpiredException cee)
{
System.out.println("认证失败: 信用过期");
System.exit(-1);
}
catch(Exception e)
{
System.out.println("认证失败: 不可预料的异常, " + e.getMessage());
e.printStackTrace();
System.exit(-1);
}
/**
* 重新获取认证主题,执行SampleAction
*/
Subject subject = loginContext.getSubject();
SampleAction sampleAction = new SampleAction();
Subject.doAs(subject, sampleAction);
System.exit(0);
}
}
/**
* 实现 CallbackHandler 接口
*/
class MyCallbackHandler implements CallbackHandler
{
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
{
for(int i = 0; i < callbacks.length; i++)
{
if(callbacks[i] instanceof TextOutputCallback)
{
// Display the message according to the specified type
TextOutputCallback toc = (TextOutputCallback) callbacks[i];
switch(toc.getMessageType())
{
case TextOutputCallback.INFORMATION:
System.out.println(toc.getMessage());
break;
case TextOutputCallback.ERROR:
System.out.println("ERROR: " + toc.getMessage());
break;
case TextOutputCallback.WARNING:
System.out.println("WARNING: " + toc.getMessage());
break;
default:
throw new IOException("Unsupported message type: " + toc.getMessageType());
}
}
else if(callbacks[i] instanceof NameCallback)
{
// 提示用户输入用户名
NameCallback nc = (NameCallback) callbacks[i];
System.err.print(nc.getPrompt());
System.err.flush();
nc.setName((new BufferedReader(new InputStreamReader(System.in))).readLine());
}
else if(callbacks[i] instanceof PasswordCallback)
{
// 提示用户输入密码
PasswordCallback pc = (PasswordCallback) callbacks[i];
System.err.print(pc.getPrompt());
System.err.flush();
// 注意: JAAS 指定密码是 char[] 而不是 String
String tmpPassword = (new BufferedReader(new InputStreamReader(System.in))).readLine();
int passLen = tmpPassword.length();
char[] password = new char[passLen];
for(int passIdx = 0; passIdx < passLen; passIdx++)
password[passIdx] = tmpPassword.charAt(passIdx);
pc.setPassword(password);
}
else
{
throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -