You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by Apache Wiki <wi...@apache.org> on 2006/07/12 19:24:17 UTC

[Tapestry Wiki] Update of "AcegiSpringJava5Part2" by JesperZedlitz

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.

The following page has been changed by JesperZedlitz:
http://wiki.apache.org/tapestry/AcegiSpringJava5Part2

New page:
 * AcegiSpringJava5 - First part of the tutorial

Now that I know how to protect my Tapestry pages I would like to print the name of a registered user. That is not difficult...

To avoid duplicated code I created a base class for {{{Home.java}}} and {{{Secured.java}}}:

{{{src/main/java/de/zedlitz/tapestry/acegi/UserPage.java}}}
{{{
package de.zedlitz.tapestry.acegi;

import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.UserDetails;

public abstract class UserPage extends org.apache.tapestry.html.BasePage {
    public String getUserName() {
        String userName = null;

        if( SecurityContextHolder.getContext() != null ) {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            if( auth != null ) {
                Object principal = auth.getPrincipal();
                if( principal != null ) {
                    if( principal instanceof UserDetails ) {
                        userName = ((UserDetails)principal).getUsername();
                    } else {
                        userName = principal.toString();
                    }
                }
            }
        }

        if( userName == null ) {
            userName = "";
        }
        return userName;
    }
}
}}}

The two existing pages extend this base class:

{{{src/main/java/de/zedlitz/tapestry/acegi/Home.java}}}
{{{
package de.zedlitz.tapestry.acegi;
public abstract class Home extends UserPage { }
}}}

{{{src/main/java/de/zedlitz/tapestry/acegi/Secured.java}}}
{{{
package de.zedlitz.tapestry.acegi;

@org.acegisecurity.annotation.Secured("ROLE_USER")
public abstract class Secured extends UserPage {}
}}}

The last step is to display the username on the html pages:

{{{src/main/webapp/Home.html}}}
{{{
<html>
  <head>
    <title>tapestry-acegi: normal page</title>
  </head>
  <body>
    <h1>tapestry-acegi: normal page</h1>
    <p>You are logged in as <em><span jwcid="@Insert" value="ognl:userName">Timmi Tester</span></em>.</p>
    <p><a href="Secured.html" jwcid="@PageLink" page="Secured">secured page</a></p>
  </body>
</html>
}}}

{{{src/main/webapp/Home.html}}}
{{{
<html>
  <head>
    <title>tapestry-acegi: secured page</title>
  </head>
  <body>
    <h1>tapestry-acegi: secured page</h1>
    <p>You are logged in as <em><span jwcid="@Insert" value="ognl:userName">Timmi Tester</span></em>.</p>
    <p><a href="Home.html" jwcid="@PageLink" page="Home">back to homepage</a></p>
  </body>
</html>
}}}

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org