/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.shiro.samples.guice;

import com.google.inject.Provides;
import com.google.inject.binder.AnnotatedBindingBuilder;
import com.google.inject.name.Names;
import org.apache.shiro.config.Ini;
import org.apache.shiro.guice.web.ShiroWebModule;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;

import javax.inject.Singleton;
import javax.servlet.ServletContext;
import java.net.MalformedURLException;
import java.net.URL;

public class SampleShiroNativeSessionsServletModule extends ShiroWebModule {
    private final ServletContext servletContext;

    public SampleShiroNativeSessionsServletModule(ServletContext servletContext) {
        super(servletContext);

        this.servletContext = servletContext;
    }

    @Override
    protected void configureShiroWeb() {
        bindConstant().annotatedWith(Names.named("shiro.loginUrl")).to("/login.jsp");
        try {
            this.bindRealm().toConstructor(IniRealm.class.getConstructor(Ini.class));
        } catch (NoSuchMethodException e) {
            addError("Could not locate proper constructor for IniRealm.", e);
        }

        this.addFilterChain("/login.jsp", AUTHC);
        this.addFilterChain("/logout", LOGOUT);
        this.addFilterChain("/account/**", AUTHC);
        this.addFilterChain("/remoting/**", filterConfig(AUTHC), filterConfig(ROLES, "b2bClient"), filterConfig(PERMS, "remote:invoke:lan,wan"));
    }

    @Provides
    @Singleton
    Ini loadShiroIni() throws MalformedURLException {
        URL iniUrl = servletContext.getResource("/WEB-INF/shiro.ini");
        return Ini.fromResourcePath("url:" + iniUrl.toExternalForm());
    }

    @Override
    protected void bindSessionManager(AnnotatedBindingBuilder<SessionManager> bind) {
        bind.to(DefaultWebSessionManager.class);
        bindConstant().annotatedWith(Names.named("shiro.globalSessionTimeout")).to(5000L);
        bindConstant().annotatedWith(Names.named("shiro.sessionIdUrlRewritingEnabled")).to(false);
        bind(DefaultWebSessionManager.class);
        bind(Cookie.class).toInstance(new SimpleCookie("myCookie"));
    }
}

@@ -969,24 +969,117 @@
 name.Names;%0A
+import org.apache.shiro.codec.Base64;%0Aimport org.apache.shiro.config.ConfigurationException;%0A
 import org.a
@@ -1245,24 +1245,192 @@
 ionManager;%0A
+import org.apache.shiro.web.mgt.CookieRememberMeManager;%0Aimport org.apache.shiro.web.mgt.DefaultWebSecurityManager;%0Aimport org.apache.shiro.web.mgt.WebSecurityManager;%0A
 import org.a
@@ -3369,11 +3369,991 @@
 );%0A    %7D
+%0A%0A    @Override%0A    protected void bindWebSecurityManager(AnnotatedBindingBuilder%3C? super WebSecurityManager%3E bind)%0A    %7B%0A        try%0A        %7B%0A            String cipherKey = loadShiroIni().getSectionProperty( %22main%22, %22securityManager.rememberMeManager.cipherKey%22 );%0A%0A            DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();%0A            CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();%0A            rememberMeManager.setCipherKey( Base64.decode( cipherKey ) );%0A            securityManager.setRememberMeManager(rememberMeManager);%0A            bind.toInstance(securityManager);%0A        %7D%0A        catch ( MalformedURLException e )%0A        %7B%0A            // for now just throw, you could just call%0A            // super.bindWebSecurityManager(bind) if you do not need rememberMe functionality%0A            throw new ConfigurationException( %22securityManager.rememberMeManager.cipherKey must be set in shiro.ini.%22 );%0A        %7D%0A    %7D
 %0A%7D%0A

Fix Guice IT that expects RememberMe to persist across restarts