You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by petros <pe...@cypoz.com> on 2007/05/17 03:17:11 UTC

Tapestry 5 and Acegi

Hi

I integrated T5.0.4 with Acegi and I wanted to share my code with this
forum. 
The solution may not be the best approach but it could save you sometime for
now. 
I am only providing the security related code. 

Login.html 
<t:layoutcmpnt
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">

	<t:loginForm> 		
		<div class="error_message">  			              
			${errorMessage}
		</div>	 
		<t:if test="feedbackMessage">      
			<div class="feedback_message">  			              
            	${feedbackMessage}
        	</div>
		</t:if> 

		<t:label for="usernameField"/>:
		<input t:type="TextField" t:id="usernameField" size="15"/>
		<br/>
		<t:label for="passwordField"/>:
		<input t:type="PasswordField" t:id="passwordField" size="15"/>
		<br />  
		<div align="center">  
			<p>
				<t:Submit t:value="message:login-label" />
			</p>  
		</div> 		
	</t:loginForm>

Login.java
	private String username;
	private String password;
	private String emailAddress;

    @Component
    private Form loginForm;

	@Persist
	public String errorMessage;
	
	@Persist
	public String feedbackMessage;

	@Inject
	@Service("RequestGlobals")
	private RequestGlobals requestGlobals;	
	
	@Inject
	private Messages messages;
	
    @PageAttached
    void pageAttached(MarkupWriter writer)
    {
		@SuppressWarnings("unused")
		String errorParamAsString =
requestGlobals.getRequest().getParameter("error");
		
		boolean error = Boolean.parseBoolean(errorParamAsString);
    	if(error)
    	{
    		errorMessage = messages.format("login-error-message", new Object[]{});
    		feedbackMessage = "";
    	}
    	else
    	{
//    		errorMessage = "";
    	}
    }	
	
    Object onSuccessFromLoginForm()
    {
    	Link acegiLoginLink = new LinkImpl(new NoOpURLEncoder() , "jumpstart",
"j_security_check");
    	acegiLoginLink.addParameter("j_username", username);
    	acegiLoginLink.addParameter("j_password", password);
    	
    	
    	return acegiLoginLink;
    }

... getters and setters

LayoutCmpnt.java
	public User getTapestryPrincipal() {
		User tapestryPrincipal = null;
		Authentication authentication =
SecurityContextHolder.getContext().getAuthentication();
		
		if(authentication != null)
		{
			Object acegiPrincipal = authentication.getPrincipal();
			if(acegiPrincipal != null)
			{
				
				if(acegiPrincipal instanceof String)
				{
					tapestryPrincipal = null;
				}
				else
				{
					
					tapestryPrincipal = (User) acegiPrincipal;
				}
			}
		}
		return tapestryPrincipal;
	}	

LayoutCmpnt.html
<strong>${tapestryPrincipal.username}</strong>

Update Acegi Security Context
I also have this code where the user's details are updated in order to keep
the Acegi Principal up to date. 

UsernamePasswordAuthenticationToken token = new    
   UsernamePasswordAuthenticationToken(updatedUser,updatedUsername,
updatedAuthorities);
   SecurityContextHolder.getContext().setAuthentication(token);

Petros
-- 
View this message in context: http://www.nabble.com/Tapestry-5-and-Acegi-tf3769520.html#a10657302
Sent from the Tapestry - User mailing list archive at Nabble.com.


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


Re: Tapestry 5 and Acegi

Posted by petros <pe...@cypoz.com>.
Hi Adnrea, 

I'll go through the links you provided and I will spend some time improving
my code before updating the wiki. 
At the moment I feel that my code is more of a hack than a nice solution.

Petros


