EJB 애플리케이션용 프로그램 API 개발
이 주제에서는 EJB(Enterprise JavaBeans) 애플리케이션을 프로그램 방식으로 보안합니다.
이 태스크 정보
- IsCallerInRole(문자열 역할 이름): Bean 호출자에게 역할 이름으로 지정된 보안 역할이 부여된 경우 true를 리턴합니다. 호출자에게 지정된 역할이 부여되지 않거나 호출자가 인증되지 않은 경우 false를 리턴합니다. 지정된 역할에 모두가 액세스할 수 있도록 권한이 부여된 경우 항상 true를 리턴합니다.
- getCallerPrincipal: java.security를 리턴합니다. bean 호출자 이름을 포함하는 프린시펄 오브젝트. 호출자에게 권한이 없는 경우, 권한이 없는 이름을 포함하는 프린시펄을 리턴합니다.
어떤 프린시펄 클래스가 이러한 셀에 의해서 보호되는지를 나타내기 위해 로그인 모듈을 사용 가능으로 설정할 수 있습니다.
isCallerInRole 메소드가 사용될 때, 이 메소드에 전달되는 역할명을 포함하는 서브요소인 역할명을 가진 배치 디스크립터에 security-role-ref 요소를 선언하십시오. 애플리케이션의 어셈블리 단계 중에 실제 역할이 작성되기 때문에 논리 역할을 역할 이름으로 사용하고 security-role-ref 요소 설명에서 어셈블러에 충분한 힌트를 제공하여 해당 역할을 실제 역할에 링크할 수 있습니다. 어셈블리 중에, 어셈블러가 역할 링크 하위 요소를 작성하여 역할 이름을 실제 역할에 링크합니다. Rational® Application Developer와 같은 어셈블리 도구가 사용되면 보안 역할 -ref 요소를 작성할 수 있습니다. 또한 어셈블리 도구를 사용하여 어셈블리 단계 중에 security-role-ref 요소를 작성할 수도 있습니다.
프로시저
- EJB 모듈 코드에 필수 보안 메소드를 추가하십시오.
- isCallerInRole 메소드에서 사용되는 모든 역할명에 대한 역할명 필드로 security-role-ref 요소를 작성하십시오. security-role-ref 요소가 개발 동안 작성되지 않으면, 어셈블리 단계 중 작성되어야 합니다.
결과
예
Java EE 보안 모델 기능을 사용하여 보안 정책을 선언 방식으로 지정하는 것은 EJB 애플리케이션이 외부 자원에 액세스하고 자체 권한 테이블을 사용하여 이들 외부 자원에 대한 액세스를 제어(사용자 맵핑에 대한 외부 자원)하려는 경우에 유용합니다. 이 경우, getCallerPrincipal 메소드를 사용하여 호출자 ID를 가져온 다음 애플리케이션이 자체의 권한 테이블을 참조하여 권한을 수행할 수 있습니다. 또한 호출자 ID는 데이터베이스와 같은 외부 소스 또는 또 다른 엔터프라이즈 Bean에서 해당 사용자 정보를 검색할 수도 있습니다. 유사한 방법으로 isCallerInRole 메소드를 사용할 수 있습니다.
<security-role-ref>
<description>Provide hints to assembler for linking this role-name to
actual role here<\description>
<role-name>Mgr<\role-name>
</security-role-ref>
<security-role-ref>
<description>Hints provided by developer to map role-name to role-link</description>
<role-name>Mgr</role-name>
<role-link>Manager</role-link>
</security-role-ref>
public class aSessionBean implements SessionBean {
.....
// SessionContext extends EJBContext. If it is entity bean use EntityContext
javax.ejb.SessionContext context;
// The following method will be called by the EJB container
// automatically
public void setSessionContext(javax.ejb.SessionContext ctx) {
context = ctx; // save the session bean's context
}
....
private void aBusinessMethod() {
....
// to get bean's caller using getCallerPrincipal()
java.security.Principal principal = context.getCallerPrincipal();
String callerId= principal.getName();
// to check if bean's caller is granted Mgr role
boolean isMgr = context.isCallerInRole("Mgr");
// use the above information in any way as needed by the
//application
....
}
....
}
@javax.annotation.security.DeclareRoles("Mgr")
@Stateless // annotation is used to indicate a session bean
public class aSessionBean implements MyBusinessInterface { //you don't have to extend sessionbean interface
.....
// SessionContext extends EJBContext. In EJB 3.0 use Resource annotation to inject context
@Resource
javax.ejb.SessionContext context; }
....
private void aBusinessMethod() {
....
// to get bean's caller using getCallerPrincipal()
java.security.Principal principal = context.getCallerPrincipal();
String callerId= principal.getName();
// to check if bean's caller is granted Mgr role
boolean isMgr = context.isCallerInRole("Mgr");
// use the above information in any way as needed by the
//application
....
}
....
}