What are jftfdi jffi doing to my query string? - jsf

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

Related

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

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

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.

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.

How to disable ViewState?

I'm coming into Java world from MS and ASP.NET and looking for the similar to ASP.NET component-based HTML framework in Java. After reviewing tons of links in internet it looks like JSF2 (with facelets) is best match (is this true by the way? or there are other better choices?).
The problem I'm encountering during evaluation right now is correct usage of JSF's view state. My final usage scenario would be a clustered WEB server and i'm NOT going to have any session/server-stored objects and i'm NOT going to use network bandwidth for dummy view state (see another guy's somewhat related problem here JSF Tuning).
I took some JSF2 tutorial and after setting javax.faces.STATE_SAVING_METHOD = client got ViewState generated into HTML of 440 chars (omygod, page contains just 1 dummy text input and 1 submit button). In "POST on submit" I do need only text from text input (10 chars) and not that dummy view state (440 chars).
So the question is - Is it possible to disable view state in JSF2?
Relevant links:
Use-case in ASP.NET - "Disable View State for a Page":
http://www.ironspeed.com/articles/Disable%20View%20State%20for%20a%20Page/Article.aspx
Not helpful answer on stackoverflow:
How to reduce javax.faces.ViewState in JSF
Update: Relevant links (from comments below):
JSF 2.0 partial state saving does not seem to work
"Stateless JSF": http://industrieit.com/blog/2011/11/stateless-jsf-high-performance-zero-per-request-memory-overhead
JSF is a component based framework which is heavily stateful - so you need the state somewhere, either sent to the client over the wire and posted in again, or on the server side. So AFAIK the answer is No, you cannot disable the View state. But you can minimize it - however some state will always need storing. This link is relevant.
If you're looking for a Java web framework which is not so stateful - then maybe look at some Action based framework like Struts or Stripes, so you can work in Request scope and not need a component tree present (or rebuilt) on a postback. The Play framework has been gaining good press - which is specifically designed to target RESTful architectures. I do not have experience of this myself, but you may want to investigate it. Taken from the Play website:
Simple stateless MVC architecture
You’ve got a database on one side and a web browser on the other. Why
should you have a state in between?
Stateful and component based Java Web frameworks make it easy to
automatically save page state, but that brings a lot of other
problems: what happens if the user opens a second window? What if the
user hits the browser back button?
Since Mojarra 2.1.19 and Mojarra 2.2.0-m10 it's possible to disable the state saving on a per-view basis by setting the transient attribute of <f:view> to true.
<f:view transient="true">
...
<h:form>
...
</h:form>
...
</f:view>
See also:
JSF is going stateless
What is the usefulness of statelessness in JSF?

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..

Resources