2009-10-22 37 views
5

Hãy xem xét các thiết lập sau:Làm thế nào để sử dụng các thông tin trong một LTPA thẻ

  • Một webapplication triển khai trên một Application Server Websphere (6.1 nếu vấn đề)
  • ứng dụng sẽ được truy cập thông qua một webseal reverse proxy
  • các webseal sẽ chăm sóc của các xác thực và chuyển vào một thẻ LTPA như dấu hiệu của chứng thực hợp lệ

Nếu tôi đã làm đúng, token LTPA chứa các thông tin như tên, rol es và như vậy.

Câu hỏi: làm cách nào để truy cập thông tin này từ mã thông báo LTPA trong ứng dụng web java của tôi?

Trả lời

9

Bạn không trực tiếp truy cập mã thông báo LTPA, đúng hơn là bạn cho rằng WebSphere đã thiết lập một ngữ cảnh bảo mật cho bạn trên cơ sở các quy trình xác thực của nó.

Sau đó bạn có thể sử dụng

getUserPrincipal() 

trên HttpServletRequest đối tượng của bạn để truy cập vào danh tính của người dùng.

Vai trò là đặc biệt đến tài nguyên hiện tại (serlvet, ejb ...) và do đó bạn sử dụng phương pháp HttpServletRequest

isUserInRole() 

để xác định xem người dùng đang ở trong một vai trò.

Bạn cũng có thể sử dụng phương pháp

public static javax.security.auth.Subject getCallerSubject() 

để có được thông tin bảo mật hơn nữa trong đó có thành viên nhóm.

+0

Tuyệt vời, chính xác những gì tôi đang tìm kiếm. Thanx –

10

Nhìn vào bên trong mã thông báo LTPA thật tuyệt vời để gỡ lỗi, chúng tôi sử dụng rất nhiều thứ này. Bạn sẽ cần khóa và mật khẩu ltpa để làm việc này

 
/* Copyright notice 
# Copyright (C) 2007, Cosmin Stejerean (http://www.offbytwo.com) 
# 
# You are free to use this code under the terms of the Creative Commons Attribution license 
# available at http://creativecommons.org/licenses/by/3.0/ 
# so long as you include the following notice 'includes code from Cosmin Stejerean (http://www.offbytwo.com)' 
*/ 

import java.security.Key; 
import java.security.MessageDigest; 
import java.security.spec.KeySpec; 
import java.sql.Date; 
import java.text.SimpleDateFormat; 
import java.util.Arrays; 
import java.util.StringTokenizer; 

import javax.crypto.Cipher; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.DESedeKeySpec; 

import sun.misc.BASE64Decoder; 


//The shared 3DES key is itself encrypted using the SHA hash value of the LTPA password (padded with 0x0 upto 24 bytes). 

public class LtpaDecoder 
{ 
    private String ltpa3DESKey = "JvJRzwdhKk6o40FuATa9acKD2uaXswVHlUsn2c2+MKQ="; 
    private String ltpaPassword = "secretpassword"; 

    private String sUserInfo = ""; 
    private Date dExpiry; 
    private String sFullToken = ""; 
    private String sSignature = ""; 

    public static void main(String[] args) 
    { 
      String tokenCipher = "vsof5exb990sb2r5hRJ+bneCnmBTuLQ3XF+......"; 

      try { 
      LtpaDecoder t = new LtpaDecoder(tokenCipher); 
      System.out.println("UserInfo: " + t.getUserInfo()); 
      System.out.println("Expiry: " + t.getExpiryDate()); 
      System.out.println("Full token: " + t.getFullToken()); 
     } 
     catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public LtpaDecoder(String fulltoken) throws Exception { 
     byte[] secretKey = getSecretKey(this.ltpa3DESKey, this.ltpaPassword); 
     String ltpaPlaintext = new String(decryptLtpaToken(fulltoken, secretKey)); 

     extractTokenData(ltpaPlaintext); 
    } 

    private void extractTokenData(String token) 
    { 
     System.out.println("\n"); 
     StringTokenizer st = new StringTokenizer(token, "%"); 

     sUserInfo = st.nextToken(); 
     String sExpires = st.nextToken(); 
     sSignature = st.nextToken(); 
     dExpiry = new Date(Long.parseLong(sExpires)); 
     sFullToken = token; 
    } 

    public String getSignature() { 
     return sSignature; 
    } 

    public String getFullToken() { 
     return sFullToken; 
    } 

    public String getUserInfo() { 
     return sUserInfo; 
    } 

    public String getExpiryDate() { 
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); 
     return sdf.format(dExpiry); 
    } 

    private byte[] getSecretKey(String shared3DES, String password) throws Exception 
    { 
     MessageDigest md = MessageDigest.getInstance("SHA"); 
     md.update(password.getBytes()); 
     byte[] hash3DES = new byte[24]; 
     System.arraycopy(md.digest(), 0, hash3DES, 0, 20); 
     Arrays.fill(hash3DES, 20, 24, (byte) 0); 
     // decrypt the real key and return it 
     BASE64Decoder base64decoder = new BASE64Decoder(); 
     return decrypt(base64decoder.decodeBuffer(shared3DES), hash3DES); 
    } 

    public byte[] decryptLtpaToken(String encryptedLtpaToken, byte[] key) throws Exception 
    { 
     BASE64Decoder base64decoder = new BASE64Decoder(); 
     final byte[] ltpaByteArray = base64decoder.decodeBuffer(encryptedLtpaToken); 
     return decrypt(ltpaByteArray, key); 
    } 

    public byte[] decrypt(byte[] ciphertext, byte[] key) throws Exception { 
     final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); 
     final KeySpec keySpec = new DESedeKeySpec(key); 
     final Key secretKey = SecretKeyFactory.getInstance("TripleDES").generateSecret(keySpec); 

     cipher.init(Cipher.DECRYPT_MODE, secretKey); 
     return cipher.doFinal(ciphertext); 
    } 
} 
+0

Có nguy cơ nói rõ ràng, suy nghĩ có thể tốt đẹp cho việc gỡ lỗi trong môi trường phát triển, nhưng chúng tôi không dựa vào mã ứng dụng sản xuất trên kỹ thuật này, đúng không? – djna

+0

Sau khi làm việc với các sản phẩm của IBM trong vài năm, tôi muốn nói mã này có ích trong mọi môi trường;) – Tommy

+0

Trong khi tôi hy vọng không bao giờ phải sử dụng những thứ đó trong mã sản xuất (hoặc tìm thấy ở đó) nó vẫn hữu ích. –

Các vấn đề liên quan