我需要编辑将返回给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)我已经阅读了正式文件,因此根据我的理解,服务json文件的相关部分应该是:
...
"usernameAttributeProvider" : {
"@class" : "com.mypackage.cas.principal.MyCustomRegisteredServiceUsernameProvider",
"canonicalizationMode" : "NONE"在我的自定义UserNameProvider类中,我需要访问db,以便根据我前面提到的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的“官方”方式是如何实现这一行为的?
Cas版本: 5.3.4 Cas覆盖模板
环境: W10 x64,Java 8
部署在野蝇12上
发布于 2018-11-09 22:29:19
所以,首先我想知道为什么我的方法不起作用。
...because这样的提供者不是春管理bean.它们是从JSON文档中找到的序列化对象,而且从未经过Spring运行时。
其次,cas的“官方”实现这种行为的方式是什么?
一个可能的解决办法是删除注入的字段,并在resolveUsername方法中这样做:
var appContext = ApplicationContextProvider.getApplicationContext();
var usersService = appContext.getBean("bean-name", UsersService.class);
return usersService.getUserNameFromImportant(importantId);https://stackoverflow.com/questions/53203932
复制相似问题