0

I am starting to develop a website using Spring MVC 4. I'd like my website to have a top-navigation. When a user hovers over the top-navigation I want them to see a list of the pages that they have access to.

How do I achieve this in a Spring MVC 4 best-practice way?

To illustrate my example, imagine the following:

<ul id='menu-nav'>
<li><a href='homeURL'>Home</a></li>
<li><a href='page1URL'>Page 1</a></li>
<li><a href='page2URL'>Page 2</a></li>
</ul>

Above is a list of all the pages in my very limited website. Once each user logs in, I want them to only be able to see the links to the pages that they have access to.

So, as an example, a user logging in with ADMIN rights would see ALL the links. But a user who DOES NOT have ADMIN rights would only see links to the Home Page and Page 1.

Can anyone suggest a way to implement this?

user2318704
  • 95
  • 2
  • 9

1 Answers1

0

I think this should work :

<ul id='menu-nav'>
    <li><a href='homeURL'>Home</a></li>
    <li><a href='page1URL'>Page 1</a></li>
    <!-- For many roles -->
    <!-- <security:authorize access="hasAnyRole('ADMIN', 'USER')"> -->
    <!-- For one role -->
    <security:authorize access="hasRole('ADMIN')">
        <li><a href='page2URL'>Page 2</a></li>
    </security:authorize>
</ul>

Check this answer : https://stackoverflow.com/a/11469342/8800147

br.julien
  • 3,420
  • 2
  • 23
  • 44
  • Hi Brice, thanks for getting back to me. I can see how your solution would work for static html. But would this work if I generated the list using a loop (list data passed to the page in the Model)? And how do I handle more than one group? – user2318704 Nov 07 '17 at 16:11
  • Hi, I edited the answer so you can see how it would be with many roles. You would use **hasAnyRole**. I am not sure about the conditional statements in the templates (maybe this will help https://stackoverflow.com/questions/10815464/render-html-conditionally-in-spring-mvc), but otherwise maybe you could dynamically make a list for admin users and another one for the other users. – br.julien Nov 07 '17 at 16:37
  • Thanks for the update. You mention the idea of dynamically making a list for admin users and another for the other users. I think that helps...I might try thinking about how to create the list based on the logged in user. The jsp then just has to display whatever it is given. – user2318704 Nov 07 '17 at 16:53