Filter for JAX-RS / EE MVC applications for redirecting unauthenticated users to the login application

Jens Pelzetter 2020-09-19 17:41:00 +02:00
parent 5685edafb2
commit db5734f3f7
3 changed files with 90 additions and 6 deletions

View File

@ -0,0 +1,69 @@
/*
* Copyright (C) 2020 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.ui;
import org.libreccm.security.Shiro;
import java.io.IOException;
import java.net.URI;
import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@PreMatching
public class IsAuthenticatedFilter implements ContainerRequestFilter {
@Inject
private ServletContext servletContext;
@Inject
private Shiro shiro;
@Override
public void filter(final ContainerRequestContext requestContext)
throws IOException {
if (!shiro.getSubject().isAuthenticated()) {
final String contextPath = servletContext.getContextPath();
final String returnUrl = requestContext
.getUriInfo()
.getRequestUri()
.getPath();
requestContext.abortWith(
Response.temporaryRedirect(
URI.create(
String.format(
"/%s/ccm/register?return_url=%s",
contextPath,
returnUrl
)
)
).build()
);
}
}
}

View File

@ -18,8 +18,9 @@
*/
package org.libreccm.ui.admin;
import org.libreccm.ui.IsAuthenticatedFilter;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
@ -40,11 +41,19 @@ public class AdminApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
return adminPages
.stream()
.map(AdminPage::getControllerClasses)
.flatMap(controllers -> controllers.stream())
.collect(Collectors.toSet());
final Set<Class<?>> classes = new HashSet<>();
classes.add(IsAuthenticatedFilter.class);
classes.addAll(
adminPages
.stream()
.map(AdminPage::getControllerClasses)
.flatMap(controllers -> controllers.stream())
.collect(Collectors.toSet())
);
return classes;
// final Set<Class<?>> classes = new HashSet<>();
// classes.add(SystemInformationController.class);

View File

@ -18,6 +18,10 @@
*/
package org.libreccm.ui.admin.systeminformation;
import org.libreccm.core.CoreConstants;
import org.libreccm.security.AuthorizationRequired;
import org.libreccm.security.RequiresPrivilege;
import javax.enterprise.context.RequestScoped;
import javax.mvc.Controller;
import javax.ws.rs.GET;
@ -34,6 +38,8 @@ public class SystemInformationController {
@GET
@Path("/")
@AuthorizationRequired
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
public String getSystemInformation() {
return "org/libreccm/ui/admin/systeminformation.xhtml";
}