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.spring;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import org.apache.shiro.SecurityUtils;
25  import org.apache.shiro.session.Session;
26  import org.apache.shiro.subject.Subject;
27  
28  
29  /**
30   * Default implementation of the {@link SampleManager} interface that stores
31   * and retrieves a value from the user's session.
32   *
33   * @since 0.1
34   */
35  public class DefaultSampleManager implements SampleManager {
36  
37      /*--------------------------------------------
38      |             C O N S T A N T S             |
39      ============================================*/
40      /**
41       * Key used to store the value in the user's session.
42       */
43      private static final String VALUE_KEY = "sample_value";
44  
45      /*--------------------------------------------
46      |    I N S T A N C E   V A R I A B L E S    |
47      ============================================*/
48      private static final Logger log = LoggerFactory.getLogger(DefaultSampleManager.class);
49  
50      /*--------------------------------------------
51      |         C O N S T R U C T O R S           |
52      ============================================*/
53  
54      /*--------------------------------------------
55      |  A C C E S S O R S / M O D I F I E R S    |
56      ============================================*/
57  
58      /*--------------------------------------------
59      |               M E T H O D S               |
60      ============================================*/
61  
62      public String getValue() {
63          String value = null;
64  
65          Subject subject = SecurityUtils.getSubject();
66          Session session = subject.getSession(false);
67          if (session != null) {
68              value = (String) session.getAttribute(VALUE_KEY);
69              if (log.isDebugEnabled()) {
70                  log.debug("retrieving session key [" + VALUE_KEY + "] with value [" + value + "] on session with id [" + session.getId() + "]");
71              }
72          }
73  
74          return value;
75      }
76  
77      public void setValue(String newValue) {
78          Subject subject = SecurityUtils.getSubject();
79          Session session = subject.getSession();
80  
81          if (log.isDebugEnabled()) {
82              log.debug("saving session key [" + VALUE_KEY + "] with value [" + newValue + "] on session with id [" + session.getId() + "]");
83          }
84  
85          session.setAttribute(VALUE_KEY, newValue);
86      }
87  
88      public void secureMethod1() {
89          if (log.isInfoEnabled()) {
90              log.info("Secure method 1 called...");
91          }
92      }
93  
94      public void secureMethod2() {
95          if (log.isInfoEnabled()) {
96              log.info("Secure method 2 called...");
97          }
98      }
99  
100     public void secureMethod3() {
101         if (log.isInfoEnabled()) {
102             log.info("Secure method 3 called...");
103         }
104     }
105 }