웹 애플리케이션용 프로그램 보안 API 개발
이 정보를 사용하면 웹 애플리케이션을 위해 API를 프로그래밍 방식으로 보안할 수 있습니다.
시작하기 전에
프로그램 보안은 선언적 보안 단독으로는 애플리케이션의 보안 모델을 표시하기에 충분하지 않는 경우에 보안 인지 애플리케이션에 의해 사용됩니다.
로그아웃, 로그인 및 인증 API는 WebSphere® Application Server의 이 릴리스에서 Java™ Servlet 3.0의 새로운 사항입니다.
웹 클라이언트가 보호 및 보호되지 않은 URI(Uniform Resource Identifier)와 상호 작용하는 방법을 판별하는 웹 인증의 몇몇 옵션을 구성할 수 있습니다. 또한 WebSphere Application Server가 HTTPS 클라이언트의 인증서 인증이 실패하는 경우 웹 클라이언트에서 기본 인증 정보를 확인하는지 여부를 지정할 수 있습니다. 자세한 정보는 인증 메커니즘 선택 기사를 참조하십시오.
어떤 프린시펄 클래스가 이러한 셀에 의해서 보호되는지를 나타내기 위해
로그인 모듈을 사용 가능으로 설정할 수 있습니다. 자세한 정보는 JAAS(Java Authentication and Authorization Service) 로그인 모듈을 사용하여
레지스트리 프린시펄을 SAF(System Authorization
Facility) 사용자 ID에 맵핑에 대한 주제를 참조하십시오.
isUserInRole 메소드가 사용되면 이 메소드에 전달되는 역할 이름을 포함하는 role-name 하위 요소를 사용하여 배치 디스크립터에서 security-role-ref 요소를 선언하거나 @DeclareRoles 어노테이션을 사용하십시오. 애플리케이션의 어셈블리 단계 동안에 실제 역할이 작성되면 논리 역할을 역할 이름으로서 사용하고 해당 역할을 실제 역할에 링크하기 위해 security-role-ref 요소의 설명에 어셈블러에 대한 충분한 힌트를 제공할 수 있습니다. 어셈블리 동안에 어셈블러는 role-link 하위 요소를 작성하여 역할 이름을 실제 역할에 링크합니다. Rational® Application Developer 등과 같은 어셈블리 도구가 사용되는 경우에는 security-role-ref 요소 작성이 가능합니다. 어셈블리 도구를 사용하여 어셈블리 단계 동안에 security-role-ref 요소를 작성할 수도 있습니다.
프로시저
- 서블릿 코드에서 필수 보안 메소드를 추가하십시오.
- role-name 필드를 사용하여 security-role-ref 요소를 작성하십시오. 개발 동안에 security-role-ref 요소가 작성되지 않은 경우에는 어셈블리 단계 동안에 작성되는지 확인하십시오.
결과
예
<security-role-ref>
<description>Provide hints to assembler for linking this role
name to an actual role here<\description>
<role-name>Mgr<\role-name>
</security-role-ref>
<security-role-ref>
<description>Hints provided by developer to map the role
name to the role-link</description>
<role-name>Mgr</role-name>
<role-link>Manager</role-link>
</security-role-ref>
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
....
// to logoff the current user
request.logout();
// to login with a new user
request.login(“bob”,”bobpwd”)
// to get remote user using getUserPrincipal()
java.security.Principal principal = request.getUserPrincipal();
String remoteUser = principal.getName();
// to get remote user using getRemoteUser()
remoteUser = request.getRemoteUser();
// to check if remote user is granted Mgr role
boolean isMgr = request.isUserInRole("Mgr");
// use this information in any way as needed by
// the application
....
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
....
// to logout the current user. If you are not already authenticate, then no need to call the logout() method.
request.logout();
// to login with a new user
request.login(“utle”,”mypwd”)
// the user utle subject now set on the thread and the LTPA SSO cookie is set in the response
....
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
....
// to logout the current user. If you are not already authenticate, then no need to call the logout() method.
// to login with a new user
request.authenticate(response);
// the new user subject now set on the thread and the LTPA SSO cookie is set in the response
....
}
@javax.annotation.security.DeclareRoles("Mgr")
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
....
// to get remote user using getUserPrincipal()
java.security.Principal principal = request.getUserPrincipal();
String remoteUser = principal.getName();
// to get remote user using getRemoteUser()
remoteUser = request.getRemoteUser();
// to check if remote user is granted Mgr role
boolean isMgr = request.isUserInRole("Mgr");
// use this information in any way as needed by
// the application
....
}
다음 예는 프로그램 보안 모델을 사용하는 웹 애플리케이션 및 서블릿을 묘사합니다.
이 예는 프로그램 보안 모델의 유일한 용도일 필요는 없는 하나의 용도를 설명합니다. 애플리케이션은 해당 애플리케이션에 의미 있는 다른 어떤 방법을 사용하여 getUserPrincipal, isUserInRole 및 getRemoteUser 메소드가 리턴하는 정보를 사용할 수 있습니다. 가능할 때마다 선언적 보안 모델을 사용하십시오.
파일 : HelloServlet.java
public class HelloServlet extends javax.servlet.http.HttpServlet {
public void doPost(
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
}
public void doGet(
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
String s = "Hello";
// get remote user using getUserPrincipal()
java.security.Principal principal = request.getUserPrincipal();
String remoteUserName = "";
if( principal != null )
remoteUserName = principal.getName();
// get remote user using getRemoteUser()
String remoteUser = request.getRemoteUser();
// check if remote user is granted Mgr role
boolean isMgr = request.isUserInRole("Mgr");
// display Hello username for managers and bob.
if ( isMgr || remoteUserName.equals("bob") )
s = "Hello " + remoteUserName;
String message = "<html> \n" +
"<head><title>Hello Servlet</title></head>\n" +
"<body> /n +"
"<h1> " +s+ </h1>/n " +
byte[] bytes = message.getBytes();
// displays "Hello" for ordinary users
// and displays "Hello username" for managers and "bob".
response.getOutputStream().write(bytes);
}
}
<security-role-ref>
<description> </description>
<role-name>Mgr</role-name>
</security-role-ref>