View Javadoc

1   /*
2    * Copyright 2008-2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.service.impl;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import org.kuali.rice.kim.api.identity.AuthenticationService;
20  
21  import org.kuali.student.common.util.security.UserWithId;
22  import org.springframework.security.core.Authentication;
23  import org.springframework.security.core.context.SecurityContextHolder;
24  import org.springframework.security.core.userdetails.UserDetails;
25  
26  public class AuthenticationServiceImpl implements AuthenticationService {
27      
28  	public String getPrincipalName(HttpServletRequest request) {
29  	    String username=null;
30  	    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
31  	    
32  	    if(auth!=null){
33              Object obj = auth.getPrincipal();
34              if(obj instanceof UserWithId){
35                  //This is actually the user Id
36                  username = ((UserWithId)obj).getUserId();
37              }else if (obj instanceof UserDetails) {
38                  username = ((UserDetails)obj).getUsername();
39              } else {
40                  username = request.getRemoteUser();
41              }
42          }
43  	    return username;
44  	}
45  }