CasAuthorizeEndpoint.java 7.7 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
/**
 * 
 */
package org.maxkey.authz.cas.endpoint;

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
23
import java.security.Principal;
24
import java.util.Map;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
25 26 27 28

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

M
MaxKey 已提交
29
import org.apache.commons.lang3.StringUtils;
M
MaxKey 已提交
30
import org.maxkey.authn.session.Session;
M
MaxKey 已提交
31
import org.maxkey.authn.web.AuthorizationUtils;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
32 33
import org.maxkey.authz.cas.endpoint.ticket.CasConstants;
import org.maxkey.authz.cas.endpoint.ticket.ServiceTicketImpl;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
34
import org.maxkey.authz.singlelogout.LogoutType;
M
MaxKey 已提交
35
import org.maxkey.entity.apps.AppsCasDetails;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
36 37 38 39 40
import org.maxkey.web.WebConstants;
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
41
import org.springframework.web.bind.annotation.GetMapping;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
42 43 44 45
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
M
v 3.2.0  
MaxKey 已提交
46 47 48

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
49

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
50 51
/**
 * @author Crystal.Sea
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
52
 * https://apereo.github.io/cas/6.2.x/protocol/CAS-Protocol.html
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
53
 */
M
v 3.2.0  
MaxKey 已提交
54
@Tag(name = "2-3-CAS API文档模块")
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
55
@Controller
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
56
public class CasAuthorizeEndpoint  extends CasBaseAuthorizeEndpoint{
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
57 58 59

	final static Logger _logger = LoggerFactory.getLogger(CasAuthorizeEndpoint.class);
	
M
v 3.2.0  
MaxKey 已提交
60
	@Operation(summary = "CAS页面跳转service认证接口", description = "传递参数service",method="GET")
61 62 63 64 65
	@GetMapping(CasConstants.ENDPOINT.ENDPOINT_LOGIN)
	public ModelAndView casLogin(@RequestParam(value=CasConstants.PARAMETER.SERVICE,required=false) String casService,
								 HttpServletRequest request,
								 HttpServletResponse response
			){
MaxKey单点登录官方's avatar
CAS FIX  
MaxKey单点登录官方 已提交
66
	    
67
		AppsCasDetails  casDetails = casDetailsService.getAppDetails(casService , true);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
68
		
69
		return buildCasModelAndView(request,response,casDetails,casService);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
70 71
	}
	
M
v 3.2.0  
MaxKey 已提交
72
	@Operation(summary = "CAS页面跳转应用ID认证接口", description = "传递参数应用ID",method="GET")
73 74 75 76 77
	@GetMapping(CasConstants.ENDPOINT.ENDPOINT_BASE + "/{id}")
	public ModelAndView authorize(  @PathVariable("id") String id,
									HttpServletRequest request,
									HttpServletResponse response
			){
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
78
		
79
		AppsCasDetails casDetails = casDetailsService.getAppDetails(id , true);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
80
		
81
		return buildCasModelAndView(request,response,casDetails,casDetails == null ? id : casDetails.getCallbackUrl());
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
82 83
	}
	
84 85 86 87 88 89 90 91 92 93
	private  ModelAndView buildCasModelAndView( HttpServletRequest request,
	                							HttpServletResponse response,
	                							AppsCasDetails casDetails,
	                							String casService){
		if(casDetails == null) {
			_logger.debug("service {} not registered  " , casService);
			ModelAndView modelAndView = new ModelAndView("authorize/cas_sso_submint");
			modelAndView.addObject("errorMessage", casService);
			return modelAndView;
		}
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
94
		
95
		_logger.debug("Detail {}" , casDetails);
96 97
		Map<String, String> parameterMap = WebContext.getRequestParameterMap(request);
		String service = casService;
M
MaxKey 已提交
98
		_logger.debug("CAS Parameter service = {}" , service);
99 100 101 102 103 104 105 106
		if(casService.indexOf("?") >-1 ) {
		    service = casService.substring(casService.indexOf("?") + 1);
		    if(service.indexOf("=") > -1) {
		        String [] parameterValues = service.split("=");
		        if(parameterValues.length == 2) {
		            parameterMap.put(parameterValues[0], parameterValues[1]);
		        }
		    }
M
MaxKey 已提交
107
		    _logger.debug("CAS service with Parameter : {}" , parameterMap);
108
		}
109
		WebContext.setAttribute(CasConstants.PARAMETER.PARAMETER_MAP, parameterMap);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
110 111
		WebContext.setAttribute(CasConstants.PARAMETER.ENDPOINT_CAS_DETAILS, casDetails);
		WebContext.setAttribute(WebConstants.SINGLE_SIGN_ON_APP_ID, casDetails.getId());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
112
		WebContext.setAttribute(WebConstants.AUTHORIZE_SIGN_ON_APP,casDetails);
M
MaxKey 已提交
113
		return WebContext.redirect(CasConstants.ENDPOINT.ENDPOINT_SERVICE_TICKET_GRANTING);
114
		
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
115 116
	}
	
M
MaxKey 已提交
117
	@RequestMapping(CasConstants.ENDPOINT.ENDPOINT_SERVICE_TICKET_GRANTING)
118 119 120 121
	public ModelAndView grantingTicket( Principal principal,
										HttpServletRequest request,
										HttpServletResponse response){
		ModelAndView modelAndView = new ModelAndView("authorize/cas_sso_submint");
M
MaxKey 已提交
122
		AppsCasDetails casDetails = (AppsCasDetails)WebContext.getAttribute(CasConstants.PARAMETER.ENDPOINT_CAS_DETAILS);
123
		
M
MaxKey 已提交
124
		ServiceTicketImpl serviceTicket = new ServiceTicketImpl(AuthorizationUtils.getAuthentication(),casDetails);
M
MaxKey 已提交
125 126
		
		_logger.trace("CAS start create ticket ... ");
M
MaxKey 已提交
127
		String ticket = ticketServices.createTicket(serviceTicket,casDetails.getExpires());
M
MaxKey 已提交
128
		_logger.trace("CAS ticket {} created . " , ticket);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
129
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
130 131 132 133 134
		StringBuffer callbackUrl = new StringBuffer(casDetails.getCallbackUrl());
		if(casDetails.getCallbackUrl().indexOf("?")==-1) {
		    callbackUrl.append("?");
		}
		
M
MaxKey 已提交
135
		if(callbackUrl.indexOf("&") != -1 ||callbackUrl.indexOf("=") != -1) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
136 137 138
		    callbackUrl.append("&");
		}
		
139 140 141 142 143 144 145 146 147 148 149 150 151 152
		//append ticket
		callbackUrl.append(CasConstants.PARAMETER.TICKET).append("=").append(ticket);
		
