Moved AuthenticationFilter to Octagon

This commit is contained in:
Robert Vokac 2023-10-05 19:56:29 +02:00
parent b2ab35f1c1
commit 7b59ed7aab
Signed by: robertvokac
GPG Key ID: 693D30BEE3329055
4 changed files with 27 additions and 84 deletions

View File

@ -162,6 +162,13 @@
<!-- <artifactId>log4j-slf4j-impl</artifactId>-->
<!-- <version>2.11.2</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.nanoboot.octagon</groupId>
<artifactId>octagon-jakarta</artifactId>
<version>${octagon.version}</version>
</dependency>
</dependencies>
<repositories>

View File

@ -1,84 +0,0 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Nanodata.
// Copyright (C) 2023-2023 the original author or authors.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; version 2
// of the License only.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package org.nanoboot.nanodata.web.filters;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
public class AuthenticationFilter implements Filter {
private ServletContext context;
public void init(FilterConfig fConfig) throws ServletException {
this.context = fConfig.getServletContext();
this.context.log("AuthenticationFilter started");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession(false);
boolean sessionExists = session != null;
String octConfpath = System.getProperty("nanodata.confpath");
if (octConfpath == null || octConfpath.isEmpty()) {
String msg = "csa configuration is broken : " + "nanodata.confpath=" + octConfpath;
throw new RuntimeException(msg);
}
File octagonProperties = new File(octConfpath + "/nanodata.properties");
try ( InputStream input = new FileInputStream(octagonProperties.getAbsolutePath())) {
Properties properties = new Properties();
properties.load(input);
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException("Loading nanodata.properties failed.");
}
if (!sessionExists) {
res.sendRedirect(req.getContextPath() + "/login.html");
this.context.log("Access is not authorized.");
} else {
this.context.log("Access is authorized.");
chain.doFilter(request, response);
}
}
public void destroy() {
//close any resources here
}
}

View File

@ -73,6 +73,16 @@
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>org.nanoboot.octagon.jakarta.filters.AuthenticationFilter</filter-class>
</filter>
<!-- <filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>-->
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
@ -80,4 +90,13 @@
</jsp-property-group>
</jsp-config>
<context-param>
<param-name>octagon.application-code</param-name>
<param-value>nanodata</param-value>
</context-param>
<listener>
<listener-class>org.nanoboot.octagon.jakarta.listeners.ApplicationCodeListener</listener-class>
</listener>
</web-app>

View File

@ -50,6 +50,7 @@
<power.version>2.0.1-SNAPSHOT</power.version>
<checkstyle.skip>true</checkstyle.skip><!-- TODO: make false-->
<db-migration-core.version>0.1.1-SNAPSHOT</db-migration-core.version>
<octagon.version>2.0.0-SNAPSHOT</octagon.version>
</properties>
<build>