Andreas Andreou-3 wrote:
> 
> Petros, hi.
> It would be nice to have this in the wiki,
> http://wiki.apache.org/tapestry/FrontPage
> 
> BTW, there's already some T4.x related acegi pages there
> (http://wiki.apache.org/tapestry/AcegiSpringJava5?highlight=%28acegi%29)
> 
> 
> On 5/17/07, petros <pe...@cypoz.com> wrote:
>>
>>
>> Hi
>>
>> I integrated T5.0.4 with Acegi and I wanted to share my code with this
>> forum.
>> The solution may not be the best approach but it could save you sometime
>> for
>> now.
>> I am only providing the security related code.
>>
>> Login.html
>> <t:layoutcmpnt
>> xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
>>
>>         <t:loginForm>
>>                 <div class="error_message">
>>                         ${errorMessage}
>>                 </div>
>>                 <t:if test="feedbackMessage">
>>                         <div class="feedback_message">
>>                 ${feedbackMessage}
>>                 </div>
>>                 </t:if>
>>
>>                 <t:label for="usernameField"/>:
>>                 <input t:type="TextField" t:id="usernameField"
>> size="15"/>
>>                 <br/>
>>                 <t:label for="passwordField"/>:
>>                 <input t:type="PasswordField" t:id="passwordField"
>> size="15"/>
>>                 <br />
>>                 <div align="center">
>>                         <p>
>>                                 <t:Submit t:value="message:login-label"
>> />
>>                         </p>
>>                 </div>
>>         </t:loginForm>
>>
>> Login.java
>>         private String username;
>>         private String password;
>>         private String emailAddress;
>>
>>     @Component
>>     private Form loginForm;
>>
>>         @Persist
>>         public String errorMessage;
>>
>>         @Persist
>>         public String feedbackMessage;
>>
>>         @Inject
>>         @Service("RequestGlobals")
>>         private RequestGlobals requestGlobals;
>>
>>         @Inject
>>         private Messages messages;
>>
>>     @PageAttached
>>     void pageAttached(MarkupWriter writer)
>>     {
>>                 @SuppressWarnings("unused")
>>                 String errorParamAsString =
>> requestGlobals.getRequest().getParameter("error");
>>
>>                 boolean error = Boolean.parseBoolean(errorParamAsString);
>>         if(error)
>>         {
>>                 errorMessage = messages.format("login-error-message", new
>> Object[]{});
>>                 feedbackMessage = "";
>>         }
>>         else
>>         {
>> //              errorMessage = "";
>>         }
>>     }
>>
>>     Object onSuccessFromLoginForm()
>>     {
>>         Link acegiLoginLink = new LinkImpl(new NoOpURLEncoder() ,
>> "jumpstart",
>> "j_security_check");
>>         acegiLoginLink.addParameter("j_username", username);
>>         acegiLoginLink.addParameter("j_password", password);
>>
>>
>>         return acegiLoginLink;
>>     }
>>
>> ... getters and setters
>>
>> LayoutCmpnt.java
>>         public User getTapestryPrincipal() {
>>                 User tapestryPrincipal = null;
>>                 Authentication authentication =
>> SecurityContextHolder.getContext().getAuthentication();
>>
>>                 if(authentication != null)
>>                 {
>>                         Object acegiPrincipal =
>> authentication.getPrincipal();
>>                         if(acegiPrincipal != null)
>>                         {
>>
>>                                 if(acegiPrincipal instanceof String)
>>                                 {
>>                                         tapestryPrincipal = null;
>>                                 }
>>                                 else
>>                                 {
>>
>>                                         tapestryPrincipal = (User)
>> acegiPrincipal;
>>                                 }
>>                         }
>>                 }
>>                 return tapestryPrincipal;
>>         }
>>
>> LayoutCmpnt.html
>> <strong>${tapestryPrincipal.username}</strong>
>>
>> Update Acegi Security Context
>> I also have this code where the user's details are updated in order to
>> keep
>> the Acegi Principal up to date.
>>
>> UsernamePasswordAuthenticationToken token = new
>>    UsernamePasswordAuthenticationToken(updatedUser,updatedUsername,
>> updatedAuthorities);
>>    SecurityContextHolder.getContext().setAuthentication(token);
>>
>> Petros
>> --
>> View this message in context:
>> http://www.nabble.com/Tapestry-5-and-Acegi-tf3769520.html#a10657302
>> Sent from the Tapestry - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
> 
> 
> -- 
> Andreas Andreou - andyhot@apache.org - http://andyhot.di.uoa.gr
> Tapestry / Tacos developer
> Open Source / JEE Consulting
> 
> 

-- 
View this message in context: http://www.nabble.com/Tapestry-5-and-Acegi-tf3769520.html#a10657702
Sent from the Tapestry - User mailing list archive at Nabble.com.


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


Re: Tapestry 5 and Acegi

Posted by Andreas Andreou <an...@di.uoa.gr>.
Petros, hi.
It would be nice to have this in the wiki,
http://wiki.apache.org/tapestry/FrontPage

BTW, there's already some T4.x related acegi pages there
(http://wiki.apache.org/tapestry/AcegiSpringJava5?highlight=%28acegi%29)


On 5/17/07, petros <pe...@cypoz.com> wrote:
>
>
> Hi
>
> I integrated T5.0.4 with Acegi and I wanted to share my code with this
> forum.
> The solution may not be the best approach but it could save you sometime
> for
> now.
> I am only providing the security related code.
>
> Login.html
> <t:layoutcmpnt
> xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
>
>         <t:loginForm>
>                 <div class="error_message">
>                         ${errorMessage}
>                 </div>
>                 <t:if test="feedbackMessage">
>                         <div class="feedback_message">
>                 ${feedbackMessage}
>                 </div>
>                 </t:if>
>
>                 <t:label for="usernameField"/>:
>                 <input t:type="TextField" t:id="usernameField" size="15"/>
>                 <br/>
>                 <t:label for="passwordField"/>:
>                 <input t:type="PasswordField" t:id="passwordField"
> size="15"/>
>                 <br />
>                 <div align="center">
>                         <p>
>                                 <t:Submit t:value="message:login-label" />
>                         </p>
>                 </div>
>         </t:loginForm>
>
> Login.java
>         private String username;
>         private String password;
>         private String emailAddress;
>
>     @Component
>     private Form loginForm;
>
>         @Persist
>         public String errorMessage;
>
>         @Persist
>         public String feedbackMessage;
>
>         @Inject
>         @Service("RequestGlobals")
>         private RequestGlobals requestGlobals;
>
>         @Inject
>         private Messages messages;
>
>     @PageAttached
>     void pageAttached(MarkupWriter writer)
>     {
>                 @SuppressWarnings("unused")
>                 String errorParamAsString =
> requestGlobals.getRequest().getParameter("error");
>
>                 boolean error = Boolean.parseBoolean(errorParamAsString);
>         if(error)
>         {
>                 errorMessage = messages.format("login-error-message", new
> Object[]{});
>                 feedbackMessage = "";
>         }
>         else
>         {
> //              errorMessage = "";
>         }
>     }
>
>     Object onSuccessFromLoginForm()
>     {
>         Link acegiLoginLink = new LinkImpl(new NoOpURLEncoder() ,
> "jumpstart",
> "j_security_check");
>         acegiLoginLink.addParameter("j_username", username);
>         acegiLoginLink.addParameter("j_password", password);
>
>
>         return acegiLoginLink;
>     }
>
> ... getters and setters
>
> LayoutCmpnt.java
>         public User getTapestryPrincipal() {
>                 User tapestryPrincipal = null;
>                 Authentication authentication =
> SecurityContextHolder.getContext().getAuthentication();
>
>                 if(authentication != null)
>                 {
>                         Object acegiPrincipal =
> authentication.getPrincipal();
>                         if(acegiPrincipal != null)
>                         {
>
>                                 if(acegiPrincipal instanceof String)
>                                 {
>                                         tapestryPrincipal = null;
>                                 }
>                                 else
>                                 {
>
>                                         tapestryPrincipal = (User)
> acegiPrincipal;
>                                 }
>                         }
>                 }
>                 return tapestryPrincipal;
>         }
>
> LayoutCmpnt.html
> <strong>${tapestryPrincipal.username}</strong>
>
> Update Acegi Security Context
> I also have this code where the user's details are updated in order to
> keep
> the Acegi Principal up to date.
>
> UsernamePasswordAuthenticationToken token = new
>    UsernamePasswordAuthenticationToken(updatedUser,updatedUsername,
> updatedAuthorities);
>    SecurityContextHolder.getContext().setAuthentication(token);
>
> Petros
> --
> View this message in context:
> http://www.nabble.com/Tapestry-5-and-Acegi-tf3769520.html#a10657302
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Andreas Andreou - andyhot@apache.org - http://andyhot.di.uoa.gr
Tapestry / Tacos developer
Open Source / JEE Consulting

Re: Tapestry 5 and Acegi

Posted by petros <pe...@cypoz.com>.
Ok... I don't have the tapestry-hibernate.jar in my classpath. 
Please send me an email to petros.petrou@glintech.com and I will reply with
my files that are relevant to your question.

Petros


Joshua Jackson-3 wrote:
> 
> Hi Petros
> 
> Here's my response:
>> 1. Are you using a database?
> Yes I will be using a database for the user and role
> 
>> 2. Are you using an ORM framework -> Hibernate?
> Yes I will. But for the Injection to my Page I am using
> tapestry-hibernate module
> 
>> 3. What do you refer to when you say "not use spring-hibernate"
> The spring-hibernate.jar
> 
>> If you don't use a database you could have a quick read of section 6.2.1
>> of
>> the acegi guide
>> http://www.acegisecurity.org/docbook/acegi.html
>>
> 
> Thanks in advance
> 
> -- 
> YM!: thejavafreak
> Blog: http://www.nagasakti.or.id/roller/joshua/
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Tapestry-5-and-Acegi-tf3769520.html#a10887657
Sent from the Tapestry - User mailing list archive at Nabble.com.


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


Re: Tapestry 5 and Acegi

Posted by Joshua Jackson <jo...@gmail.com>.
Hi Petros

Here's my response:
> 1. Are you using a database?
Yes I will be using a database for the user and role

> 2. Are you using an ORM framework -> Hibernate?
Yes I will. But for the Injection to my Page I am using
tapestry-hibernate module

> 3. What do you refer to when you say "not use spring-hibernate"
The spring-hibernate.jar

> If you don't use a database you could have a quick read of section 6.2.1 of
> the acegi guide
> http://www.acegisecurity.org/docbook/acegi.html
>

Thanks in advance

-- 
YM!: thejavafreak
Blog: http://www.nagasakti.or.id/roller/joshua/

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


Re: Tapestry 5 and Acegi

Posted by petros <pe...@cypoz.com>.
As a quick reply to your question, I'll say yes you do not have to use
hibernate but in order to give you more helpful information can you please
give me a bit more information about your environment?
1. Are you using a database?
2. Are you using an ORM framework -> Hibernate?
3. What do you refer to when you say "not use spring-hibernate"

If you don't use a database you could have a quick read of section 6.2.1 of
the acegi guide
http://www.acegisecurity.org/docbook/acegi.html

Petros


Joshua Jackson-3 wrote:
> 
> Hi Petros,
> 
> Thanks for the quick reply.
> Darn :-D I was hoping that you didn't use Spring at all to configure
> acegi :)) If I use Spring, that means I will have two IoC container.
> Anywa, would it be possible just to use Spring to configure acegi and
> not use spring-hibernate for the data access?
> 
> On 5/31/07, petros <pe...@cypoz.com> wrote:
>>
>> Joshua,
>>
>> I used Spring and the following two classes
>>
>> org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
>> org.springframework.transaction.interceptor.TransactionInterceptor
>>
>> and my UserManager bean extends the acegi
>> org.acegisecurity.userdetails.UserDetailsService
>>
>> Please let me know if you want to see my security.xml and the
>> springContext.xml files
>>
> 
> -- 
> YM!: thejavafreak
> Blog: http://www.nagasakti.or.id/roller/joshua/
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Tapestry-5-and-Acegi-tf3769520.html#a10887085
Sent from the Tapestry - User mailing list archive at Nabble.com.


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


