Apereo Cas - 自定义主体 ID 发布

Apereo Cas - Custom Principal Id release

我需要编辑将返回给 cas 客户端应用程序的主体 ID。

我需要编辑的主体是从我不拥有的外部 idp 返回的,其格式如下:

Id: idp autogenerated ID having no meaning for my setup
Attributes:
... Some attributes I do not need ...
**ImportantId**(this is the property I need)

我已经阅读了 official documentation 所以根据我的理解,我的服务 json 文件的相关部分应该是:

...       
"usernameAttributeProvider" : {
        "@class" : "com.mypackage.cas.principal.MyCustomRegisteredServiceUsernameProvider",
        "canonicalizationMode" : "NONE"

在我的自定义 UserNameProvider class 中,我需要访问数据库,以便根据我上面提到的 importantId 获取我的设置特定用户名,所以我需要做这样的事情:

public class MyCustomRegisteredServiceUsernameProvider implements RegisteredServiceUsernameAttributeProvider {

    @Autowired
    UsersService usersService;

    @Override
    public String resolveUsername(Principal principal, Service service, RegisteredService registeredService) {
        // String importantId = ...getting it from principal attributes.. 
        return usersService.getUserNameFromImportant(importantId);

    }   
}

问题是 Autowired 注释不幸地不起作用,因此我的 UsersService 未初始化导致运行时出现 NPE。

所以,首先我想知道为什么我的方法不起作用,其次我想知道实现此行为的 cas "official" 方法是什么?

Cas版本:5.3.4 Cas叠加模板

环境:W10 x64,Java8

部署在 Wildfly 12 上

So, firstly I would like to know why my approach is not working

...因为此类提供程序不是 spring 管理的 bean。它们是从 JSON 文档中找到的序列化对象,并且永远不会通过 Spring 运行时。

and secondly what is the cas "official" way to achieve this behavior?

一种可能的解决方法是删除注入的字段并在您的 resolveUsername 方法中执行此操作:

var appContext = ApplicationContextProvider.getApplicationContext();
var usersService = appContext.getBean("bean-name", UsersService.class);
return usersService.getUserNameFromImportant(importantId);