View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.samples.guice;
20  
21  import com.google.inject.Provides;
22  import com.google.inject.binder.AnnotatedBindingBuilder;
23  import com.google.inject.name.Names;
24  import org.apache.shiro.config.Ini;
25  import org.apache.shiro.guice.web.ShiroWebModule;
26  import org.apache.shiro.realm.text.IniRealm;
27  import org.apache.shiro.session.mgt.SessionManager;
28  import org.apache.shiro.web.servlet.Cookie;
29  import org.apache.shiro.web.servlet.SimpleCookie;
30  import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
31  
32  import javax.inject.Singleton;
33  import javax.servlet.ServletContext;
34  import java.net.MalformedURLException;
35  import java.net.URL;
36  
37  public class SampleShiroNativeSessionsServletModule extends ShiroWebModule {
38      private final ServletContext servletContext;
39  
40      public SampleShiroNativeSessionsServletModule(ServletContext servletContext) {
41          super(servletContext);
42  
43          this.servletContext = servletContext;
44      }
45  
46      @Override
47      protected void configureShiroWeb() {
48          bindConstant().annotatedWith(Names.named("shiro.loginUrl")).to("/login.jsp");
49          try {
50              this.bindRealm().toConstructor(IniRealm.class.getConstructor(Ini.class));
51          } catch (NoSuchMethodException e) {
52              addError("Could not locate proper constructor for IniRealm.", e);
53          }
54  
55          this.addFilterChain("/login.jsp", AUTHC);
56          this.addFilterChain("/logout", LOGOUT);
57          this.addFilterChain("/account/**", AUTHC);
58          this.addFilterChain("/remoting/**", AUTHC, config(ROLES, "b2bClient"), config(PERMS, "remote:invoke:lan,wan"));
59      }
60  
61      @Provides
62      @Singleton
63      Ini loadShiroIni() throws MalformedURLException {
64          URL iniUrl = servletContext.getResource("/WEB-INF/shiro.ini");
65          return Ini.fromResourcePath("url:" + iniUrl.toExternalForm());
66      }
67  
68      @Override
69      protected void bindSessionManager(AnnotatedBindingBuilder<SessionManager> bind) {
70          bind.to(DefaultWebSessionManager.class);
71          bindConstant().annotatedWith(Names.named("shiro.globalSessionTimeout")).to(5000L);
72          bind(DefaultWebSessionManager.class);
73          bind(Cookie.class).toInstance(new SimpleCookie("myCookie"));
74      }
75  }