CasAuthorizeEndpoint.java 5.5 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
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;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
45 46 47
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
48 49
/**
 * @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
 */
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
52
@Api(tags = "CAS API文档模块")
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
53
@Controller
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
54
public class CasAuthorizeEndpoint  extends CasBaseAuthorizeEndpoint{
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
55 56 57

	final static Logger _logger = LoggerFactory.getLogger(CasAuthorizeEndpoint.class);
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
58
	@ApiOperation(value = "CAS页面跳转service认证接口", notes = "传递参数service",httpMethod="GET")
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
59 60 61 62 63
	@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单点登录官方 已提交
64 65
	    
		AppsCasDetails  casDetails=casDetailsService.getAppDetails(casService);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
66
		
67
		return buildCasModelAndView(request,response,casDetails);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
68 69 70
		
	}
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
71
	@ApiOperation(value = "CAS页面跳转应用ID认证接口", notes = "传递参数应用ID",httpMethod="GET")
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
72 73 74 75 76 77
	@RequestMapping("/authz/cas/{id}")
	public ModelAndView authorize(
			HttpServletRequest request,
			HttpServletResponse response,
			@PathVariable("id") String id){
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
78
		AppsCasDetails casDetails=casDetailsService.getAppDetails(id);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
79
		
80
		return buildCasModelAndView(request,response,casDetails);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
81 82
	}
	
83 84 85 86
	private  ModelAndView buildCasModelAndView(
	                HttpServletRequest request,
	                HttpServletResponse response,
	                AppsCasDetails casDetails){
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
87 88 89
		
		_logger.debug(""+casDetails);

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