		callbackUrl.append("&");
		//append service
		callbackUrl.append(CasConstants.PARAMETER.SERVICE).append("=").append(casDetails.getService());
		
		//增加可自定义的参数
		if(WebContext.getAttribute(CasConstants.PARAMETER.PARAMETER_MAP)!=null) {
    		@SuppressWarnings("unchecked")
            Map <String, String> parameterMap = (Map <String, String>)WebContext.getAttribute(CasConstants.PARAMETER.PARAMETER_MAP);
    		parameterMap.remove(CasConstants.PARAMETER.TICKET);
    		parameterMap.remove(CasConstants.PARAMETER.SERVICE);
    		for (String key : parameterMap.keySet()) {
M
cas pr  
MaxKey 已提交
153
    		    callbackUrl.append("&").append(key).append("=").append(parameterMap.get(key));
154 155
    		}
		}
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
156
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
157
		if(casDetails.getLogoutType()==LogoutType.BACK_CHANNEL) {
M
MaxKey 已提交
158
			_logger.debug("CAS LogoutType BACK_CHANNEL ... ");
159
			String sessionId = AuthorizationUtils.getPrincipal().getSession().getId();
M
MaxKey 已提交
160
			_logger.trace("get session by id {} . ",sessionId);
M
MaxKey 已提交
161
		    Session session  = sessionManager.get(sessionId);
M
MaxKey 已提交
162
		    _logger.trace("current session {}  ",session);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
163 164
		    //set cas ticket as OnlineTicketId
		    casDetails.setOnlineTicket(ticket);
M
MaxKey 已提交
165
		    session.setAuthorizedApp(casDetails);
M
MaxKey 已提交
166
		    _logger.trace("session store ticket  {} .",ticket);
M
MaxKey 已提交
167
		    sessionManager.create(sessionId, session);
M
MaxKey 已提交
168
		    _logger.debug("CAS LogoutType session store ticket to AuthorizedApp .");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
169 170
		}
		
M
MaxKey 已提交
171
		_logger.debug("redirect to CAS Client URL {}" , callbackUrl);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
172 173
		modelAndView.addObject("callbackUrl", callbackUrl.toString());
		return modelAndView;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
174
	}
M
MaxKey 已提交
175 176 177 178 179 180 181 182
	
	/**
	 * for cas logout then redirect to logout
	 * @param request
	 * @param response
	 * @param casService
	 * @return
	 */
M
v 3.2.0  
MaxKey 已提交
183
	@Operation(summary = "CAS注销接口", description = "CAS注销接口",method="GET")
M
MaxKey 已提交
184
	@RequestMapping(CasConstants.ENDPOINT.ENDPOINT_LOGOUT)
185 186 187
	public ModelAndView logout(HttpServletRequest request , HttpServletResponse response,
			@RequestParam(value = CasConstants.PARAMETER.SERVICE , required = false) String casService){
		StringBuffer logoutUrl = new StringBuffer("force/logout");
M
MaxKey 已提交
188 189 190 191
		if(StringUtils.isNotBlank(casService)){
			logoutUrl.append("?").append("redirect_uri=").append(casService);
		}
		return WebContext.forward(logoutUrl.toString());
M
MaxKey 已提交
192
	}
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
193
}