AS
Authentication Service:认证服务,发放TGT
KDC
Key Distribution Center:密钥发放中心
TGS
Ticket-Granting Service:票据授权服务,索取TGT,发放ST
TGC
ticket-granting cookie:授权的票据证明,由CAS Server通过SSL方式发送给终端用户。该值存在Cookie中,根据TGC可以找到TGT。
TGT
Ticket Granting tieckt:俗称大令牌,或者票根,由KDC和AS发放,获取该票据后,可直接申请其他服务票据ST,不需要提供身份认证信息
ST
Service Ticket:服务票据,由KDC的TGS发放,ST是访问server内部的令牌
net.unicon.cas cas-client-autoconfig-support2.3.0-GA
#CAS服务器的URL前缀。指定CAS服务器的地址和端口号。 cas.server-url-prefix=http://192.168.3.96:8199/cas #CAS服务器的登录页面URL。指定CAS登录页面的完整地址。 cas.server-login-url=http://192.168.3.96:8199/cas/login # CAS客户端的主机URL。指定CAS客户端的地址和端口号。 cas.client-host-url=http://localhost:9003 #:CAS验证URL的模式。指定需要进行CAS认证的URL模式,多个模式之间使用逗号分隔。 cas.authentication-url-patterns=/,/index,/base/login,/index/ #CAS客户端的本地URL。在CAS登录成功后,会跳转回CAS客户端的这个地址。service参数用来指定登录成功后要返回的URL。 local.url=http://192.168.3.96:8199/cas/login?service=http://localhost:9003/cas/index #应用程序的HTTP端口号。指定应用程序运行的HTTP端口。 server.httpPort= 9003
package com.xk.common.web; import com.xk.common.core.user.dao.CapUserRepository; import com.xk.common.core.user.model.dto.CapUserDto; import com.xk.common.core.user.model.entity.CapUserEntity; import com.xk.framework.common.Constants; import net.unicon.cas.client.configuration.CasClientConfigurationProperties; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; @Controller("indexController") public class IndexController { protected Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private AuthenticationManager authenticationManager; @Autowired private CapUserRepository capUserRepository; @RequestMapping("/") public String toIndexDefault(HttpServletRequest request) { CapUserDto capUserDto = (CapUserDto) request.getSession().getAttribute(Constants.XK_SESSION_USER); // 如果当前没有登录,session中没有会话,代表非法访问 if (capUserDto == null) { logger.info(">>>>>>>>>>>>>>当前没有登录,session中没有会话,代表非法访问<<<<<<<<<<<<<<<"); return "error/404"; } CapUserEntity capUser=capUserRepository.xkFindById(capUserDto.getId()); String oldPwd=null; BCryptPasswordEncoder bc = new BCryptPasswordEncoder(4); if(null!=capUser && StringUtils.isNotEmpty(capUser.getId())){ oldPwd=capUser.getPassword(); capUser.setPassword(bc.encode("wgs1234")); }else{ logger.info(">>>>>>>>>>>>>>当前没有登录,session中没有会话,代表非法访问<<<<<<<<<<<<<<<"); return "error/404"; } UsernamePasswordAuthenticationToken token=new UsernamePasswordAuthenticationToken(capUserDto.getUserId(),"wgs1234"); Authentication authentication=authenticationManager.authenticate(token); SecurityContextHolder.getContext().setAuthentication(authentication); request.getSession().setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext()); // 从session中获取用户信息,然后根据返回的usertype,跳转到不同的逻辑页面 //CapUserDto wgsUserInfoDto = capUserService.getUserById(capUserDto.getUserId()); // 如果当前登录,但是没有找到匹配的记录 /*if (wgsUserInfoDto == null) { logger.info(">>>>>>>>>>>>>>当前登录存在session会话,但是没有找到匹配的记录,userid:{}<<<<<<<<<<<<<<<", capUserDto.getUserId()); return "error/404"; }*/ //userService.loadUserByUsername(capUserDto.getUserName()); request.getSession().setAttribute("XK_USER", capUserDto); capUser.setPassword(oldPwd); return "admin/home"; } @GetMapping("/home") public String toIndex(HttpServletRequest request) { return toIndexDefault(request); } @Autowired private CasClientConfigurationProperties casClientConfigurationProperties; /** * 本地应用(CASClient)退出登录 * @param request * @return */ @GetMapping("/portal/logout") public String toLogout(HttpServletRequest request) { //销毁本地应用的Session request.getSession().invalidate(); //直接跳转至CASServer的注销地址,并带上本地应用的主页地址,便于再次登录后返回至该应用 return "redirect:" + casClientConfigurationProperties.getServerUrlPrefix() + "/logout?service=" + casClientConfigurationProperties.getClientHostUrl() ; } }
package com.xk.wgs.cas; import com.google.common.collect.ImmutableMap; import com.xk.common.core.organize.model.dto.EmployeeDto; import com.xk.common.core.user.model.dto.CapRoleDto; import com.xk.common.core.user.model.dto.CapUserDto; import com.xk.framework.common.Constants; import com.xk.platform.core.organize.service.IEmployeeQueryService; import com.xk.platform.security.user.service.CustomUserDetailsService; import com.xk.platform.security.user.service.ICapRoleService; import com.xk.platform.security.user.service.ICapUserService; import org.jasig.cas.client.authentication.AttributePrincipal; import org.jasig.cas.client.util.AssertionHolder; import org.jasig.cas.client.validation.Assertion; import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.List; import java.util.Map; public class CasUserReceivingTicketValidationFilter extends Cas20ProxyReceivingTicketValidationFilter { public CasUserReceivingTicketValidationFilter() { super(); } private ICapUserService capUserService; private ICapRoleService capRoleQueryService; private IEmployeeQueryService employeeQueryService; @Resource private CustomUserDetailsService userDetailsService; public CasUserReceivingTicketValidationFilter(ICapUserService capUserService, ICapRoleService capRoleQueryService,IEmployeeQueryService employeeQueryService) { super(); this.capUserService = capUserService; this.capRoleQueryService = capRoleQueryService; this.employeeQueryService = employeeQueryService; } @Override public void onSuccessfulValidation(HttpServletRequest request, HttpServletResponse response, Assertion assertion) { AttributePrincipal principal = assertion.getPrincipal(); logger.info(principal.getName() + "--------------------------------"); AssertionHolder.setAssertion(assertion); //获取用户信息 CapUserDto capUserDto = capUserService.getUserByUserid(principal.getName()); capUserDto.setPassword(""); String roleId = ""; // 获取用户登录信息 // 获取当前用户授予的角色 if (capUserDto == null) { logger.error(">>>>>>>>>>>>>>>>>>>单点登录成功后的门户无此人信息,登录账号:{}<<<<<<<<<<<<<<", principal.getName()); } else { // 根据学号/工号,查询所属的员工,再查员工授予的角色 EmployeeDto empByUserId = employeeQueryService.getEmpByUserId(capUserDto.getId()); // 查询员工授予的角色 ListcapRoleDtos = capRoleQueryService.findRolesByPartyIdAndPartyType(empByUserId.getId(), "EMP"); if (capRoleDtos != null && capRoleDtos.size() > 0) { roleId = capRoleDtos.get(0).getId(); logger.info(">>>>>>>查询到角色id:{}",roleId); } } // 这里一般只有一个角色,确定; // 生成令牌 Map map = ImmutableMap.of("userid", principal.getName(), "roleId", roleId); String jwt = RTokenAuthenticationService.genAuthentication(response, principal.getName(), map); //userDetailsService.loadUserByUsername(capUserDto.getUserId()); request.getSession().setAttribute(Constants.XK_SESSION_USER, capUserDto); request.getSession().setAttribute("XK_TOKEN", jwt); request.getSession().setAttribute("XK_USERID", principal.getName()); } @Override public void onFailedValidation(HttpServletRequest request, HttpServletResponse response) { logger.info("Failed to validate cas ticket"); } }
该部分代码因涉及到token的生成及其他业务流程,不适用于其他项目,仅为提供参考思路。
退出登录时通过a标签跳转/portal/logout,重定向到cas销毁地址,实现退出登录。
访问应用,跳转到cas登陆页面,输入账号密码后登陆成功。
点击退出登录
上一篇:如何查看nginx日志