首页 > 解决方案 > 在节点中使用 CAS 身份验证对虚拟服务进行身份验证

问题描述

我正在尝试制作一个使用我大学的 CAS 身份验证来登录用户的 Web 应用程序。后端是使用 Express 编写的,localhost:5000目前正在运行。我已使用该cas-authentication软件包为我处理身份验证。使用其 npm 页面中的示例代码:

var app = require('express')();
var session = require('express-session');
var CASAuthentication = require('cas-authentication');
 
// Set up an Express session, which is required for CASAuthentication.
app.use( session({
    secret            : 'super secret key',
    resave            : false,
    saveUninitialized : true
}));
 
// Create a new instance of CASAuthentication.
var cas = new CASAuthentication({
    cas_url     : 'https://my-cas-host.com/cas',
    service_url : 'https://my-service-host.com'
});
 
// Unauthenticated clients will be redirected to the CAS login and then back to
// this route once authenticated.
app.get( '/app', cas.bounce, function ( req, res ) {
    res.send( '<html><body>Hello!</body></html>' );
});
 
// Unauthenticated clients will receive a 401 Unauthorized response instead of
// the JSON data.
app.get( '/api', cas.block, function ( req, res ) {
    res.json( { success: true } );
});
 
// An example of accessing the CAS user session variable. This could be used to
// retrieve your own local user records based on authenticated CAS username.
app.get( '/api/user', cas.block, function ( req, res ) {
    res.json( { cas_user: req.session[ cas.session_name ] } );
});
 
// Unauthenticated clients will be redirected to the CAS login and then to the
// provided "redirectTo" query parameter once authenticated.
app.get( '/authenticate', cas.bounce_redirect );
 
// This route will de-authenticate the client with the Express server and then
// redirect the client to the CAS logout page.
app.get( '/logout', cas.logout );

app.listen(5000);

我的问题是,如果我将我大学的 CAS 门户的 url 作为对象cas_url放入CASAuthentication,那么我应该放入什么service_url

我试着放在localhost:5000那里,但是当我启动应用程序并转到 时localhost:5000/app,它会将我重定向到一个页面,上面写着“应用程序未授权使用 CAS”。取而代之的是,将我大学的 moodle 站点放在它的位置不会出现此问题,并在身份验证后重定向到 moodle(我们大学的 Moodle 也使用 CAS)。

我知道这是一种防止恶意服务使用 CAS 的安全功能,但这是否意味着仍处于早期开发阶段的每项服务都必须向 CAS 注册?或者有没有一些我可以在不需要与大学管理员交谈的情况下使用的工作?

标签: javascriptnode.jsexpressauthenticationcas

解决方案


来自CAS 文档

当在您的 CAS 服务注册表中找不到请求的应用程序/服务 url 时,您可能会遇到此错误。当向 CAS 登录端点提交身份验证请求时,目标应用程序将显示为 url 参数,该参数将根据 CAS 服务注册表进行检查,以确定是否允许应用程序使用 CAS。如果未找到该 url,则将显示此消息。由于注册表中的服务定义能够由 url 模式定义,因此完全有可能注册表中的服务定义模式配置错误,并且不会成功匹配请求的应用程序 url。

所以,

这是否意味着仍处于早期开发阶段的每项服务都必须在 CAS 注册?

是的。如果你想使用别人的系统,你需要他们的许可,无论如何。

或者有没有一些我可以在不需要与大学管理员交谈的情况下使用的工作?

没有解决方法。但是,您可以设置自己的 CAS 服务器。


推荐阅读