ExtendApiAuthorizeEndpoint.java 3.0 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
 * 
 * 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.
 */
 

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
18 19 20 21 22 23 24
/**
 * 
 */
package org.maxkey.authz.exapi.endpoint;

import javax.servlet.http.HttpServletRequest;

M
MaxKey 已提交
25 26
import org.maxkey.authn.annotation.CurrentUser;
import org.maxkey.authn.web.AuthorizationUtils;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
27 28
import org.maxkey.authz.endpoint.AuthorizeBaseEndpoint;
import org.maxkey.authz.endpoint.adapter.AbstractAuthorizeAdapter;
M
v 3.3.0  
MaxKey 已提交
29
import org.maxkey.constants.ConstsBoolean;
M
MaxKey 已提交
30
import org.maxkey.entity.Accounts;
M
MaxKey 已提交
31
import org.maxkey.entity.UserInfo;
M
MaxKey 已提交
32
import org.maxkey.entity.apps.Apps;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
33 34 35 36 37 38 39 40
import org.maxkey.util.Instance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

M
v 3.2.0  
MaxKey 已提交
41 42
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
43

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
44 45 46 47
/**
 * @author Crystal.Sea
 *
 */
M
v 3.2.0  
MaxKey 已提交
48
@Tag(name = "2-8-ExtendApi接口文档模块")
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
49 50 51 52
@Controller
public class ExtendApiAuthorizeEndpoint  extends AuthorizeBaseEndpoint{
	final static Logger _logger = LoggerFactory.getLogger(ExtendApiAuthorizeEndpoint.class);

M
v 3.2.0  
MaxKey 已提交
53
	@Operation(summary = "ExtendApi认证地址接口", description = "参数应用ID",method="GET")
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
54
	@RequestMapping("/authz/api/{id}")
M
MaxKey 已提交
55 56 57 58
	public ModelAndView authorize(
			HttpServletRequest request,
			@PathVariable("id") String id,
			@CurrentUser UserInfo currentUser){
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
59
	    
M
MaxKey 已提交
60 61 62 63
	    ModelAndView modelAndView = new ModelAndView("authorize/redirect_sso_submit");
	    modelAndView.addObject("errorCode", 0);
	    modelAndView.addObject("errorMessage", "");
	    
M
MaxKey 已提交
64
		Apps apps = getApp(id);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
65
		_logger.debug(""+apps);
M
v 3.3.0  
MaxKey 已提交
66
		if(ConstsBoolean.isTrue(apps.getIsAdapter())){
M
MaxKey 已提交
67
			_logger.debug("Adapter {}",apps.getAdapter());
M
MaxKey 已提交
68
			AbstractAuthorizeAdapter adapter = (AbstractAuthorizeAdapter)Instance.newInstance(apps.getAdapter());
M
MaxKey 已提交
69
			Accounts account = getAccounts(apps,currentUser);
M
MaxKey 已提交
70
			if(apps.getCredential().equalsIgnoreCase(Apps.CREDENTIALS.USER_DEFINED) && account == null) {
M
MaxKey 已提交
71
				return initCredentialView(id,"/authorize/api/"+id);
M
MaxKey 已提交
72
			}
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
73
			
M
220413  
MaxKey 已提交
74
			adapter.setPrincipal(AuthorizationUtils.getPrincipal());
M
MaxKey 已提交
75 76 77 78
			adapter.setApp(apps);
			adapter.setAccount(account);
			
			return adapter.authorize(modelAndView);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
79
		}else{
M
MaxKey 已提交
80
			_logger.debug("redirect_uri {}",apps.getLoginUrl());
M
MaxKey 已提交
81
	        modelAndView.addObject("redirect_uri", apps.getLoginUrl());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
82
	        return modelAndView;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
83 84 85 86
		}
		
	}
}