com.flowlogix.shiro.ee.cdi.ShiroSessionScopeExtension Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shiro-ee Show documentation
Show all versions of shiro-ee Show documentation
Flow Logix Jakarta EE Integration with Apache Shiro Security
The newest version!
/*
* Copyright (C) 2011-2024 Flow Logix, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.flowlogix.shiro.ee.cdi;
import com.flowlogix.shiro.ee.cdi.ShiroSecurityExtension.ShiroSecureAnnotated;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.WithAnnotations;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;
/**
* Entry point for Shiro Session scope CDI extension
*
* @author lprimak
*/
public class ShiroSessionScopeExtension implements Extension, Serializable {
private static final long serialVersionUID = 1L;
@SuppressWarnings("ConstantName")
private static final List contexts = Stream.of(
new ShiroScopeContext(ShiroSessionScoped.class, SessionScoped.class),
new ShiroScopeContext(ShiroFacesViewScoped.class, javax.faces.view.ViewScoped.class),
new ShiroScopeContext(ShiroOmniViewScoped.class, org.omnifaces.cdi.ViewScoped.class))
.collect(Collectors.toList());
@SessionScoped
@SuppressWarnings("serial")
private static final class SessionScopedAnnotated implements Serializable { }
@javax.faces.view.ViewScoped
@SuppressWarnings("serial")
private static final class FacesViewScopedAnnotated implements Serializable { }
@org.omnifaces.cdi.ViewScoped
@SuppressWarnings("serial")
private static final class OmniViewScopedAnnotated implements Serializable { }
@ShiroSessionScoped
@SuppressWarnings("serial")
private static final class ShiroSessionScopedAnnotated implements Serializable { }
@ShiroFacesViewScoped
@SuppressWarnings("serial")
private static final class ShiroFacesViewScopedAnnotated implements Serializable { }
@ShiroOmniViewScoped
@SuppressWarnings("serial")
private static final class ShiroOmniViewScopedAnnotated implements Serializable { }
/**
* intercept session destroy session listeners and destroy the beans
* @param sessionListeners
* @param sm
*/
public void addSessionListeners(Collection sessionListeners, SecurityManager sm) {
sessionListeners.add(new SessionListener() {
@Override
public void onStart(Session session) {
contexts.forEach(ctx -> ctx.onCreate(session));
}
@Override
public void onStop(Session session) {
contexts.forEach(ctx -> ctx.onDestroy(session));
}
@Override
public void onExpiration(Session session) {
onStop(session);
}
});
}
void addSessionScoped(@Observes @WithAnnotations(SessionScoped.class) ProcessAnnotatedType pat) {
pat.setAnnotatedType(new AnnotatedTypeWrapper<>(pat.getAnnotatedType(), true,
Set.of(ShiroSessionScopedAnnotated.class.getDeclaredAnnotations()[0],
ShiroSecureAnnotated.class.getDeclaredAnnotations()[0]),
Set.of(SessionScopedAnnotated.class.getDeclaredAnnotations()[0])));
}
void addFacesViewScoped(@Observes @WithAnnotations(javax.faces.view.ViewScoped.class) ProcessAnnotatedType pat) {
pat.setAnnotatedType(new AnnotatedTypeWrapper<>(pat.getAnnotatedType(), true,
Set.of(ShiroFacesViewScopedAnnotated.class.getDeclaredAnnotations()[0],
ShiroSecureAnnotated.class.getDeclaredAnnotations()[0]),
Set.of(FacesViewScopedAnnotated.class.getDeclaredAnnotations()[0])));
}
void addOmniViewScoped(@Observes @WithAnnotations(org.omnifaces.cdi.ViewScoped.class) ProcessAnnotatedType pat) {
pat.setAnnotatedType(new AnnotatedTypeWrapper<>(pat.getAnnotatedType(), true,
Set.of(ShiroOmniViewScopedAnnotated.class.getDeclaredAnnotations()[0],
ShiroSecureAnnotated.class.getDeclaredAnnotations()[0]),
Set.of(OmniViewScopedAnnotated.class.getDeclaredAnnotations()[0])));
}
void addScope(@Observes final BeforeBeanDiscovery event) {
contexts.forEach(ctx -> event.addScope(ctx.getScope(), true, true));
}
void registerContext(@Observes final AfterBeanDiscovery event) {
contexts.forEach(event::addContext);
}
}