wood burning stoves 2.0*
The moose likes Struts and the fly likes Struts 2 and authentication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 and authentication" Watch "Struts 2 and authentication" New topic
Author

Struts 2 and authentication

David Wimmer
Greenhorn

Joined: Jun 01, 2011
Posts: 13
Hello,

to check if the user is allowed to see the visited page, i wrote
an abstract class extending ActionSupport for doing the check for the user role in the system.
Every class which want to check for authentication has to extend this abstract class.

My problem is, that I cannot get access to the session in the abstract class "AbstractAction" and returns null, respectively.
















Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

You should look into implementing SessionAware on your abstract class.
David Wimmer
Greenhorn

Joined: Jun 01, 2011
Posts: 13
unfortunately had not worked.

Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

How did you implement it because if it didn't work you are doing it wrong.
David Wimmer
Greenhorn

Joined: Jun 01, 2011
Posts: 13
I let my AbstractAction class addinionally implement the SeesionAware and added the following method



To get the user object from the session I wrote in the constructor.

Hope I did this right.

Ps: This is the error I get


Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

Are you implementing SessionAware also in the parent class (so you'd be overriding it)?

Where is the null coming from? Session being null or user being null.
David Wimmer
Greenhorn

Joined: Jun 01, 2011
Posts: 13
My AbstractAction class is so to speak my parent-class and I extended SessionAware there. I did not implemet SessionAware in my "child" classes, because
there I can access to the session without any problems

The error occures while accessing the sesson, so the program does not even come to check whether
a "user"-object is existing.

Can it be possible that I have to register my AbstractAction class in the struts.xml.
Usually not, right? Because my AbstractAction is never called directly.
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

Sorry I did mean if you had overridden in your children class. No you should not have to register in the struts.xml. Mine works fine and I did everything exactly the same except for where you are trying to access the session. From the first set of code I do see that you are doing this in the constructor. I think there may be an issue there. You should probably instead use Preparable and put the things in your constructor in the prepare method instead.

-JM
David Wimmer
Greenhorn

Joined: Jun 01, 2011
Posts: 13
I did not implement the setSession-Method in the child class, because it works there without passing the session explicitly.

Now I read over the code again and I saw that there's a conflict.
Typically, a session will be created when the user data are correct.
But when I first check whether a user is allowed to see this page at all, but this is not logged in, then there could be no session.

David Wimmer
Greenhorn

Joined: Jun 01, 2011
Posts: 13
Got it to work, but I have an additional question:



Is there a way to check the permissions in simple actions that implement any Java class?
In the example above, I would not view the registration page when a user is already logged in.
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

Im sure there are many ways, I'd probably start by looking at using a custom interceptor.
David Wimmer
Greenhorn

Joined: Jun 01, 2011
Posts: 13
Thanks for your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 2 and authentication
 
Similar Threads
Struts2 FileUpload - request is nulled
Populate JavaBean With Data Fetched From the Database
open a pdf inline using MIME in ie7
How to redirect to same page in struts 2
execute method of ActionSupport Class