Il n'y a pas de fonctionnalité d'authentification inhérente dans JSF principal au-delà de la possibilité d'utiliser des éléments comme les attributs du composant rendered
orientés vers la sécurité basée sur les rôles.
Par défaut, une application JSF repose sur les mêmes mécanismes de sécurité gérés par conteneur que le composant Web qui la contient (JEE5 tutorial). Les frameworks tiers tels que Seam peuvent fournir des alternatives.
Si vous voulez ajouter votre propre sécurité d'application, un servlet filter est l'un des mécanismes les plus simples.
Ce filtre protège les ressources dans le répertoire restricted
tel que défini dans web.xml
:
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>restricted.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/restricted/*</url-pattern>
</filter-mapping>
La mise en œuvre de classe de filtre:
public class AuthenticationFilter implements Filter {
private FilterConfig config;
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
if (((HttpServletRequest) req).getSession().getAttribute(
AuthenticationBean.AUTH_KEY) == null) {
((HttpServletResponse) resp).sendRedirect("../restricted_login.faces");
} else {
chain.doFilter(req, resp);
}
}
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
public void destroy() {
config = null;
}
}
Un grain de connexion défini dans faces-config.xml
:
public class AuthenticationBean {
public static final String AUTH_KEY = "app.user.name";
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public boolean isLoggedIn() {
return FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().get(AUTH_KEY) != null;
}
public String login() {
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(
AUTH_KEY, name);
return "secret";
}
public String logout() {
FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
.remove(AUTH_KEY);
return null;
}
}
Le formulaire de connexion JSF dans le e restricted_login.jsp
page:
<f:view>
<p><a href="restricted/secret.faces">try to go to secret
page</a></p>
<h:form>
Username:
<h:panelGroup rendered="#{not authenticationBean.loggedIn}">
<h:inputText value="#{authenticationBean.name}" />
<h:commandButton value="login"
action="#{authenticationBean.login}" />
</h:panelGroup>
<h:commandButton value="logout"
action="#{authenticationBean.logout}"
rendered="#{authenticationBean.loggedIn}" />
</h:form>
</f:view>
(L'URL de redirection/mécanisme a été choisi par souci de concision plutôt que toute sorte de bonnes pratiques; voir le Servlet API pour plus d'options.)
Connexes: http://stackoverflow.com/questions/2206911/best-way-for-user-authentication-on-javaee-6-using-jsf-2-0/2207147#2207147 Ou si vous êtes ouvert aux bibliothèques tierces comme Shiro, puis http://balusc.blogspot.com/2013/01/apache-shiro-is-it-ready-for-java-ee-6.html – BalusC