CasAuthorizeEndpoint.java 5.4 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;
MaxKey单点登录官方's avatar
m-11/6  
MaxKey单点登录官方 已提交
34
import org.maxkey.domain.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 40 41
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
42 43 44 45 46 47 48 49
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;

/**
 * @author Crystal.Sea
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
50
 * https://apereo.github.io/cas/6.2.x/protocol/CAS-Protocol.html
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
51 52
 */
@Controller
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
53
public class CasAuthorizeEndpoint  extends CasBaseAuthorizeEndpoint{
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
54 55 56 57 58 59 60 61

	final static Logger _logger = LoggerFactory.getLogger(CasAuthorizeEndpoint.class);
	
	@RequestMapping("/authz/cas/login")
	public ModelAndView casLogin(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value=CasConstants.PARAMETER.SERVICE,required=false) String casService){
MaxKey单点登录官方's avatar
CAS FIX  
MaxKey单点登录官方 已提交
62 63
	    
		AppsCasDetails  casDetails=casDetailsService.getAppDetails(casService);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
64
		
65
		return buildCasModelAndView(request,response,casDetails);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
66 67 68 69 70 71 72 73 74
		
	}
	
	@RequestMapping("/authz/cas/{id}")
	public ModelAndView authorize(
			HttpServletRequest request,
			HttpServletResponse response,
			@PathVariable("id") String id){
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
75
		AppsCasDetails casDetails=casDetailsService.getAppDetails(id);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
76
		
77
		return buildCasModelAndView(request,response,casDetails);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
78 79
	}
	
80 81 82 83
	private  ModelAndView buildCasModelAndView(
	                HttpServletRequest request,
	                HttpServletResponse response,
	                AppsCasDetails casDetails){
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
84 85 86
		
		_logger.debug(""+casDetails);

87 88 89 90
		WebContext.setAttribute(
    		        CasConstants.PARAMETER.PARAMETER_MAP, 
    		        WebContext.getRequestParameterMap(request)
		        );
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
91 92
		WebContext.setAttribute(CasConstants.PARAMETER.ENDPOINT_CAS_DETAILS, casDetails);
		WebContext.setAttribute(WebConstants.SINGLE_SIGN_ON_APP_ID, casDetails.getId());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
93
		WebContext.setAttribute(WebConstants.AUTHORIZE_SIGN_ON_APP,casDetails);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
94 95 96 97
		return WebContext.redirect("/authz/cas/granting");
	}
	
	@RequestMapping("/authz/cas/granting")
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
98 99
	public ModelAndView grantingTicket(Principal principal,
	        @AuthenticationPrincipal Object user,
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
100 101 102
			HttpServletRequest request,
			HttpServletResponse response){
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
103
	    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
MaxKey单点登录官方's avatar
m-11/6  
MaxKey单点登录官方 已提交
104
		AppsCasDetails casDetails=(AppsCasDetails)WebContext.getAttribute(CasConstants.PARAMETER.ENDPOINT_CAS_DETAILS);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
105 106 107 108
		ServiceTicketImpl serviceTicket=new ServiceTicketImpl(WebContext.getAuthentication(),casDetails);
		
		String ticket=ticketServices.createTicket(serviceTicket);
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
109 110 111 112 113
		StringBuffer callbackUrl = new StringBuffer(casDetails.getCallbackUrl());
		if(casDetails.getCallbackUrl().indexOf("?")==-1) {
		    callbackUrl.append("?");
		}
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
114 115 116 117
		if(callbackUrl.indexOf("&") != -1) {
		    callbackUrl.append("&");
		}
		
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
		//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()) {
    		    callbackUrl.append("&").append(key).append(parameterMap.get(key));
    		}
		}
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
135
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
136
		if(casDetails.getLogoutType()==LogoutType.BACK_CHANNEL) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
137
		    String onlineTicketId = ((SigninPrincipal)WebContext.getAuthentication().getPrincipal()).getOnlineTicket().getTicketId();
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
138
		    OnlineTicket onlineTicket  = onlineTicketServices.get(onlineTicketId);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
139 140
		    //set cas ticket as OnlineTicketId
		    casDetails.setOnlineTicket(ticket);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
141 142 143 144
		    onlineTicket.setAuthorizedApp(casDetails);
		    onlineTicketServices.store(onlineTicketId, onlineTicket);
		}
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
145 146 147
		_logger.debug("redirect to CAS Client URL " + callbackUrl);
		
		return WebContext.redirect(callbackUrl.toString());
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
148 149
	}
}