CAS 5.3.12注册后自动登录

本文为原创,参考文章:https://skyfar666.iteye.com/blog/2309505

本功能要实现在第三方页面注册后,做CAS的静默登录(自动登录)功能。
使用的CAS版本为5.3.12,此版本为JDK1.8的最新版本。

在support下,新建一个cas-server-support-xxxx的模块,
在模块下,新建如下类,包名以org.apereo.cas.web为前缀,这样能被扫描到

@Controller
@RequestMapping("/registerLogin")
public class RegisterAutoLoginController {


    @Autowired
    @Qualifier("webApplicationServiceFactory")
    private ServiceFactory webApplicationServiceFactory;

    @Autowired
    @Qualifier("centralAuthenticationService")
    private CentralAuthenticationService centralAuthenticationService;

    @Autowired
    @Qualifier("defaultAuthenticationSystemSupport")
    private AuthenticationSystemSupport authenticationSystemSupport;

    @Autowired
    @Qualifier("ticketGrantingTicketCookieGenerator")
    private CookieRetrievingCookieGenerator cookieRetrievingCookieGenerator;

    @Autowired
    @Qualifier("customAuthenticationHandler")
    private CustomUserAuthHandler customAuthenticationHandler;

    /**
     * Gets default view.
     *
     * @param request  the request
     * @param response the response
     * @return the default view
     * @throws Exception the exception
     */
    @GetMapping
    public ModelAndView getDefaultView(final HttpServletRequest request,
                                       final HttpServletResponse response) throws Exception {

        String username=request.getParameter("username");
        String password=request.getParameter("password");

        try {
            username = new String(new BASE64Decoder().decodeBuffer(username));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            password = new String(new BASE64Decoder().decodeBuffer(password));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 检查用户名和密码的有效性
        boolean checkAuthValidate = customAuthenticationHandler.authValidate(username, password);
        if (checkAuthValidate) {
            bindTGT(username, password, request, response);
        }

        String service = ServletRequestUtils.getStringParameter(request, "service", "");
        String viewName =  ("redirect:login" + (service.length() > 0 ? "?service=" + service : ""));

        return new ModelAndView(viewName);

    }

    /**
     * 设置TGT的Cookie
     * @param username
     * @param password
     * @param request
     * @param response
     */
    private void bindTGT(String username, String password, HttpServletRequest request, HttpServletResponse response) {
        UsernamePasswordCredential credential = new UsernamePasswordCredential();
        credential.setUsername(username);
        credential.setPassword(password);
        Service service = this.webApplicationServiceFactory.createService(request);
        AuthenticationResult authenticationResult =
                authenticationSystemSupport.handleAndFinalizeSingleAuthenticationTransaction(service, credential);
        String tgt = centralAuthenticationService.createTicketGrantingTicket(authenticationResult).getId();
        cookieRetrievingCookieGenerator.addCookie(request, response, tgt);
    }
}

build.gradle依赖如下:

description = "注册后自动登录"
dependencies {
    api project(":api:cas-server-core-api")

    implementation libraries.metrics

    implementation project(":core:cas-server-core-util-api")
    implementation project(":core:cas-server-core-web-api")
    implementation project(":core:cas-server-core-audit-api")
    implementation project(":core:cas-server-core-monitor")
    implementation project(":core:cas-server-core-authentication")
    implementation project(":core:cas-server-core-authentication-api")
    implementation project(":core:cas-server-core-validation")
    implementation project(":core:cas-server-core-configuration-api")
    implementation project(":core:cas-server-core-validation-api")
    implementation project(":core:cas-server-core-cookie-api")

    compileOnly project(":support:cas-server-support-trusted-mfa")
    compileOnly project(":core:cas-server-core-events")
    compileOnly project(":support:cas-server-support-discovery-profile")

    testImplementation project(":core:cas-server-core")
    testImplementation project(":core:cas-server-core-logout-api")
    testImplementation project(":core:cas-server-core-logout")
    testImplementation project(":core:cas-server-core-monitor")
    testImplementation project(":core:cas-server-core-cookie")
    testImplementation project(":core:cas-server-core-services-authentication")
    testImplementation project(":core:cas-server-core-tickets")
    testImplementation project(":core:cas-server-core-services")
    testImplementation project(":core:cas-server-core-tickets-api")
    testImplementation project(":core:cas-server-core-authentication")
    testImplementation project(":core:cas-server-core-web")
    testImplementation project(":core:cas-server-core-audit")
    testImplementation project(":core:cas-server-core-util")
    testImplementation project(":core:cas-server-core-configuration")
    testImplementation project(":core:cas-server-core-web-api")
    testImplementation project(":core:cas-server-core-events")
    testImplementation project(":support:cas-server-support-validation")
    testImplementation project(":support:cas-server-support-metrics")
    testImplementation project(":support:cas-server-support-discovery-profile")
    testImplementation project(":support:cas-server-support-trusted-mfa")

    testImplementation project(path: ":core:cas-server-core", configuration: "tests")
    testImplementation project(path: ":core:cas-server-core-authentication-api", configuration: "tests")
    testImplementation project(path: ":core:cas-server-core-authentication", configuration: "tests")
    testImplementation project(path: ":core:cas-server-core-validation", configuration: "tests")
    testImplementation project(path: ":core:cas-server-core-services", configuration: "tests")
}

在setting.gradle引入该包的依赖

include "support:cas-server-support-xxx"

在webapp的webapp.gradle引入该包的依赖

implementation project(":support:cas-server-support-")

准备完毕后,启动服务。
在第三方页面注册后,访问如下地址,即可自动登录:
http://cas-server:8050/cas/registerLogin?username=xxxxxx&password=xxxx&service=http://self-sever/test

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值