本文整理汇总了Java中org.apache.shiro.web.env.WebEnvironment类的典型用法代码示例。如果您正苦于以下问题:Java WebEnvironment类的具体用法?Java WebEnvironment怎么用?Java WebEnvironment使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WebEnvironment类属于org.apache.shiro.web.env包,在下文中一共展示了WebEnvironment类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getWebEnvironment
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
/**
* Find the Shiro {@link WebEnvironment} for this web application.
*
* @param sc ServletContext to find the web application context for
* @param attrName the name of the ServletContext attribute to look for
* @return the desired WebEnvironment for this web app, or <code>null</code> if none
* @since 1.2
*/
public static WebEnvironment getWebEnvironment(ServletContext sc, String attrName) {
if (sc == null) {
throw new IllegalArgumentException("ServletContext argument must not be null.");
}
Object attr = sc.getAttribute(attrName);
if (attr == null) {
return null;
}
if (attr instanceof RuntimeException) {
throw (RuntimeException) attr;
}
if (attr instanceof Error) {
throw (Error) attr;
}
if (attr instanceof Exception) {
throw new IllegalStateException((Exception) attr);
}
if (!(attr instanceof WebEnvironment)) {
throw new IllegalStateException("Context attribute is not of type WebEnvironment: " + attr);
}
return (WebEnvironment) attr;
}
示例2: contextInitialized
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
@Override
public void contextInitialized( ServletContextEvent sce )
{
configuration.refresh();
ShiroIniConfiguration config = configuration.get();
String iniResourcePath = config.iniResourcePath().get() == null ? "classpath:shiro.ini" : config.iniResourcePath().get();
sce.getServletContext().setInitParameter( "shiroConfigLocations", iniResourcePath );
WebEnvironment env = initEnvironment( sce.getServletContext() );
if ( realmsRefs != null && realmsRefs.iterator().hasNext() ) {
// Register Realms Services
RealmSecurityManager realmSecurityManager = ( RealmSecurityManager ) env.getSecurityManager();
Collection<Realm> iniRealms = new ArrayList<Realm>( realmSecurityManager.getRealms() );
for ( ServiceReference<Realm> realmRef : realmsRefs ) {
iniRealms.add( realmRef.get() );
LOG.debug( "Realm Service '{}' registered!", realmRef.identity() );
}
realmSecurityManager.setRealms( iniRealms );
}
}
示例3: setup
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
@Override
protected void setup() {
String configFile = getSettings().getString("shiro.configurationFile", "classpath:conf/shiro.ini");
Ini ini = Ini.fromResourcePath(configFile);
IniWebEnvironment webEnvironment = new IniWebEnvironment();
webEnvironment.setIni(ini);
webEnvironment.setServletContext(getServletContext());
webEnvironment.init();
bind(WebEnvironment.class).toInstance(webEnvironment);
bind(SecurityManager.class).toInstance(webEnvironment.getSecurityManager());
bind(WebSecurityManager.class).toInstance(webEnvironment.getWebSecurityManager());
String basePath = Strings.nullToEmpty(getSettings().getString(RestServlet.SETTING_URL, null)).trim();
filter(basePath + "/*").through(ShiroFilter.class);
install(new AopModule());
}
示例4: createEnvironment
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
/**
* Registers the ModularWebEnvironment to the specified servlet context.
*
* @param sc current servlet context
* @throws IllegalStateException
* if an existing WebEnvironment has already been initialized and associated with
* the specified {@code ServletContext} or if ModularWebEnvironment is not
* prepared to be associated
*/
@Override
protected WebEnvironment createEnvironment(ServletContext sc) {
if (environment == null) {
String errMsg = "ModularWebEnvironment must be initialized before setting it "
+ "to servlet context";
log.error(errMsg);
throw new IllegalStateException(errMsg);
}
environment.setServletContext(sc);
customizeEnvironment(environment);
LifecycleUtils.init(environment);
return environment;
}
示例5: testInitSecurityFilter
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
@Test
public void testInitSecurityFilter() throws Exception {
// expect initializing environment
expect(environmentLoaderMock.initEnvironment(servletContextMock)).
andReturn(createMock(WebEnvironment.class));
expectSettingDefaultLoginUrl();
expectSettingDefaultPasswordParam();
expectSettingDefaultRememberMeParam();
expectSettingDefaultSuccessUrl();
expectSettingDefaultUsernameParam();
expect(servletContextMock.getAttribute(EnvironmentLoader.ENVIRONMENT_ATTRIBUTE_KEY)).andReturn(null);
// perform test
replayAll();
filter.init();
verifyAll();
}
示例6: createEnvironment
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
@Override
protected WebEnvironment createEnvironment(ServletContext sc) {
DefaultWebEnvironment webEnvironment = (DefaultWebEnvironment) super.createEnvironment(sc);
webEnvironment.setSecurityManager(securityManager);
webEnvironment.setFilterChainResolver(filterChainResolver);
return webEnvironment;
}
示例7: init
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
/**
* Configures this instance based on the existing {@link org.apache.shiro.web.env.WebEnvironment} instance
* available to the currently accessible {@link #getServletContext() servletContext}.
*
* @see org.apache.shiro.web.env.EnvironmentLoaderListener
* @since 1.2
*/
@Override
public void init() throws Exception {
WebEnvironment env = WebUtils.getRequiredWebEnvironment(getServletContext());
setSecurityManager(env.getWebSecurityManager());
FilterChainResolver resolver = env.getFilterChainResolver();
if (resolver != null) {
setFilterChainResolver(resolver);
}
}
示例8: initEnvironment
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
public WebEnvironment initEnvironment(ServletContext servletContext) throws IllegalStateException {
if (servletContext.getAttribute(ENVIRONMENT_ATTRIBUTE_KEY) != null) {
String msg = "There is already a Shiro environment associated with the current ServletContext. " +
"Check if you have multiple EnvironmentLoader* definitions in your web.xml!";
throw new IllegalStateException(msg);
}
servletContext.log("Initializing Shiro environment");
LOGGER.info("Starting Shiro environment initialization.");
long startTime = System.currentTimeMillis();
try {
WebEnvironment environment = createEnvironment(servletContext);
servletContext.setAttribute(ENVIRONMENT_ATTRIBUTE_KEY, environment);
LOGGER.debug("Published WebEnvironment as ServletContext attribute with name [{}]",
ENVIRONMENT_ATTRIBUTE_KEY);
if (LOGGER.isInfoEnabled()) {
long elapsed = System.currentTimeMillis() - startTime;
LOGGER.info("Shiro environment initialized in {} ms.", elapsed);
}
return environment;
} catch (RuntimeException ex) {
LOGGER.error(ex, "Shiro environment initialization failed");
servletContext.setAttribute(ENVIRONMENT_ATTRIBUTE_KEY, ex);
throw ex;
} catch (Error err) {
LOGGER.error(err, "Shiro environment initialization failed");
servletContext.setAttribute(ENVIRONMENT_ATTRIBUTE_KEY, err);
throw err;
}
}
示例9: createEnvironment
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
protected WebEnvironment createEnvironment(ServletContext sc) {
Class<?> clazz = determineWebEnvironmentClass(sc);
if (!MutableWebEnvironment.class.isAssignableFrom(clazz)) {
throw new ConfigurationException("Custom WebEnvironment class [" + clazz.getName() +
"] is not of required type [" + WebEnvironment.class.getName() + "]");
}
String configLocations = sc.getInitParameter(CONFIG_LOCATIONS_PARAM);
boolean configSpecified = StringUtils.hasText(configLocations);
if (configSpecified && !(ResourceConfigurable.class.isAssignableFrom(clazz))) {
String msg = "WebEnvironment class [" + clazz.getName() + "] does not implement the " +
ResourceConfigurable.class.getName() + "interface. This is required to accept any " +
"configured " + CONFIG_LOCATIONS_PARAM + "value(s).";
throw new ConfigurationException(msg);
}
MutableWebEnvironment environment = (MutableWebEnvironment) ClassUtils.newInstance(clazz);
environment.setServletContext(sc);
if (configSpecified && (environment instanceof ResourceConfigurable)) {
((ResourceConfigurable) environment).setConfigLocations(configLocations);
}
customizeEnvironment(environment);
//environment.setWebSecurityManager(defaultWebSecurityManager);
//LifecycleUtils.initialBuild(environment);
return environment;
}
示例10: createEnvironment
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
@Override
protected WebEnvironment createEnvironment(ServletContext sc) {
WebEnvironment webEnvironment = super.createEnvironment(sc);
RealmSecurityManager rsm = (RealmSecurityManager) webEnvironment.getSecurityManager();
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(HASHING_ALGORITHM);
hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
jpaRealm.setCredentialsMatcher(hashedCredentialsMatcher);
Collection<Realm> realms = rsm.getRealms();
realms.add(jpaRealm);
rsm.setRealms(realms);
((DefaultWebEnvironment) webEnvironment).setSecurityManager(rsm);
return webEnvironment;
}
示例11: createEnvironment
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
@Override
protected WebEnvironment createEnvironment(ServletContext servletContext) {
ShiroWebEnvironment environment = new ShiroWebEnvironment();
environment.setServletContext(servletContext);
String configLocations = StringUtils.trimToNull(servletContext.getInitParameter(CONFIG_LOCATIONS_PARAM));
if (configLocations != null) {
environment.setConfigLocations(configLocations);
}
environment.init();
return environment;
}
示例12: createEnvironment
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
/**
*
* @param servletContext
* @return
*/
@Override
protected WebEnvironment createEnvironment(ServletContext servletContext) {
final DefaultWebEnvironment environment = (DefaultWebEnvironment)
super.createEnvironment(servletContext);
environment.setSecurityManager(this.webSecurityManager);
environment.setFilterChainResolver(this.filterChainResolver);
return environment;
}
示例13: init
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
/**
* @see org.apache.wicket.Application#init()
*/
@Override
public void init() {
super.init();
String version = "0.0.17";
System.setProperty("MyOpenTraderWebVersion", version);
// Check the properties
PropertiesFactory pf = PropertiesFactory.getInstance();
ServletContext context = this.getServletContext();
String configDir = pf.getConfigDir();
// If the configDir is already set, use this one otherwise get the
// context
if (configDir == null) {
configDir = context.getRealPath("/WEB-INF/conf");
pf.setConfigDir(configDir);
}
System.out.println("*** STARTING NEW MYOPENTRADER WEB INSTANCE ***");
System.out.println("Using Config directory: " + configDir);
System.out.println("Using context path: " + context.getContextPath());
System.out.println("**********");
// Configure the log4j properties
PropertyConfigurator.configure(pf.getConfigDir() + "/log4j.properties");
// The following lines ensure that the context is set for SHIRO. If not,
// the securityFactory will not get a subject
WebEnvironment wm = WebUtils.getRequiredWebEnvironment(context);
WebSecurityManager wsm = wm.getWebSecurityManager();
ThreadContext.bind(wsm);
}
示例14: createEnvironment
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
@Override
protected WebEnvironment createEnvironment(final ServletContext sc) {
WebEnvironment environment = super.createEnvironment(sc);
AuthorizingSecurityManager securityManager = (AuthorizingSecurityManager) environment.getSecurityManager();
securityManager.setAuthorizer(this.authorizer);
securityManager.setRealm(this.realm);
return environment;
}
示例15: initialize
import org.apache.shiro.web.env.WebEnvironment; //导入依赖的package包/类
@Override
public boolean initialize(SilentGo me) throws AppBuildException {
ShiroConfig config = (ShiroConfig) me.getConfig().getConfig(Name);
me.getFactory(CacheFactory.class);
PropKit prop = me.getConfig().getUserProp();
RetryLimitHashedCredentialsMatcher retryLimitHashedCredentialsMatcher = new RetryLimitHashedCredentialsMatcher();
retryLimitHashedCredentialsMatcher.setHashAlgorithmName(prop.getValue(Dict.SHIRO_CREDENTIALS_MATCHER_HASHALGORITHMNAME, "md5"));
retryLimitHashedCredentialsMatcher.setHashIterations(prop.getInt(Dict.SHIRO_CREDENTIALS_MATCHER_HASHITERATIONS, 2));
retryLimitHashedCredentialsMatcher.setStoredCredentialsHexEncoded(prop.getBool(Dict.SHIRO_CREDENTIALS_MATCHER_STOREDCREDENTIALSHEXENCODED, true));
EhCacheManager ehCacheManager = new EhCacheManager();
EhCache cache = (EhCache) me.getConfig().getCacheManager();
ehCacheManager.setCacheManager(cache.getCacheManager());
JavaUuidSessionIdGenerator sessionIdGenerator = new JavaUuidSessionIdGenerator();
SimpleCookie sessionIdCookie = new SimpleCookie(prop.getValue(Dict.SHIRO_SESSION_COOKIENAME, "lc4e"));
sessionIdCookie.setMaxAge(prop.getInt(Dict.SHIRO_SESSION_IDCOOKIE_MAXAGE, -1));
sessionIdCookie.setHttpOnly(prop.getBool(Dict.SHIRO_SESSION_IDCOOKIE_HTTPONLY, true));
SimpleCookie rememberMeCookie = new SimpleCookie(prop.getValue(Dict.SHIRO_SESSION_REMEMBER_COOKIENAME, "rlc4e"));
rememberMeCookie.setHttpOnly(prop.getBool(Dict.SHIRO_SESSION_REMEMBER_ME_HTTPONLY, true));
rememberMeCookie.setMaxAge(prop.getInt(Dict.SHIRO_SESSION_REMEMBER_ME_MAXAGE, 2592000));
CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
rememberMeManager.setCipherKey(Base64.decode(prop.getValue(Dict.SECURITY_KEY, "4AvVhmFLUs0KTA3Kprsdag==")));
rememberMeManager.setCookie(rememberMeCookie);
EnterpriseCacheSessionDAO sessionDAO = new EnterpriseCacheSessionDAO();
sessionDAO.setActiveSessionsCacheName(prop.getValue(Dict.SHIRO_SESSION_ACTIVE_NAME, "shiro-activeSessionCache"));
sessionDAO.setSessionIdGenerator(sessionIdGenerator);
sessionDAO.setCacheManager(ehCacheManager);
config.getRealm().setCredentialsMatcher(retryLimitHashedCredentialsMatcher);
config.getRealm().setCachingEnabled(false);
config.getRealm().setCacheManager(ehCacheManager);
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
sessionValidationScheduler = new QuartzSessionValidationScheduler(sessionManager);
sessionValidationScheduler.setSessionValidationInterval(prop.getLong(Dict.SHIRO_SESSION_VALIDATIONINTERVAL, 1800000L));
sessionManager.setGlobalSessionTimeout(prop.getLong(Dict.SHIRO_SESSION_GLOBALSESSIONTIMEOUT, 1800000L));
sessionManager.setSessionDAO(sessionDAO);
sessionManager.setSessionValidationInterval(prop.getLong(Dict.SHIRO_SESSION_VALIDATIONINTERVAL, 360000L));
sessionManager.setSessionValidationScheduler(sessionValidationScheduler);
sessionManager.setSessionIdCookieEnabled(prop.getBool(Dict.SHIRO_SESSION_IDCOOKIEENABLED, true));
sessionManager.setSessionIdCookie(sessionIdCookie);
sessionValidationScheduler.setSessionManager(sessionManager);
defaultWebSecurityManager = new DefaultWebSecurityManager(config.getRealm());
defaultWebSecurityManager.setCacheManager(ehCacheManager);
defaultWebSecurityManager.setRememberMeManager(rememberMeManager);
defaultWebSecurityManager.setSessionManager(sessionManager);
ServletContext servletContext = me.getContext();
ShiroLoader shiroLoader = new ShiroLoader();
shiroLoader.initEnvironment(servletContext);
WebEnvironment environment = (WebEnvironment) servletContext.getAttribute(ShiroLoader.ENVIRONMENT_ATTRIBUTE_KEY);
((MutableWebEnvironment) environment).setWebSecurityManager(defaultWebSecurityManager);
servletContext.setAttribute(ShiroLoader.ENVIRONMENT_ATTRIBUTE_KEY, environment);
return false;
}