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.web;
20  
21  import org.apache.shiro.SecurityUtils;
22  import org.apache.shiro.samples.spring.SampleManager;
23  import org.apache.shiro.session.Session;
24  import org.apache.shiro.subject.Subject;
25  import org.springframework.validation.BindException;
26  import org.springframework.validation.Errors;
27  import org.springframework.web.servlet.ModelAndView;
28  import org.springframework.web.servlet.mvc.SimpleFormController;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import java.util.HashMap;
33  import java.util.LinkedHashMap;
34  import java.util.Map;
35  
36  /**
37   * Spring MVC controller responsible for rendering the Shiro Spring sample
38   * application index page.
39   *
40   * @since 0.1
41   */
42  public class IndexController extends SimpleFormController {
43  
44      /*--------------------------------------------
45      |             C O N S T A N T S             |
46      ============================================*/
47  
48      /*--------------------------------------------
49      |    I N S T A N C E   V A R I A B L E S    |
50      ============================================*/
51  
52      private SampleManager sampleManager;
53  
54      /*--------------------------------------------
55      |         C O N S T R U C T O R S           |
56      ============================================*/
57  
58      /*--------------------------------------------
59      |  A C C E S S O R S / M O D I F I E R S    |
60      ============================================*/
61  
62      public void setSampleManager(SampleManager sampleManager) {
63          this.sampleManager = sampleManager;
64      }
65  
66      /*--------------------------------------------
67      |               M E T H O D S               |
68      ============================================*/
69  
70      protected Object formBackingObject(HttpServletRequest request) throws Exception {
71          SessionValueCommand command = (SessionValueCommand) createCommand();
72  
73          command.setValue(sampleManager.getValue());
74          return command;
75      }
76  
77      protected Map<String, Object> referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
78          Subject subject = SecurityUtils.getSubject();
79          boolean hasRole1 = subject.hasRole("role1");
80          boolean hasRole2 = subject.hasRole("role2");
81  
82          Map<String, Object> refData = new HashMap<String, Object>();
83          refData.put("hasRole1", hasRole1);
84          refData.put("hasRole2", hasRole2);
85  
86          Session session = subject.getSession();
87          Map<Object, Object> sessionAttributes = new LinkedHashMap<Object, Object>();
88          for (Object key : session.getAttributeKeys()) {
89              sessionAttributes.put(key, session.getAttribute(key));
90          }
91          refData.put("sessionAttributes", sessionAttributes);
92  
93          refData.put("subjectSession", subject.getSession());
94          return refData;
95      }
96  
97      protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj, BindException errors) throws Exception {
98          SessionValueCommand command = (SessionValueCommand) obj;
99  
100         sampleManager.setValue(command.getValue());
101 
102         return showForm(request, response, errors);
103     }
104 
105 }