Re: Tapestry 5 and Acegi

Posted by Joshua Jackson <jo...@gmail.com>.
Hi Petros,

Thanks for the quick reply.
Darn :-D I was hoping that you didn't use Spring at all to configure
acegi :)) If I use Spring, that means I will have two IoC container.
Anywa, would it be possible just to use Spring to configure acegi and
not use spring-hibernate for the data access?

On 5/31/07, petros <pe...@cypoz.com> wrote:
>
> Joshua,
>
> I used Spring and the following two classes
>
> org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
> org.springframework.transaction.interceptor.TransactionInterceptor
>
> and my UserManager bean extends the acegi
> org.acegisecurity.userdetails.UserDetailsService
>
> Please let me know if you want to see my security.xml and the
> springContext.xml files
>

-- 
YM!: thejavafreak
Blog: http://www.nagasakti.or.id/roller/joshua/

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


Re: Tapestry 5 and Acegi

Posted by miGueL25 <fo...@yahoo.fr>.
Hello, if possible i will want to see ur  security.xml and the
springContext.xml files. the i try to use the example, but i have a lot of
exceptions. And how can i configure this files when the using database is
hibernate instead of spring


Petros Petrou wrote:
> 
> Joshua, 
> 
> I used Spring and the following two classes
> 
> org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
> org.springframework.transaction.interceptor.TransactionInterceptor
> 
> and my UserManager bean extends the acegi
> org.acegisecurity.userdetails.UserDetailsService
> 
> Please let me know if you want to see my security.xml and the
> springContext.xml files
> 
> Petros
> 
> 
> Joshua Jackson-3 wrote:
>> 
>> Hi Petros. Thanks for the contribution since I need this as well. But
>> my question is, did you use Spring to configure Acegi? Or did you use
>> tapestry-ioc to configure it? If you use tapestry-ioc, can I take a
>> look at your module for configuring acegi?
>> 
>> Thanks in advance
>> 
>> On 5/17/07, petros <pe...@cypoz.com> wrote:
>>>
>>> Hi
>>>
>>> I integrated T5.0.4 with Acegi and I wanted to share my code with this
>>> forum.
>>> The solution may not be the best approach but it could save you sometime
>>> for
>>> now.
>>> I am only providing the security related code.
>>> ...
>>> Update Acegi Security Context
>>> I also have this code where the user's details are updated in order to
>>> keep
>>> the Acegi Principal up to date.
>>> ...
>>> Petros
>>>
>> 
>> 
>> -- 
>> YM!: thejavafreak
>> Blog: http://www.nagasakti.or.id/roller/joshua/
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Tapestry-5-and-Acegi-tp10657302p20088714.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


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


