com.github.lontime.extpac4j.provider.AbstractProvider Maven / Gradle / Ivy
The newest version!
package com.github.lontime.extpac4j.provider;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.github.lontime.base.commonj.components.AbstractComponentLifecycle;
import com.github.lontime.base.commonj.components.ComponentInterfaceHelper;
import com.github.lontime.base.commonj.utils.StringHelper;
import com.github.lontime.extjson.JSONInstance;
import com.github.lontime.extpac4j.config.PropertiesConfigExtFactory;
import com.github.lontime.extpac4j.configuration.CallbackOption;
import com.github.lontime.extpac4j.configuration.LogoutOption;
import com.github.lontime.extpac4j.configuration.OptionResolver;
import com.github.lontime.extpac4j.configuration.Options;
import com.github.lontime.extpac4j.impl.BasicUserProfileReader;
import com.github.lontime.extpac4j.sql.DbProfile;
import org.pac4j.core.config.Config;
import org.pac4j.core.context.JEEContext;
import org.pac4j.core.context.JEEContextFactory;
import org.pac4j.core.context.WebContextFactory;
import org.pac4j.core.context.session.JEESessionStore;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.core.engine.CallbackLogic;
import org.pac4j.core.engine.DefaultCallbackLogic;
import org.pac4j.core.engine.DefaultLogoutLogic;
import org.pac4j.core.engine.DefaultSecurityLogic;
import org.pac4j.core.engine.LogoutLogic;
import org.pac4j.core.engine.SecurityGrantedAccessAdapter;
import org.pac4j.core.engine.SecurityLogic;
import org.pac4j.core.engine.decision.AlwaysUseSessionProfileStorageDecision;
import org.pac4j.core.http.adapter.HttpActionAdapter;
import org.pac4j.core.http.adapter.JEEHttpActionAdapter;
import org.pac4j.core.profile.AnonymousProfile;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.core.profile.ProfileManager;
import org.pac4j.core.profile.UserProfile;
import org.pac4j.core.profile.factory.ProfileManagerFactory;
import org.pac4j.core.util.FindBest;
/**
* AbstractProvider.
*
* @author lontime
* @since 1.0
*/
public abstract class AbstractProvider extends AbstractComponentLifecycle implements Provider {
private Options options;
private Config config;
public AbstractProvider() {
this.options = OptionResolver.getInstance().getOptions();
}
@Override
public void initialize() {
initReader();
this.config = createConfig();
}
protected void initReader() {
JSONInstance.get().withReader(CommonProfile.class, new BasicUserProfileReader<>())
.withReader(AnonymousProfile.class, new BasicUserProfileReader<>())
.withReader(DbProfile.class, new BasicUserProfileReader<>());
}
private Config createConfig() {
final PropertiesConfigExtFactory factory =
new PropertiesConfigExtFactory(options.getCallbackUrl(), options.getProperties());
final Config config = factory.build();
if (options.getDefaultSecurityClients() != null) {
config.getClients().setDefaultSecurityClients(options.getDefaultSecurityClients());
}
if (options.getAlwaysUseSession()) {
final DefaultSecurityLogic, ?> securityLogic = DefaultSecurityLogic.INSTANCE;
securityLogic.setProfileStorageDecision(new AlwaysUseSessionProfileStorageDecision<>());
config.setSecurityLogic(securityLogic);
}
if (StringHelper.hasText(options.getProfileStorageDecisionName())) {
ComponentInterfaceHelper.getOnly(ProfileStorageDecisionInterface.class, options.getProfileStorageDecisionName())
.map(s -> {
final DefaultSecurityLogic, ?> securityLogic = DefaultSecurityLogic.INSTANCE;
securityLogic.setProfileStorageDecision(s);
return securityLogic;
}).ifPresent(s -> config.setSecurityLogic(s));
}
return config;
}
@Override
@SuppressWarnings("all")
public R security(HttpServletRequest request, HttpServletResponse response,
SecurityGrantedAccessAdapter accessAdapter,
HttpActionAdapter localAdapter,
SessionStore localSessionStore,
WebContextFactory localContextFactory,
SecurityLogic localSecurityLogic) {
final SessionStore bestSessionStore = FindBest.sessionStore(localSessionStore,
config, JEESessionStore.INSTANCE);
final HttpActionAdapter bestAdapter =
FindBest.httpActionAdapter(localAdapter, config, JEEHttpActionAdapter.INSTANCE);
final SecurityLogic bestLogic = FindBest.securityLogic(
localSecurityLogic, config, DefaultSecurityLogic.INSTANCE);
final JEEContext context = (JEEContext) FindBest.webContextFactory(localContextFactory, config,
JEEContextFactory.INSTANCE).newContext(request, response, bestSessionStore);
return bestLogic.perform(context, config, accessAdapter, bestAdapter,
options.getClients(), options.getAuthorizers(), options.getMatchers(), options.getMultiProfile());
}
@Override
@SuppressWarnings("all")
public void logout(final HttpServletRequest request, final HttpServletResponse response,
HttpActionAdapter
© 2015 - 2024 Weber Informatics LLC | Privacy Policy