加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JAAS LOGIN IN WEBLOGIC SERVER--reference

发布时间:2020-12-14 06:18:28 所属栏目:Java 来源:网络整理
导读:The Java Authentication and Authorization Service (JAAS) is a standard extension to the security in the J2SE Development Kit 5.0. JAAS provides the ability to enforce access controls based on user identity. JAAS is provided in WebLogic Ser

The Java Authentication and Authorization Service (JAAS) is a standard extension to the security in the J2SE Development Kit 5.0. JAAS provides the ability to enforce access controls based on user identity. JAAS is provided in WebLogic Server as an alternative to the JNDI authentication mechanism.

WebLogic Server clients use the authentication portion of the standard JAAS only. The JAAS LoginContext provides support for the ordered execution of all configured authentication provider LoginModule instances and is responsible for the management of the completion status of each configured provider.

<span style="color: #0000ff;">import<span style="color: #000000;"> javax.security.auth.Subject;
<span style="color: #0000ff;">import javax.security.auth.callback.<span style="color: #000000;">;
<span style="color: #0000ff;">import javax.security.auth.login.
<span style="color: #000000;">;

<span style="color: #0000ff;">import<span style="color: #000000;"> weblogic.security.auth.callback.URLCallback;

<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> JAASLogin
{
<span style="color: #0000ff;">private <span style="color: #0000ff;">static <span style="color: #0000ff;">final <span style="color: #0000ff;">class<span style="color: #000000;"> ConfigInfo
{
<span style="color: #0000ff;">static String USERID =<span style="color: #000000;"> “weblogic”;
<span style="color: #0000ff;">static String PASSWORD =<span style="color: #000000;"> “weblogic”;
<span style="color: #0000ff;">static String URL = “t3:<span style="color: #008000;">//<span style="color: #008000;">10.10.71.79:7001?;
<span style="color: #0000ff;">static String JAAS_STRING =<span style="color: #000000;"> “other”;

}

<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args)
{

<span style="color: #0000ff;">try<span style="color: #000000;">{
System.out.println(“Server is at ” +<span style="color: #000000;"> ConfigInfo.URL);
System.out.println(“Userid: ” +<span style="color: #000000;"> ConfigInfo.USERID);
System.out.println(“Password: ” +<span style="color: #000000;"> ConfigInfo.PASSWORD);
LoginContext lc = <span style="color: #0000ff;">new LoginContext(ConfigInfo.JAAS_STRING,<span style="color: #0000ff;">new<span style="color: #000000;"> JAASLogin.CallbackHandler(ConfigInfo.USERID,ConfigInfo.PASSWORD,ConfigInfo.URL));
System.out.println(“LoginContext:: “);
lc.login();
System.out.println(“lc.login():: “);
Subject subject =<span style="color: #000000;"> lc.getSubject();
System.out.println(“Subject: ” +<span style="color: #000000;"> subject);
}
<span style="color: #0000ff;">catch<span style="color: #000000;">(AccountExpiredException ae){

ae.printStackTrace();
}
<span style="color: #0000ff;">catch<span style="color: #000000;">(CredentialExpiredException ce){

ce.printStackTrace();
}
<span style="color: #0000ff;">catch<span style="color: #000000;">(FailedLoginException fe){

fe.printStackTrace();
}
<span style="color: #0000ff;">catch<span style="color: #000000;">(LoginException le){

le.printStackTrace();
}

}

<span style="color: #0000ff;">private <span style="color: #0000ff;">static <span style="color: #0000ff;">final <span style="color: #0000ff;">class CallbackHandler <span style="color: #0000ff;">implements<span style="color: #000000;"> javax.security.auth.callback.CallbackHandler
{
<span style="color: #0000ff;">private<span style="color: #000000;"> String userid;
<span style="color: #0000ff;">private<span style="color: #000000;"> String password;
<span style="color: #0000ff;">private<span style="color: #000000;"> String url;

<span style="color: #0000ff;">public<span style="color: #000000;"> CallbackHandler(String userid,String password,String url)
{
<span style="color: #0000ff;">this.userid =<span style="color: #000000;"> userid;
<span style="color: #0000ff;">this.password =<span style="color: #000000;"> password;
<span style="color: #0000ff;">this.url =<span style="color: #000000;"> url;
}
<span style="color: #0000ff;">public <span style="color: #0000ff;">void handle(Callback[] callbacks) <span style="color: #0000ff;">throws<span style="color: #000000;"> UnsupportedCallbackException
{
<span style="color: #0000ff;">for (<span style="color: #0000ff;">int i = 0; i < callbacks.length; i++<span style="color: #000000;">)
{
<span style="color: #0000ff;">if (callbacks[i] <span style="color: #0000ff;">instanceof<span style="color: #000000;"> TextOutputCallback)
{
TextOutputCallback toc =<span style="color: #000000;"> (TextOutputCallback)callbacks[i];
System.err.println(“JAAS callback: ” +<span style="color: #000000;"> toc.getMessage());
}
<span style="color: #0000ff;">else <span style="color: #0000ff;">if (callbacks[i] <span style="color: #0000ff;">instanceof<span style="color: #000000;"> NameCallback)
{
NameCallback nc =<span style="color: #000000;"> (NameCallback)callbacks[i];
nc.setName(userid);
}
<span style="color: #0000ff;">else <span style="color: #0000ff;">if (callbacks[i] <span style="color: #0000ff;">instanceof<span style="color: #000000;"> PasswordCallback)
{
PasswordCallback pc =<span style="color: #000000;"> (PasswordCallback)callbacks[i];
pc.setPassword(password.toCharArray());
}
<span style="color: #0000ff;">else <span style="color: #0000ff;">if (callbacks[i] <span style="color: #0000ff;">instanceof<span style="color: #000000;"> weblogic.security.auth.callback.URLCallback)
{
URLCallback uc =<span style="color: #000000;"> (URLCallback)callbacks[i];
uc.setURL(url);
}
<span style="color: #0000ff;">else<span style="color: #000000;">
{
System.out.println(callbacks[i] +<span style="color: #000000;"> ” Unrecognized Callback”);
<span style="color: #0000ff;">throw <span style="color: #0000ff;">new<span style="color: #000000;"> UnsupportedCallbackException(callbacks[i],“Unrecognized Callback”);
}
}
}
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读