Re: Tapestry 5 and Acegi

Posted by petros <pe...@cypoz.com>.
Joshua, 

I used Spring and the following two classes

org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
org.springframework.transaction.interceptor.TransactionInterceptor

and my UserManager bean extends the acegi
org.acegisecurity.userdetails.UserDetailsService

Please let me know if you want to see my security.xml and the
springContext.xml files

Petros


Joshua Jackson-3 wrote:
> 
> Hi Petros. Thanks for the contribution since I need this as well. But
> my question is, did you use Spring to configure Acegi? Or did you use
> tapestry-ioc to configure it? If you use tapestry-ioc, can I take a
> look at your module for configuring acegi?
> 
> Thanks in advance
> 
> On 5/17/07, petros <pe...@cypoz.com> wrote:
>>
>> Hi
>>
>> I integrated T5.0.4 with Acegi and I wanted to share my code with this
>> forum.
>> The solution may not be the best approach but it could save you sometime
>> for
>> now.
>> I am only providing the security related code.
>> ...
>> Update Acegi Security Context
>> I also have this code where the user's details are updated in order to
>> keep
>> the Acegi Principal up to date.
>> ...
>> Petros
>>
> 
> 
> -- 
> YM!: thejavafreak
> Blog: http://www.nagasakti.or.id/roller/joshua/
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Tapestry-5-and-Acegi-tf3769520.html#a10886654
Sent from the Tapestry - User mailing list archive at Nabble.com.


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


Re: Tapestry 5 and Acegi

Posted by Joshua Jackson <jo...@gmail.com>.
Hi Petros. Thanks for the contribution since I need this as well. But
my question is, did you use Spring to configure Acegi? Or did you use
tapestry-ioc to configure it? If you use tapestry-ioc, can I take a
look at your module for configuring acegi?

Thanks in advance

On 5/17/07, petros <pe...@cypoz.com> wrote:
>
> Hi
>
> I integrated T5.0.4 with Acegi and I wanted to share my code with this
> forum.
> The solution may not be the best approach but it could save you sometime for
> now.
> I am only providing the security related code.
> ...
> Update Acegi Security Context
> I also have this code where the user's details are updated in order to keep
> the Acegi Principal up to date.
> ...
> Petros
>


-- 
YM!: thejavafreak
Blog: http://www.nagasakti.or.id/roller/joshua/

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