보안 공용 API
Liberty 프로파일의 보안 공용 API는 보안 인프라 확장 방법을 제공합니다.
Liberty 프로파일에는 보안 기능을 구현하는 데 사용할 수 있는 공용 API가 포함되어 있습니다. Liberty 프로파일의 보안 공용 API는 전체 프로파일 보안 공용 API의 서브세트입니다. 기본 클래스는 WSSecurityHelper, WSSubject, RegistryHelper입니다. 이 클래스는 전체 프로파일 버전에서 사용 가능한 메소드의 서브세트를 포함합니다. 새 클래스 WebSecurityHelper도 있습니다.
다음 섹션에서는 이 기본 클래스를 설명합니다. UserRegistry, WSCredential와 같은 기타 클래스 및 기타 예외 클래스도 있습니다.
Liberty 프로파일에서 지원되는 모든 보안 공용 API는 Java™ API 문서에 있습니다. 각 Liberty 프로파일 API에 대한 Java API 문서는 Information Center의 프로그래밍 인터페이스(API) 절에 자세히 설명되어 있고 ${wlp.install.dir}/dev 디렉토리의 javadoc 서브디렉토리 중 하나에 별도의 .zip 파일로도 사용 가능합니다.
- WSSecurityHelper
- 이 클래스는 메소드 isServerSecurityEnabled() 및 isGlobalSecurityEnabled()만 포함합니다.
다음 기능 중 하나가 server.xml 파일에서 사용 가능한 경우
둘 다 true를 리턴합니다.
- appSecurity-2.0
- zosSecurity-1.0
- 참고:
- Liberty 프로파일에는 셀이 없으므로 Liberty 프로파일에서 글로벌 보안 및 서버 보안 간의 구분이 없습니다. 따라서 두 메소드는 모두 동일한 값을 리턴합니다.
- revokeSSOCookies(javax.servlet.http.HttpServletRequest req,javax.servlet.http.HttpServletResponse res) 메소드는 Liberty 프로파일에서 지원되지 않습니다. 대신 Servlet 3.0 로그아웃 기능을 사용할 수 있습니다.
- 메소드 getLTPACookieFromSSOToken()의 이름이 새 공용 API 클래스 WebSecurityHelper로 변경됩니다.
- WSSubject
- 이 클래스는 보안 스레드 컨텍스트를 조회하고 설정하기 위한
유틸리티 메소드를 제공합니다. 전체 프로파일 WSSubject의
모든 메소드는 Liberty 프로파일에서 지원됩니다. 참고: Java 2 보안은 지원되지만 Liberty 프로파일에서 기본적으로 사용 설정되어 있지는 않습니다. 따라서 기본적으로 WSSubject의 Java 2 보안 검사는 수행되지 않습니다.
- RegistryHelper
- 이 클래스는 UserRegistry 오브젝트 및 신뢰할 수 있는 영역 정보에 대한 액세스를 제공합니다. Liberty 프로파일에서 이는 전체 프로파일 메소드의 다음 서브세트를 포함합니다.
- 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)
- WebSecurityHelper
- 이 클래스는 이름이 바뀐 getLTPACookieFromSSOToken() 메소드(WSSecurityHelper에서 이동됨)를 포함합니다.
- public static Cookie getLTPACookieFromSSOToken() throws Exception
보안 공용 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; } } } }