Performing user authentication in Java EE / JSF / EJB, on JBoss [duplicate] - security

Currently, I am working on a web project using JSF 2.0, Tomcat 7 and MongoDB. I have a big question of how to handle the session management and authentication/authorization with users in a database.
The structure I want is as follows: only logged in users can create events and everyone can see the created events.
create.xhtml --> only for logged in users.
events.xhtml --> public for everyone.
The basic structure I'm planning is:
Check if the page requires logged in user (e.g. create.xhtml)
If yes, check if user is logged in
If user is not logged in, go to login.xhtml
If successfully logged in, come back to requested page
Keep the "User is logged in" information unless user clicks log out
button. (there I guess #SessionScoped gets into play)
The question is:
What is the less complicated way of doing this?
Where should I use the #SessionScoped annotation? In Create.java or
LoginManager.java?
Spring security looks kind of complicated for my issue, do I really
need it? if yes, can you explain a little bit of how the implementation works together with JSF 2.0 and Mongo DB?

There are several options. Which to choose is fully up to you. Just objectively weigh the concrete advantages and disadvantages conform your own situation.
1. Use Java EE provided container managed authentication
Just declare a <security-constraint> in web.xml which refers a security realm which is configured in servletcontainer. You can for your webapp specify URL pattern(s) which should be checked for login and/or role(s), e.g. /secured/*, /app/*, /private/*, etc.
Before Java EE 8, you unfortunately still need to configure a security realm in a servletcontainer-specific way. It's usually described in servletconainer-specific documentation. In case of Tomcat 8, that's the Realm HOW-TO. For example, a database based realm based on users/roles tables is described in section "JDBCRealm".
Since Java EE 8, there will finally be a standard API based on JSR-375.
Advantages:
Relatively quick and easy to setup and use.
Since Java EE 8 there's finally a robust and flexible standard API.
Disadvantages:
Before Java EE 8, realm configuration is container-specific. In Java EE 8, the new JSR-375 Security Spec should solve that with help of JASPIC.
Before Java EE 8, , there is no fine grained control.
Before Java EE 8, it's very spartan; no "remember me", poor error handling, no permission based restriction.
See also:
Performing user authentication in Java EE / JSF using j_security_check - contains complete code examples
Java EE kickoff application - example web application (developed by me) which also demonstrates Java EE 8 authentication with Soteria (the JSR-375 RI).
2. Homegrow a servlet filter
This allows for much more fine grained control, but you're going to need to write all the code yourself and you should really know/understand how you should implement such a filter to avoid potential security holes. In JSF side, you could for example just put the logged-in user as a session attribute by sessionMap.put("user", user) and check in the filter if session.getAttribute("user") is not null.
Advantages:
Fine grained control.
Completely container independent.
Disadvantages:
Reinvention of the wheel; new features require a lot of code.
As starter, you're never sure if your code is 100% robust.
See also:
Is there any easy way to preprocess and redirect GET requests? - contains introducory explanation and kickoff example for authentication
Authorization redirect on session expiration does not work on submitting a JSF form, page stays the same - contains more extended kickoff example for authentication which also covers ajax requests
How control access and rights in JSF? - contains kickoff example for authorization
3. Adapt a 3rd party framework
For example, Apache Shiro, Spring Security, etc. This offers usually much more fine grained configuration options than standard container managed authentication and you don't need to write any code for this yourself, expect of the login page and some (XML) configuration of course.
Advantages:
Fine grained control.
Completely container independent.
No reinvention of the wheel; minimum of own code.
Thoroughly developed and tested by lot of users, so most likely 100% robust.
Disadvantages:
Some learning curve.
See also:
JSF2 - Shiro tutorial - an extensive tutorial on integrating Shiro in JSF2 webapp

Related

Advantages of using JSF Faces Flow instead of the normal navigation system

I'm exploring the JSF 2.2 Faces Flow feature but I'm still not sure what are the advantages of defining a flow using Faces Flow instead of using the normal navigation system (calling facelets in links or buttons)?
Only use it if you want to use a #FlowScoped bean on a given set of views. In other words, only use it if you want a managed bean which should live as long as you're navigating through the specified collection of views registered in the flow configuration file.
There are indeed very few real world use cases for this. They all boil down to a multi-step wizard of which each step is bookmarkable. Previously, before the introduction of the flow scope, one would use conditionally rendered includes for this, but they are in turn not individually bookmarkable, because the URL stays the same all the time.
See also:
What is new in JSF 2.2? - Faces Flow
Java EE 7 tutorial - Using Faces Flows
How to navigate in JSF? How to make URL reflect current page (and not previous one)
Faces flow and navigation are different.
Face flow like business flow in the frond end site, much like wizard.

What are jftfdi jffi doing to my query string?

We are using JavaServer Faces 2.2 (Mojarra 2.2.1) in our project. I noticed something odd. On a page called reporting.xhtml where I use f:metadata with the new f:viewAction my browser, Safari in this case, shows the following query string:
reporting.jsf?jftfdi=&jffi=reporting%3Ffaces-redirect%3Dtrue
What wizardry is this? What are the parameters jftfdi and jiffi doing? What is their purpose?
It's part of the new JSF 2.2 feature as described by spec issue 949. Basically, it enables JSF to identify the client window. It's basically the same as cid in CDI's #ConversationScoped and windowId in CODI's #ViewScoped/#ViewAccessScoped. This client window ID is in turn used by among others the new JSF 2.2 #FlowScoped scope as described by spec issue 730.
The "What's new in JSF 2.2?" article of my fellow Arjan Tijms explains the need pretty clearly:
LifeCycle
Identify client windows via a Window Id
Arguably one of the biggest problems that has been plaguing web application development since its inception is the inability to distinguish requests originating from different windows of a single browser. Not only has an actual solution been long overdue, it has taken a long time to realize this even was a problem.
The root of the problem, as always, is that the HTTP protocol is inherently stateless while applications in general are not. There is the concept of a cookie though, which is overwhelmingly the mechanism used to distinguish requests from different users and to implement things like a session scope where on its turn the bulk of login mechanisms are based on.
While a cookie does work for this, it’s global per browser and domain. If a user opens multiple tabs or windows for the same domain then requests from those will all send the same cookie to the server. Logging in as a different user in a different window for the same website is thus not normally possible, and having workflows (involving post-backs, navigation) in different windows can also be troublesome because of this.
In JSF there are various solutions that are somehow related to this. The view scope effectively implements a session per window as long as the user stays on the same page and does only post-backs. The Flash is used for transferring data between different pages (presumably within the same window) when navigation is done via Redirect/GET. There’s a wide variety of scopes implemented by third parties that do something similar.
All of these have some implicit notion or assumption of the concept of a ‘client window’, but there is no explicit API for this.
JSF 2.2 will introduce support for two different aspects of this:
Identification of an individual window: the Client Window Id
API and life-cyle awareness of the window concept
Apparently you've configured your application as such.
See also:
What's new in JSF 2.2? - Lifecycle - Identify client windows via window Id
What's new in JSF 2.2? - Navigation - Faces Flow

Concept of JSF, EJB and form based login with JDBC-Realm

I am trying to learn the concepts of Java EE (EJB,JSF...) and therefore I am working on an example application.
Unfortunately I have problems to understand how some concepts should work together and if I am doing it in an correct professional manner. At this point, I am really confused about all these different methods and hope someone can help me out.
The core functionality of my application consists of a document server where registered users can upload documents and describe it with useful information.
The Documents should simply be saved on the Server and all Information should be stored in a MySQL Database.
I created three Projects with Netbeans.
Enterprise Application Project (DocApp)
EJB Module (DocApp-ejb)
and a Web Application Project (DocApp-war).
The main things work fine like
accessing the database with JPA
uploading files with primefaces FileUploader
injecting JSF with EJB
and even the user authorization with JDBC-Realm as shown in this tutorial
http://jugojava.blogspot.de/2011/02/jdbc-security-realm-with-glassfish-and.html
My Problem now is, that all pages in a specific subdirectory should only be accessible by registered users.
The only way i see is to use one SessionScoped ManagedBean, instead of using multiple RequestScoped ManagedBeans .
This seems to be a bad practice but I have no Idea how to handle this otherwise.
The way i understand it, there should be one ManagedBeand for every JSF Page (xhtml).
Is there a good way to handle this or am i doing anything wrong?
The default mechanism to give access to a whole sub directory is adding a security constraint in web.xml for the URL pattern representing that directory.
Every registered user should get a role that represents being registered, eg "REGISTERED"
This role is then added to the security constraint in web.xml.
The interaction between JSF and the Servlet container managed security is a little awkward, but it does work.

Securing JSF applications

I've been asked by a freelancer friend of mine to join him on a JSF 2.0 project, and I'm slowly picking up speed and putting the pieces together. Coming from a Windows Forms .NET world, I have a lot to learn to say the least.
My major concern is with the lack of apparent consensus on how to protect a JSF application.
Some methods have been proposed here on SO, including using Spring security, Seam security, custom phase listeners, or simply using the rendered="#{...}" attribute to show/hide components based on user authentication.
I have tried to implement some of these methods, for example Spring security, only to find out that it gets easily defeated by the JSF navigation mechanism that forwards to views instead of redirecting. In other words, Spring security will work fine if the user types in the url of a secured page directly, but not if a h:commandButton's action takes him there.
In view of this, some have suggested to force a redirect by using "faces-redirect=true", but we feel that this could become a performance issue as this causes 2 requests from the browser each time.
On the other hand, I gave up trying to implement Seam security after getting so many missing dependencies errors.
The best solution I have found so far is a custom phase listener from Duncan Mills - Effective Page Authorization In JavaServer Faces, but I'm not 100% convinced this should be used on public facing JSF applications.
So finally, what does this leave us with ? I know this is a pretty wide open ended question, but I honestly have no clue where to go next. I'm pretty sure I have followed the different tutorials to the letter, for example Spring tutorials, but I'm still not satisfied with the way it works.
Could anyone at least confirm/infirm the fact that Spring security is supposed to work across JSF forwards, as I've seen many posts by others having the same issue ? That would at least give me a direction to keep going.
Thank you.
Combination of servlet filter for page validation (applied to the faces servlet), identity session bean (storing user attributes e.g. Role, login id) and a few methods for entitlement checks (e.g. isAdmin(), canViewRecord(recordID)) well ised throughout your page.
You see, when it comes to security I opt for not leaving it in anybody else's hand. also, I validate in several places (hiding a component won't keep folks from forging the right POST request to trigger specific bean methods so watch out).
When I work with JSF I use spring-security.
About the behavior that you comment that spring security allows redirections done with commands button, is weird you must have a wrong configuration because it seams working fine in my project (I just tested).
In any case you can also use the spring security tags to render or not components according to the user's role.
This is a project that can help you to implement the tags.
http://www.dominikdorn.com/facelets/
Hope this helps..

GWT Spring security - Client

I have managed to integrate spring security to my GWT application using the following approach:
http://technowobble.blogspot.com/2010_05_01_archive.html
Note that I used the gwtrpcspring library for basic spring integration with GWT and it works well (non-invasive and efficient).
My question for which I can't seem to find the answer for is how do I use spring security in my GWT widgets based on Authorization - for example, I wouldn't want to show a menu item or a save option to a user who doesn't have the authority for these. Of course, my methods are protected at the server side, but I wouldn't want a user to click on something only then to find out they have no access to it. I don't want to litter my presentation logic with security related code (if-else blocks) if possible.
Is there something equivalent of the spring security jsp tag library that can be used within GWT client code? Or do we have to build up something on our own?
No - there is no equivalentand what I have done is setup a user role profile and passed this over at login which is in then published to the presenters that handle accordingly..
Ian
Everything you need is just to add Spring Security framework into your project.
You can ask server using RPC call at application start (after successful login) which user is current user and which roles he has.
I offer you to use my project hosted on https://code.google.com/p/gspring for simple and clear GWT and Spring integration.

Resources