CasAuthorizeEndpoint.java 7.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
/**
 * 
 */
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;

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
29
import org.maxkey.authn.SigninPrincipal;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
30
import org.maxkey.authn.online.OnlineTicket;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
31 32
import org.maxkey.authz.cas.endpoint.ticket.CasConstants;
import org.maxkey.authz.cas.endpoint.ticket.ServiceTicketImpl;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
33
import org.maxkey.authz.singlelogout.LogoutType;
M
MaxKey 已提交
34
import org.maxkey.entity.apps.AppsCasDetails;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
35 36 37 38
import org.maxkey.web.WebConstants;
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
39
import org.springframework.security.core.annotation.AuthenticationPrincipal;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
40 41 42 43 44
import org.springframework.stereotype.Controller;
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 已提交
45 46 47

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

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

	final static Logger _logger = LoggerFactory.getLogger(CasAuthorizeEndpoint.class);
	
M
v 3.2.0  
MaxKey 已提交
59
	@Operation(summary = "CAS页面跳转service认证接口", description = "传递参数service",method="GET")
M
MaxKey 已提交
60
	@RequestMapping(CasConstants.ENDPOINT.ENDPOINT_LOGIN)
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
61 62 63 64
	public ModelAndView casLogin(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value=CasConstants.PARAMETER.SERVICE,required=false) String casService){
MaxKey单点登录官方's avatar
CAS FIX  
MaxKey单点登录官方 已提交
65
	    
M
MaxKey 已提交
66
		AppsCasDetails  casDetails=casDetailsService.getAppDetails(casService , true);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
67
		
68
		return buildCasModelAndView(request,response,casDetails,casService);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
69 70
	}
	
M
v 3.2.0  
MaxKey 已提交
71
	@Operation(summary = "CAS页面跳转应用ID认证接口", description = "传递参数应用ID",method="GET")
M
MaxKey 已提交
72
	@RequestMapping(CasConstants.ENDPOINT.ENDPOINT_BASE + "/{id}")
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
73 74 75 76 77
	public ModelAndView authorize(
			HttpServletRequest request,
			HttpServletResponse response,
			@PathVariable("id") String id){
		
M
MaxKey 已提交
78
		AppsCasDetails casDetails=casDetailsService.getAppDetails(id , true);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
79
		
80
		return buildCasModelAndView(request,response,casDetails,casDetails.getCallbackUrl());
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
81 82
	}
	
83 84 85
	private  ModelAndView buildCasModelAndView(
	                HttpServletRequest request,
	                HttpServletResponse response,
86 87
	                AppsCasDetails casDetails,
	                String casService){
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
88 89
		
		_logger.debug(""+casDetails);
90 91
		Map<String, String> parameterMap = WebContext.getRequestParameterMap(request);
		String service = casService;
M
MaxKey 已提交
92
		_logger.debug("CAS Parameter service = {}" , service);
93 94 95 96 97 98 99 100
		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 已提交
101
		    _logger.debug("CAS service with Parameter : {}" , parameterMap);
102
		}
103 104
		WebContext.setAttribute(
    		        CasConstants.PARAMETER.PARAMETER_MAP, 
105
    		        parameterMap
106
		        );
107

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
108 109
		WebContext.setAttribute(CasConstants.PARAMETER.ENDPOINT_CAS_DETAILS, casDetails);
		WebContext.setAttribute(WebConstants.SINGLE_SIGN_ON_APP_ID, casDetails.getId());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
110
		WebContext.setAttribute(WebConstants.AUTHORIZE_SIGN_ON_APP,casDetails);
M
MaxKey 已提交
111
		return WebContext.redirect(CasConstants.ENDPOINT.ENDPOINT_SERVICE_TICKET_GRANTING);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
112 113
	}
	
M
MaxKey 已提交
114
	@RequestMapping(CasConstants.ENDPOINT.ENDPOINT_SERVICE_TICKET_GRANTING)
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
115 116
	public ModelAndView grantingTicket(Principal principal,
	        @AuthenticationPrincipal Object user,
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
117 118
			HttpServletRequest request,
			HttpServletResponse response){
M
MaxKey 已提交
119 120
		AppsCasDetails casDetails = (AppsCasDetails)WebContext.getAttribute(CasConstants.PARAMETER.ENDPOINT_CAS_DETAILS);
		ServiceTicketImpl serviceTicket = new ServiceTicketImpl(WebContext.getAuthentication(),casDetails);
M
MaxKey 已提交
121 122

		String ticket = ticketServices.createTicket(serviceTicket,casDetails.getExpires());
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
123
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
124 125 126 127 128
		StringBuffer callbackUrl = new StringBuffer(casDetails.getCallbackUrl());
		if(casDetails.getCallbackUrl().indexOf("?")==-1) {
		    callbackUrl.append("?");
		}
		
M
MaxKey 已提交
129
		if(callbackUrl.indexOf("&") != -1 ||callbackUrl.indexOf("=") != -1) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
130 131 132
		    callbackUrl.append("&");
		}
		
133 134 135 136 137 138 139 140 141 142 143 144 145 146
		//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 已提交
147
    		    callbackUrl.append("&").append(key).append("=").append(parameterMap.get(key));
148 149
    		}
		}
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
150
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
151
		if(casDetails.getLogoutType()==LogoutType.BACK_CHANNEL) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
152
		    String onlineTicketId = ((SigninPrincipal)WebContext.getAuthentication().getPrincipal()).getOnlineTicket().getTicketId();
M
MaxKey 已提交
153
		    OnlineTicket onlineTicket  = onlineTicketService.get(onlineTicketId);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
154 155
		    //set cas ticket as OnlineTicketId
		    casDetails.setOnlineTicket(ticket);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
156
		    onlineTicket.setAuthorizedApp(casDetails);
M
MaxKey 已提交
157
		    onlineTicketService.store(onlineTicketId, onlineTicket);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
158 159
		}
		
M
MaxKey 已提交
160
		_logger.debug("redirect to CAS Client URL {}" , callbackUrl);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
161
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
162 163 164
		ModelAndView modelAndView=new ModelAndView("authorize/cas_sso_submint");
		modelAndView.addObject("callbackUrl", callbackUrl.toString());
		return modelAndView;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
165
	}
M
MaxKey 已提交
166 167 168 169 170 171 172 173
	
	/**
	 * for cas logout then redirect to logout
	 * @param request
	 * @param response
	 * @param casService
	 * @return
	 */
M
v 3.2.0  
MaxKey 已提交
174
	@Operation(summary = "CAS注销接口", description = "CAS注销接口",method="GET")
M
MaxKey 已提交
175
	@RequestMapping(CasConstants.ENDPOINT.ENDPOINT_LOGOUT)
M
MaxKey 已提交
176 177 178 179 180 181
	public ModelAndView logout(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value=CasConstants.PARAMETER.SERVICE,required=false) String casService){
		return WebContext.redirect("/logout?reLoginUrl=" + casService);
	}
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
182
}