OAuth2UserDetailsService.java 2.9 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright 2006-2011 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.maxkey.authz.oauth2.provider;

M
MaxKey 已提交
15 16 17 18 19
import java.util.ArrayList;

import org.maxkey.authn.AbstractAuthenticationProvider;
import org.maxkey.authn.SigninPrincipal;
import org.maxkey.authn.online.OnlineTicket;
M
MaxKey 已提交
20
import org.maxkey.entity.UserInfo;
M
MaxKey 已提交
21
import org.maxkey.persistence.repository.LoginRepository;
M
MaxKey 已提交
22 23 24 25
import org.maxkey.web.WebConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
26 27 28 29 30 31 32 33 34
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * @author Dave Syer
 * 
 */
public class OAuth2UserDetailsService implements UserDetailsService {
M
MaxKey 已提交
35 36
	 private static final Logger _logger = 
	            LoggerFactory.getLogger(OAuth2UserDetailsService.class);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
37
	
M
MaxKey 已提交
38
    LoginRepository loginRepository;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
39 40 41 42
	
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		UserInfo userInfo;
		try {
M
MaxKey 已提交
43
		    userInfo = loginRepository.find(username, "");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
44 45 46 47
		} catch (NoSuchClientException e) {
			throw new UsernameNotFoundException(e.getMessage(), e);
		}
		
M
MaxKey 已提交
48
		String onlineTickitId = WebConstants.ONLINE_TICKET_PREFIX + "-" + java.util.UUID.randomUUID().toString().toLowerCase();
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
49
		
M
220413  
MaxKey 已提交
50
		SigninPrincipal principal = new SigninPrincipal(userInfo);
M
MaxKey 已提交
51 52
		OnlineTicket onlineTicket = new OnlineTicket(onlineTickitId);
		//set OnlineTicket
M
220413  
MaxKey 已提交
53
		principal.setOnlineTicket(onlineTicket);
M
MaxKey 已提交
54
        
M
MaxKey 已提交
55
        ArrayList<GrantedAuthority> grantedAuthoritys = loginRepository.grantAuthority(userInfo);
M
220413  
MaxKey 已提交
56
        principal.setAuthenticated(true);
M
MaxKey 已提交
57 58 59
        
        for(GrantedAuthority administratorsAuthority : AbstractAuthenticationProvider.grantedAdministratorsAuthoritys) {
            if(grantedAuthoritys.contains(administratorsAuthority)) {
M
220413  
MaxKey 已提交
60
            	principal.setRoleAdministrators(true);
M
MaxKey 已提交
61 62 63 64 65
                _logger.trace("ROLE ADMINISTRATORS Authentication .");
            }
        }
        _logger.debug("Granted Authority " + grantedAuthoritys);
        
M
220413  
MaxKey 已提交
66
        principal.setGrantedAuthorityApps(grantedAuthoritys);
M
MaxKey 已提交
67
        
M
220413  
MaxKey 已提交
68
		return principal;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
69 70
	}

M
MaxKey 已提交
71 72 73 74 75
	public void setLoginRepository(LoginRepository loginRepository) {
		this.loginRepository = loginRepository;
	}

    
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
76
}