JAAS 认证

JAAS的目的:

for authentication of users, to reliably and securely determine who is currently executing Java code, regardless of whether the code is running as an application, an applet, a bean, or a servlet; and

 

for authorization of users to ensure they have the access control rights (permissions) required to do the actions performed.

认证的简单例子:

  1. package com.lht.jaas;   
  2.   
  3.   
  4. import javax.security.auth.login.*;   
  5. import com.sun.security.auth.callback.TextCallbackHandler;   
  6.   
  7. /**  
  8.  * This JaasAcn application attempts to authenticate a user  
  9.  * and reports whether or not the authentication was successful.  
  10.  */  
  11.   
  12. public class JaasAcn {   
  13.   
  14.   public static void main(String[] args) {   
  15.   
  16.       // Obtain a LoginContext, needed for authentication. Tell    
  17.       // it to use the LoginModule implementation specified by    
  18.       // the entry named "JaasSample" in the JAAS login    
  19.       // configuration file and to also use the specified    
  20.       // CallbackHandler.   
  21.       LoginContext lc = null;   
  22.       try {   
  23.           lc = new LoginContext("JaasSample",    
  24.                       new TextCallbackHandler());   
  25.       } catch (LoginException le) {   
  26.           System.err.println("Cannot create LoginContext. "  
  27.               + le.getMessage());   
  28.           System.exit(-1);   
  29.       } catch (SecurityException se) {   
  30.           System.err.println("Cannot create LoginContext. "  
  31.               + se.getMessage());   
  32.           System.exit(-1);   
  33.       }    
  34.   
  35.       try {   
  36.        
  37.           // attempt authentication   
  38.           lc.login();   
  39.        
  40.       } catch (LoginException le) {   
  41.        
  42.           System.err.println("Authentication failed: ");   
  43.           System.err.println("  " + le.getMessage());   
  44.           System.exit(-1);   
  45.        
  46.       }   
  47.        
  48.       System.out.println("Authentication succeeded!");   
  49.        
  50.     }   
  51. }   
  52.   

配置文件:

  1. JaasSample {   
  2.   com.sun.security.auth.module.Krb5LoginModule required;   
  3. };  
  1. grant codebase "file:./JaasAcn.jar" {   
  2.    permission javax.security.auth.AuthPermission    
  3.                     "createLoginContext.JaasSample";   
  4. };  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值