How to protect your page using WebFilter in JavaEE
Tech-Today

How to protect your page using WebFilter in JavaEE


This tutorial is to be use in conjunction with picketlink. Normally we want some pages to be accessible only after a user has logged in. In this case we need a real protection filter.

The class below filters a url path and check if there's a logged in user.

package com.czetsuya.listener;

import java.io.IOException;

import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.picketlink.Identity;

@WebFilter(urlPatterns = RealmProtectionFilter.REALM_BASE_URI + "/*")
public class RealmProtectionFilter implements Filter {

public static final String REALM_BASE_URI = "/pages/secured";

@Inject
private Instance identityInstance;

private Identity getIdentity() {
return this.identityInstance.get();
}

@Override
public void destroy() {

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;

boolean isAuthorized = getIdentity().isLoggedIn();

if (isAuthorized) {
chain.doFilter(httpRequest, httpResponse);
} else {
forwardAccessDeniedPage(httpRequest, httpResponse);
}
}

private void forwardAccessDeniedPage(HttpServletRequest httpRequest,
HttpServletResponse httpResponse) throws ServletException,
IOException {
httpRequest.getServletContext()
.getRequestDispatcher("/error/accessDenied.jsf")
.forward(httpRequest, httpResponse);
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

}

The url /pages/secured is validated, if no we redirect to /error/accessDenied.jsf.




- Social Login Using Rest Fb
This is an implementation tutorial on how we can use REST FB to enable facebook social login on our web application. Basically, it's a project created from javaee7-war template. To run this app you need to set up a Facebook application with callback...

- Do Some Initialization Work After The Web Application Initialization Process
We can achieve this by using a webListener. See code below: package com.czetsuya; import java.text.MessageFormat; import java.util.ResourceBundle; import javax.inject.Inject; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;...

- How To Receive An Asynchronous Jms Message
This tutorial assumes that you have glassfish installed and followed the 2 previous tutorials: http://czetsuya-tech.blogspot.com/2012/06/how-to-setup-set-jms-factory-and-queue.html http://czetsuya-tech.blogspot.com/2012/06/how-to-send-jms-message-to-jms-queue.html...

- Using Ejb3.1 As The Backing Bean For Jsf Using Cdi (jsr-330)
This tutorial assumes that you already have knowledge on EJB, JSF, maven and injection. These are the steps as well as the code I use to make it work. 1.) Create 2 maven projects (web, ejb - has HelloBean class) 2.) 2.) In the web part we need to create...

- Creating Your First Enterprise Application Project On Eclipse-jee-helios
First you should create an ejb project as specified here: http://czetsuya-tech.blogspot.com/2011/03/creating-your-first-ejb3-project-in.html Take note that my interface were inside the ejb project. You can pull that out and place in its own project ipielEJB2Client....



Tech-Today








.