AbdulAziz
AbdulAziz

Reputation: 6278

Hide or display links on JSP as per perticular user?

I have a home page in JSP of my MVC application that have some links. Now my objective is to hide or display particular links to some some particular users which I already defined in my applicationContext-security.xml as below:

<authentication-provider>
        <user-service id="userDetailsService">
            <user name="admin" password="admin" authorities="ROLE_TEST, ROLE_USER, ROLE_SUPERVISOR, ROLE_ADMIN" />
            <user name="tom" password="kite" authorities="ROLE_SUPERVISOR, ROLE_TEST, ROLE_USER" />
            <user name="user" password="pass" authorities="ROLE_USER, ROLE_TEST" />
            <user name="test" password="test" authorities="ROLE_TEST" />
        </user-service>
    </authentication-provider>

When a user logs into my website, he only can see the links he is authorized to see. Do you have or know where I could find some examples of this?

My hello.jsp (application home page)

<%@ include file="/WEB-INF/jsp/include.jsp" %>

<html>
  <head>
  <link rel="stylesheet" type="text/css" href="css/mystyle.css" />
  <title><fmt:message key="title"/></title>
  </head>
  <body>
    <h1><fmt:message key="heading"/></h1>
    <p><fmt:message key="greeting"/> <c:out value="${model.now}"/></p>
    <h3>Products</h3>
    <table border="2" align="center">
 <tr>
 <td align="center" width="50"><b><span style="color:red">Id</span></b></td>

 <td align="center" width="150"><b><span style="color:red">Product Name</span></b></td>

 <td align="center" width="150"><b><span style="color:red">Product Price</span></b></td>

 <td align="center" colspan="2" width="70"><b><span style="color:red">Actions</span></b></td>

 </tr>
    <c:forEach items="${model.products}" var="prod">
      <tr>
   <td align="center"><c:out value="${prod.id}"/></td>
   <td align="center"><c:out value="${prod.description}"/></td>
   <td align="center"><i>$<c:out value="${prod.price}"/></i></td>
   <td align="center" width="70"> <a href="<c:url value="/editpage.htm?id="/>${prod.id}&prodname=${prod.description}&prodprice=${prod.price}">Edit</a></td>
   <td align="center" width="70"><a href="<c:url value="/deleteprod.htm?id="/>${prod.id}&prodname=${prod.description}&prodprice=${prod.price}">Delete</a></td>
  </tr>
     </c:forEach>
     </table>
    <br>
    <ul><li><a href="<c:url value="/addprod.htm"/>">Add New Products</a></li></ul>
    <ul><li><a href="<c:url value="/priceincrease.htm"/>">Increase Prices</a></li></ul></br>
    <%@ include file="/WEB-INF/jsp/navigation.jsp" %>
    <%@ include file="/WEB-INF/jsp/userinfobar.jsp"%>
    <p>
  </body>
</html>

thanks for any help!

Upvotes: 1

Views: 4231

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

Spring security has a JSP taglib to do that.

Upvotes: 2

Related Questions