added AuthenticationFilter
This commit is contained in:
@ -20,6 +20,13 @@
|
|||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>log4j</groupId>
|
||||||
|
<artifactId>log4j</artifactId>
|
||||||
|
<version>1.2.17</version>
|
||||||
|
<type>jar</type>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax</groupId>
|
<groupId>javax</groupId>
|
||||||
<artifactId>javaee-api</artifactId>
|
<artifactId>javaee-api</artifactId>
|
||||||
@ -35,8 +42,8 @@
|
|||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>2.3.2</version>
|
<version>2.3.2</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.6</source>
|
<source>1.7</source>
|
||||||
<target>1.6</target>
|
<target>1.7</target>
|
||||||
<compilerArguments>
|
<compilerArguments>
|
||||||
<endorseddirs>${endorsed.dir}</endorseddirs>
|
<endorseddirs>${endorsed.dir}</endorseddirs>
|
||||||
</compilerArguments>
|
</compilerArguments>
|
||||||
|
|||||||
@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package de.muehlencord.shared.jeeutil;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.FilterConfig;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletOutputStream;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
import org.apache.log4j.Level;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Jörn Mühlencord (<a href="mailto:joern@muehlencord.de">joern@muehlencord.de</a>
|
||||||
|
*/
|
||||||
|
public class AuthenticationFilter implements Filter {
|
||||||
|
|
||||||
|
private final static Logger logger = Logger.getLogger(AuthenticationFilter.class.getName());
|
||||||
|
private final static String USER = AuthenticationFilter.class.getName() + "_user";
|
||||||
|
private String loginPage;
|
||||||
|
private String errorPage;
|
||||||
|
private FilterConfig filterConfig;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(FilterConfig filterConfig) throws ServletException {
|
||||||
|
this.filterConfig = filterConfig;
|
||||||
|
if (filterConfig != null) {
|
||||||
|
errorPage = filterConfig.getInitParameter("error_page");
|
||||||
|
loginPage = filterConfig.getInitParameter("login_page");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||||
|
|
||||||
|
if ((loginPage == null) || (errorPage == null)) {
|
||||||
|
returnError(request, response, "AuthenticationFilter not properly configured! Contact Administrator.");
|
||||||
|
}
|
||||||
|
|
||||||
|
User currentUser = null;
|
||||||
|
HttpSession session = ((HttpServletRequest) request).getSession();
|
||||||
|
|
||||||
|
if (session != null) {
|
||||||
|
if (session.getAttribute(USER) != null) {
|
||||||
|
currentUser = (User) session.getAttribute(USER);
|
||||||
|
} else {
|
||||||
|
logger.debug("No active session found - going to force login");
|
||||||
|
filterConfig.getServletContext().getRequestDispatcher(loginPage).forward(request, response);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if (currentUser == null) {
|
||||||
|
logger.debug("No logged in user found - going to force login");
|
||||||
|
filterConfig.getServletContext().getRequestDispatcher(loginPage).forward(request, response);
|
||||||
|
} else {
|
||||||
|
logger.debug("User is authenticated, continue filter chain");
|
||||||
|
// user is authenticated, continue with filter chain
|
||||||
|
chain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
this.filterConfig = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void returnError(ServletRequest request, ServletResponse response, String errorMsg) {
|
||||||
|
response.setContentType("text/html");
|
||||||
|
|
||||||
|
|
||||||
|
try (
|
||||||
|
ServletOutputStream servletOutputStream = response.getOutputStream();
|
||||||
|
PrintStream ps = new PrintStream(servletOutputStream);
|
||||||
|
PrintWriter pw = new PrintWriter(ps)) {
|
||||||
|
|
||||||
|
pw.print("<html>");
|
||||||
|
pw.print("<head><title>Error</title></head>");
|
||||||
|
pw.print("<body>");
|
||||||
|
pw.print("<h1>");
|
||||||
|
pw.print(errorMsg);
|
||||||
|
pw.print("</h1>");
|
||||||
|
pw.print("</body");
|
||||||
|
pw.print("</html>");
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
logger.log(Level.ERROR, errorMsg, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static String readFirstLineFromFile(String path) throws IOException {
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
|
||||||
|
return br.readLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package de.muehlencord.shared.jeeutil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author jomu
|
||||||
|
*/
|
||||||
|
public interface Authenticator {
|
||||||
|
|
||||||
|
public User getUser ();
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package de.muehlencord.shared.jeeutil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author jomu
|
||||||
|
*/
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user