JAAS 使用windos登陆示例

使用JAAS自带的NT登陆模块实现简单的登陆验证。

import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

public class JaasTest {
	public static void main(String[] args) {
		LoginContext lc=null; 
		try { 
			lc= new LoginContext("MyExample"); 
			lc.login(); 
		} catch (LoginException e) { 
		// Authentication failed. 
			e.printStackTrace();
		} 
		
		// Authentication successful, we can now continue. 
		// We can use the returned Subject if we like. 
		Subject sub = lc.getSubject(); 
		
		sub.doAs(sub, new TestPrivilegedAction());
		

	}
}

  模块配置文件:

MyExample { 
	com.sun.security.auth.module.NTLoginModule Required debug=true; 
};

  通过 vm参数指定配置文件的位置:

-Djava.security.auth.login.config="D:\jass.config"

  

 

posted @ 2015-07-06 15:43  jlx351312  阅读(218)  评论(0编辑  收藏  举报