use of org.maxkey.authz.cas.endpoint.ticket.ServiceTicketImpl in project MaxKey by dromara.
the class CasAuthorizeEndpoint method grantingTicket.
@RequestMapping(CasConstants.ENDPOINT.ENDPOINT_SERVICE_TICKET_GRANTING)
public ModelAndView grantingTicket(Principal principal, @AuthenticationPrincipal Object user, HttpServletRequest request, HttpServletResponse response) {
AppsCasDetails casDetails = (AppsCasDetails) WebContext.getAttribute(CasConstants.PARAMETER.ENDPOINT_CAS_DETAILS);
ServiceTicketImpl serviceTicket = new ServiceTicketImpl(WebContext.getAuthentication(), casDetails);
String ticket = ticketServices.createTicket(serviceTicket, casDetails.getExpires());
StringBuffer callbackUrl = new StringBuffer(casDetails.getCallbackUrl());
if (casDetails.getCallbackUrl().indexOf("?") == -1) {
callbackUrl.append("?");
}
if (callbackUrl.indexOf("&") != -1 || callbackUrl.indexOf("=") != -1) {
callbackUrl.append("&");
}
// 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("=").append(parameterMap.get(key));
}
}
if (casDetails.getLogoutType() == LogoutType.BACK_CHANNEL) {
String onlineTicketId = ((SigninPrincipal) WebContext.getAuthentication().getPrincipal()).getOnlineTicket().getTicketId();
OnlineTicket onlineTicket = onlineTicketServices.get(onlineTicketId);
// set cas ticket as OnlineTicketId
casDetails.setOnlineTicket(ticket);
onlineTicket.setAuthorizedApp(casDetails);
onlineTicketServices.store(onlineTicketId, onlineTicket);
}
_logger.debug("redirect to CAS Client URL {}", callbackUrl);
ModelAndView modelAndView = new ModelAndView("authorize/cas_sso_submint");
modelAndView.addObject("callbackUrl", callbackUrl.toString());
return modelAndView;
}
use of org.maxkey.authz.cas.endpoint.ticket.ServiceTicketImpl in project MaxKey by dromara.
the class CasRestV1Endpoint method requestServiceTicket.
@Operation(summary = "CAS REST认证接口", description = "通过TGT获取ST", method = "POST")
@RequestMapping(value = CasConstants.ENDPOINT.ENDPOINT_REST_TICKET_V1 + "/{ticketGrantingTicket}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> requestServiceTicket(HttpServletRequest request, HttpServletResponse response, @PathVariable("ticketGrantingTicket") String ticketGrantingTicket, @RequestParam(value = CasConstants.PARAMETER.SERVICE, required = false) String casService, @RequestParam(value = CasConstants.PARAMETER.RENEW, required = false) String renew, @RequestParam(value = CasConstants.PARAMETER.REST_USERNAME, required = false) String username, @RequestParam(value = CasConstants.PARAMETER.REST_PASSWORD, required = false) String password) {
try {
TicketGrantingTicketImpl ticketGrantingTicketImpl = (TicketGrantingTicketImpl) casTicketGrantingTicketServices.get(ticketGrantingTicket);
AppsCasDetails casDetails = casDetailsService.getAppDetails(casService, true);
ServiceTicketImpl serviceTicket = new ServiceTicketImpl(ticketGrantingTicketImpl.getAuthentication(), casDetails);
String ticket = ticketServices.createTicket(serviceTicket);
return new ResponseEntity<>(ticket, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
}
return new ResponseEntity<>("", HttpStatus.BAD_REQUEST);
}
Aggregations