Announcement Announcement Module
No announcement yet.
SecurityContextHolder.getContext().getAuthenticati on() returns null Page Title Module
Move Remove Collapse
This topic is closed
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • SecurityContextHolder.getContext().getAuthenticati on() returns null

    I am trying to use SecurityContextHolder.getContext().getAuthenticati on() to check if the authentication has been done. On the basis of this discussion, I want to display/not display certain links on my menu. This menu is available on both secure and non secure page. Now, when I hit secure page (after logging in) this returns appropriate user information. However, when I hit a non secure page it returns null. Please advice what should I do to resolve this issue. Following is my Java code.

    public boolean getIsOkToDisplayAdmin() {
    return isAuthenticated();

    public boolean isAuthenticated() {

    boolean result = false;
    System.err.println("============================ Call to isAuthenticated ============================");
    Authentication auth = SecurityContextHolder.getContext().getAuthenticati on();

    if (auth != null) {

    System.err.println("---> 1");
    Object objPrincipal = auth.getPrincipal();
    if (objPrincipal != null) {

    System.err.println("---> 11");
    UserDetails userDet = (UserDetails) objPrincipal;
    System.err.println(userDet.getUsername() + ", "
    + userDet.getPassword());
    return true;
    } else {
    System.err.println("---> 12");
    } else {
    System.err.println("---> 2");

    return result;

    And here is my JSP page where I use this method to decide whether to display certain contents of menu.

    <rich:dropDownMenu rendered="#{loginBean.isOkToDisplayAdmin}">
    <f:facet name="label">
    <h:outputText value="Administration"/>
    <rich:menuItem submitMode="ajax" value="Account Maintenance" action="#{menuBean.listenerAcctMaint}"
    onclick="#{rich:component('loading')}.show()" oncomplete="Richfaces.hideModalPanel('loading');"/>
    Thanks in advance.