Liberty의 보안 공용 API
Liberty의 보안 공용 API는 보안 인프라를 확장하는 방법을 제공합니다.
Liberty에는 보안 기능을 구현하는 데 사용할 수 있는 공용 API가 포함되어 있습니다. Liberty의 보안 공용 API는 WebSphere® Application Server Traditional 보안 공용 API의 서브세트입니다. 기본 클래스는 WSSecurityHelper, WSSubject, RegistryHelper입니다. 이 클래스에는 WebSphere Application Server Traditional 버전에서 사용 가능한 메소드의 서브세트가 포함되어 있습니다. 새 클래스 WebSecurityHelper도 있습니다.
다음 섹션에서는 이 기본 클래스를 설명합니다. UserRegistry, WSCredential와 같은 기타 클래스 및 기타 예외 클래스도 있습니다.
Liberty에서 지원하는 모든 보안 공용 API는 Java™ API 문서에 있습니다. 각 Liberty API에 대한 Java API 문서는 ${wlp.install.dir}/dev 디렉토리의 javadoc 서브디렉토리 중 하나에 있는 별도 .zip 파일에서 사용 가능합니다.
- WSSecurityHelper
- 이 클래스는 메소드 isServerSecurityEnabled() 및 isGlobalSecurityEnabled()만 포함합니다. 무엇보다도 appSecurity-2.0 또는 zosSecurity-1.0이 사용되는 경우에는 이 호출에서 true를 리턴합니다. 그렇지 않으면, 메소드는 false를 리턴합니다. 이 메소드는 호환성을 위해 WebSphere Application Server Traditional WSSecurityHelper 클래스에서 실행됩니다.
- 참고:
- Liberty에는 셀이 없습니다. 따라서 Liberty에는 글로벌 보안 및 서버 보안 간에 구분이 없습니다. 따라서 두 메소드는 동일한 값을 리턴합니다.
- revokeSSOCookies(javax.servlet.http.HttpServletRequest req,javax.servlet.http.HttpServletResponse res) 메소드는 Liberty에서 지원되지 않습니다. 대신 Servlet 3.0 로그아웃 기능을 사용할 수 있습니다.
- 메소드 getLTPACookieFromSSOToken()의 이름이 새 공용 API 클래스 WebSecurityHelper로 변경됩니다.
- WSSubject
- 이 클래스는 보안 스레드 컨텍스트를 조회하고 설정하기 위한
유틸리티 메소드를 제공합니다. WebSphere Application Server Traditional
WSSubject의 모든 메소드는 Liberty에서 지원됩니다. 참고: Java 2 보안은 지원되지만, Liberty에서는 기본적으로 사용되지 않습니다. 따라서 기본적으로 WSSubject의 Java 2 보안 검사는 수행되지 않습니다.
- RegistryHelper
- 이 클래스는 UserRegistry 오브젝트 및 신뢰 받는 영역 정보에 대한 액세스를 제공합니다. Liberty에서 이는 WebSphere Application Server Traditional 메소드의 다음 서브세트를 포함합니다.
- public static UserRegistry getUserRegistry(String realmName) throws WSSecurityException
- public static List<String> getInboundTrustedRealms(String realmName) throws WSSecurityException
- public static boolean isRealmInboundTrusted(String inboundRealm, String localRealm)
- 참고: 이 메소드는 OSGI 동적 서비스가 변경될 때 변경될 수 있는 동적 정보를 포함합니다. 검색된 값이 시간이 경과될(stale) 수 있습니다. UserRegistry 참조는 캐시되어서는 안 됩니다.
- WebSecurityHelper
- 이 클래스는 이름이 변경된 getLTPACookieFromSSOToken() 메소드를 포함하며, 이 메소드는 WSSecurityHelper에서 이동되었습니다.
- public static Cookie getSSOCookieFromSSOToken() throws Exception
- PasswordUtil
- 이 클래스는 민감한 정보 보호를 위한 인코딩 및 디코딩 메소드를 제공합니다. 이
클래스를 API로 사용하려면 server.xml 파일에서
passwordUtilities-1.0 기능을 구성하십시오.
참고: 이 클래스는 WebSphere Application Server Traditional에도 존재합니다. 그러나 Liberty는 추가 메소드가 도입된 더 많은 기능을 추가합니다.
보안 공용 API 코드 예제
다음 예제에서는 Liberty에서 보안 공용 API를 사용하여
프로그래밍 방식 로그인을 수행하고 주제에 대해 작업하는 방법을 보여줍니다.
- 예제 1: 주제를 작성하고 권한 부여를 위해 사용
- 이 예제는 WSSecurityHelper, WSSubject,
UserRegistry를 사용하여 프로그래밍 방식 로그인을 수행하고
Java 주제를 작성한 다음
조치를 수행하여 해당 주제를 필요한 권한 부여에 사용하는 방법을 설명합니다.참고: 다음 코드는 WSSecurityHelper를 사용하여 보안 처리를추가로 수행하기 전에 보안이 사용 가능한지 여부를 확인합니다. Liberty의 모듈형 특성 때문에 이 확인은 광범위하게 사용됩니다. 보안을 사용하지 않으면 보안 런타임이 로드되지 않습니다. 보안이 사용 불가능하더라도 WSSecurityHelper는 항상 로드됩니다.
import java.rmi.RemoteException; import java.security.PrivilegedAction; import javax.security.auth.Subject; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.login.LoginContext; import javax.security.auth.login.LoginException; import com.ibm.websphere.security.CustomRegistryException; import com.ibm.websphere.security.UserRegistry; import com.ibm.websphere.security.WSSecurityException; import com.ibm.websphere.security.WSSecurityHelper; import com.ibm.websphere.security.auth.WSSubject; import com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl; import com.ibm.wsspi.security.registry.RegistryHelper; public class myServlet { ... if (WSSecurityHelper.isServerSecurityEnabled()) { UserRegistry ur = null; try { ur = RegistryHelper.getUserRegistry(null); } catch (WSSecurityException e1) { // record some diagnostic info return; } String userid = "user1"; String password = "user1password"; try { if (ur.isValidUser(userid)) { // create a Subject, authenticating with // a userid and password CallbackHandler wscbh = new WSCallbackHandlerImpl(userid, password); LoginContext ctx; ctx = new LoginContext("WSLogin", wscbh); ctx.login(); Subject subject = ctx.getSubject(); // Perform an action using the Subject for // any required authorization WSSubject.doAs(subject, action); } } catch (CustomRegistryException e) { // record some diagnostic info return; } catch (RemoteException e) { // record some diagnostic info return; } catch (LoginException e) { // record some diagnostic info return; } } ... private final PrivilegedAction action = new PrivilegedAction() { @Override public Object run() { // do something useful here return null; } }; }
- 예제 2: 주제를 작성하고 스레드에서 현재 주제로 작성
- 다음 예제는 WSSecurityHelper 및 WSSubject를
사용하여 프로그래밍 방식 로그인을 수행하고 Java 주제를 작성하는 방법을 설명합니다. 해당 주제를 스레드의 현재 주제로
작성한 후 원래 보안 스레드 컨텍스트를 복원하십시오. 참고: 다음 코드는 WSSecurityHelper를 사용하여 보안 처리를추가로 수행하기 전에 보안이 사용 가능한지 여부를 확인합니다.
import javax.security.auth.Subject; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.login.LoginContext; import javax.security.auth.login.LoginException; import com.ibm.websphere.security.WSSecurityException; import com.ibm.websphere.security.WSSecurityHelper; import com.ibm.websphere.security.auth.WSSubject; import com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl; ... if (WSSecurityHelper.isServerSecurityEnabled()) { CallbackHandler wscbh = new WSCallbackHandlerImpl("user1", "user1password"); LoginContext ctx; try { // create a Subject, authenticating with // a userid and password ctx = new LoginContext("WSLogin", wscbh); ctx.login(); Subject mySubject = ctx.getSubject(); Subject oldSubject = null; try { // Save a ref to the current Subject on the thread oldSubject = WSSubject.getRunAsSubject(); // Make mySubject the current Subject on the thread WSSubject.setRunAsSubject(mySubject); // Do something useful here. Any authorization // required will be performed using mySubject } catch (WSSecurityException e) { // record some diagnostic info return; } finally { // Put the original Subject back on the thread context if (oldSubject != null) { try { WSSubject.setRunAsSubject(oldSubject); } catch (WSSecurityException e) { // record some diagnostic info } } } } catch (LoginException e) { // record some diagnostic info return; } }
- 예제 3: 스레드에서 현재 주제의 정보 가져오기
- 다음 예제는 WSSecurityHelper, WSSubject, WSCredential을 사용하여
스레드의 현재 주제에 관한 정보를 가져오는 방법을 설명합니다.참고: 다음 코드는 WSSecurityHelper를 사용하여 보안 처리를추가로 수행하기 전에 보안이 사용 가능한지 여부를 확인합니다.
import java.util.ArrayList; import java.util.Iterator; import java.util.Set; import javax.security.auth.Subject; import javax.security.auth.login.CredentialExpiredException; import com.ibm.websphere.security.WSSecurityException; import com.ibm.websphere.security.WSSecurityHelper; import com.ibm.websphere.security.auth.CredentialDestroyedException; import com.ibm.websphere.security.auth.WSSubject; import com.ibm.websphere.security.cred.WSCredential; ... if (WSSecurityHelper.isServerSecurityEnabled()) { // Get the caller's subject Subject callerSubject; try { callerSubject = WSSubject.getCallerSubject(); } catch (WSSecurityException e) { // record some diagnostic info return; } WSCredential wsCred = null; Set<WSCredential> wsCredentials = callerSubject.getPublicCredentials(WSCredential.class); Iterator<WSCredential> wsCredentialsIterator = wsCredentials.iterator(); if (wsCredentialsIterator.hasNext()) { wsCred = wsCredentialsIterator.next(); try { // Print out the groups ArrayList<String> groups = wsCred.getGroupIds(); for (String group : groups) { System.out.println("Group name: " + group); } } catch (CredentialExpiredException e) { // record some diagnostic info return; } catch (CredentialDestroyedException e) { // record some diagnostic info return; } } } }