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.spring.boot.autoconfigure.web.application;
20  
21  
22  import org.apache.shiro.event.EventBus;
23  import org.apache.shiro.event.EventBusAware;
24  import org.apache.shiro.event.Subscribe;
25  import org.apache.shiro.realm.Realm;
26  import org.apache.shiro.realm.text.TextConfigurationRealm;
27  import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
28  import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
29  import org.springframework.boot.SpringApplication;
30  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
31  import org.springframework.context.annotation.Bean;
32  import org.springframework.context.annotation.Configuration;
33  
34  @Configuration
35  @EnableAutoConfiguration
36  public class ShiroWebAutoConfigurationTestApplication {
37  
38      public static void main(String[] args) {
39          SpringApplication.run(ShiroWebAutoConfigurationTestApplication.class, args);
40      }
41  
42      @Bean
43      @SuppressWarnings("Duplicates")
44      Realm getTextConfigurationRealm() {
45  
46          TextConfigurationRealm realm = new TextConfigurationRealm();
47          realm.setUserDefinitions("joe.coder=password,user\n" +
48                                   "jill.coder=password,admin");
49  
50          realm.setRoleDefinitions("admin=read,write\n" +
51                                   "user=read");
52          realm.setCachingEnabled(true);
53          return realm;
54      }
55  
56      @Bean
57      ShiroFilterChainDefinition shiroFilterChainDefinition() {
58          DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
59          chainDefinition.addPathDefinition("/login.html", "authc");
60          return chainDefinition;
61      }
62  
63      @Bean
64      EventBusAwareObject eventBusAwareObject() {
65          return new EventBusAwareObject();
66      }
67  
68      @Bean
69      SubscribedListener subscribedListener() {
70          return new SubscribedListener();
71      }
72  
73  
74      public static class EventBusAwareObject implements EventBusAware {
75  
76          private EventBus eventBus;
77  
78          @Override
79          public void setEventBus(EventBus bus) {
80              this.eventBus = bus;
81          }
82  
83          public EventBus getEventBus() {
84              return eventBus;
85          }
86      }
87  
88      public static class SubscribedListener {
89  
90          @Subscribe
91          public void onEvent(Object object) {}
92      }
93  }