KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > security > web > AbstractUserPrincipal


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.security.web;
17
18 import java.security.Principal JavaDoc;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpSession JavaDoc;
22
23 import com.jdon.controller.model.Model;
24 import com.jdon.controller.model.ModelIF;
25 import com.jdon.controller.pool.Poolable;
26 import com.jdon.util.Debug;
27
28 /**
29  * 有关获得登陆用户的任何信æ?¯ã€‚
30  *
31  * @author banq
32  */

33 public abstract class AbstractUserPrincipal implements UserPrincipal, Poolable {
34
35   public final static String JavaDoc USERMODEL = "USERMODEL";
36
37   private final static String JavaDoc module = AbstractUserPrincipal.class.getName();
38
39   /**
40    * å°†UserModel从登陆的Principal中获å?–å?Žï¼Œ
41    * Principal当用户访问被授æ?ƒåŒºåŸŸï¼Œä¼šç”±Web容器自动产生。
42    * ä¿?存在HttpSession中,供其它Web层组件使用。
43    * @param request
44    */

45   public ModelIF getUserFromPrincipal(HttpServletRequest JavaDoc request) {
46     Debug.logVerbose("[JdonFramework]--> enter getUserFromPrincipal", module);
47     Principal JavaDoc principal = request.getUserPrincipal();
48     if (principal == null) {
49       Debug.logVerbose("[JdonFramework]principal is null, not login by JAAS", module);
50       return null;
51     }
52     String JavaDoc username = principal.getName();
53     Debug.logVerbose("[JdonFramework]--> find the logined username=" + username, module);
54     HttpSession JavaDoc session = request.getSession();
55     ModelIF model = (ModelIF) session.getAttribute(USERMODEL);
56     if (model == null) {
57       model = findUserModelIFByName(request, username);
58       session.setAttribute(USERMODEL, model);
59     }
60     return model;
61   }
62
63   public void saveModeltoSession(HttpServletRequest JavaDoc request, ModelIF model) {
64     HttpSession JavaDoc session = request.getSession();
65     session.setAttribute(USERMODEL, model);
66   }
67
68   /**
69    * 直接从Session获å?–,å?‡è®¾Session中已ç»?存在
70    * @param request HttpServletRequest
71    * @return Model
72    */

73   public ModelIF getUserFromSession(HttpServletRequest JavaDoc request) {
74     Debug.logVerbose("[JdonFramework]--> enter getUserFromSession", module);
75     HttpSession JavaDoc session = request.getSession();
76     ModelIF model = (ModelIF) session.getAttribute(USERMODEL);
77     return model;
78   }
79
80   public ModelIF findUserModelIFByName(HttpServletRequest JavaDoc request,
81                                             String JavaDoc key){
82       return findUserModelByName(request, key);
83   }
84   
85   public abstract Model findUserModelByName(HttpServletRequest JavaDoc request,
86           String JavaDoc key);
87 }
88
Popular Tags