JSF/MyFaces를 사용하여 사용자 역할을 기반으로 조건을 만드는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/286686

문제

내 JSP 페이지에서 현재 사용자의 역할을 읽어야하는 옵션은 무엇입니까? 나는 알고있다 visibleOnUserRole="myRole" Tomahawk 구성 요소에 대한 속성이지만 간단한 가시성보다 조금 더 복잡한 것에 대한 역할이 필요합니다.

도움이 되었습니까?

해결책

그만큼 외부 컨텍스트 사용자 및 역할 정보를 노출시킵니다.

public class RolesAccess implements Serializable {

    public String getUserPrincipalName() {
        FacesContext context = FacesContext.getCurrentInstance();
        Principal principal = context.getExternalContext().getUserPrincipal();
        if(principal == null) {
            return null;
        }
        return principal.getName();
    }

    public String getUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getExternalContext().getRemoteUser();
    }

    public boolean isManager() {
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getExternalContext().isUserInRole("manager");
    }

}

서블릿에서 JSF를 사용하는 경우이 정보는 httpservletrequest.

관리되는 콩을 사용하여 표현 언어.

<f:view>
    <h:outputLabel value="#{rolesBean.userPrincipalName}" />
    <h:outputLabel value="#{rolesBean.user}" />
    <h:outputLabel value="#{rolesBean.manager}" />
</f:view>

다른 팁

Java EE 6 (이 질문이 질문/답변 될 때 사용할 수 없었던)에서는 역할을 직접 테스트 할 수 있습니다. request 페이스 렛 코드의 변수. 이것은 매우 대단합니다.

예를 들어

<h:outputText value="hi, admin!" rendered="#{request.isUserInRole('Admin')}" />
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top