简体   繁体   English

如何在 Portlet 中使用 Liferay 内部代码?

[英]How to work with Liferay internal code in portlets?

Previously I asked a Question on how to access session data that was set by liferays internal OIDC process.之前我问了一个关于如何访问由 liferays 内部 OIDC 流程设置的会话数据的问题。 In the end I accessed the token using java reflection but I cannot believe that this is the right or only way to do it.最后,我使用 java 反射访问了令牌,但我无法相信这是正确的或唯一的方法。

I added the following dependencies hoping I would then be able to just directly use the OpenIdConnectSessionImpl but this way I cannot add the portlet to my liferay.我添加了以下依赖项,希望我能够直接使用 OpenIdConnectSessionImpl 但这样我无法将 portlet 添加到我的 liferay。

compileOnly 'com.liferay:com.liferay.portal.security.sso.openid.connect.impl'
compileOnly group: 'com.nimbusds', name: 'oauth2-oidc-sdk', version: '5.17.1'

This is the code I use:这是我使用的代码:

Object oidcSession = httpSession.getAttribute("OPEN_ID_CONNECT_SESSION");

if (oidcSession instanceof OpenIdConnectSessionImpl) {
  OpenIdConnectSessionImpl openIdConnectSessionImpl = (OpenIdConnectSessionImpl) oidcSession;

  LOG.info(openIdConnectSessionImpl.getUserInfo().toJSONObject().toString(new JSONStyle(4)));
} else {
  LOG.info("What is this.");
}

Getting the token from oidcSession using reflection works but not using the actual Class.使用反射从oidcSession获取令牌有效但不使用实际类。

The beauty of Liferay's modularity through OSGi is that you can have access to everything that's designed for your access (eg exported) and nothing that's explicitly not exported. Liferay 通过 OSGi 实现模块化的美妙之处在于,您可以访问为您的访问而设计的所有内容(例如导出),而没有明确未导出的内容。 Classes ending in Impl typically are among the nonexported classes.Impl结尾的类通常属于非导出类。

You can still create access, but you'll know explicitly that you're going way beyond the published API, and you make yourself dependent on the implementation.您仍然可以创建访问权限,但您会明确地知道您正在超越已发布的 API,并且您让自己依赖于实现。 Guess who's also changing the implementation, without taking care of anybody else?猜猜谁也在改变实施,而不照顾其他人? The upstream project.上游项目。 Liferay's developers are working hard to keep an API stable, but it's their business to change their implementation. Liferay 的开发人员正在努力保持 API 的稳定,但改变他们的实现是他们的职责。

If you decide that you'll need access to the implementation: You're welcome, open up those modules.如果您决定需要访问实现:不客气,打开这些模块。 Just be aware that every single future (minor) upgrade might change an aspect of the implementation that you're relying on.请注意,未来的每一次(次要)升级都可能会改变您所依赖的实现的一个方面。 You're willingly accepting a high maintenance burden for the future.您愿意为未来接受高维护负担。 For some features this is fine, and you may be willing to pay that price.对于某些功能,这很好,您可能愿意为此付出代价。 Other features might not justify this price.其他功能可能无法证明这个价格是合理的。 Choose wisely.做出明智的选择。

There is no general answer to this question, just a bunch of rules of thumb:这个问题没有通用的答案,只有一堆经验法则:

  • Re-phrase the problem, to search for an alternative solution.重新表述问题,寻找替代解决方案。 Sometimes a solution that takes longer for its first shot is a lot easier to maintain in the long run有时,从长远来看,第一次拍摄需要更长时间的解决方案更容易维护
  • Make the cost of an intrusive change visible to the business stakeholders.使业务利益相关者可以看到侵入性更改的成本。 I've seen many feature requests vanish or change significantly, when they had a price tag我看到许多功能请求在标有价格标签后消失或发生了显着变化
  • Search for alternative implementations along an API沿 API 搜索替代实现
  • Describe your (business) problem (not the solution) to the Liferay developers: They may introduce an extension point in the future, so that you can replace your intrusive change of a module's implementation with an API-based module in the next version.向 Liferay 开发人员描述您的(业务)问题(而不是解决方案):他们将来可能会引入一个扩展点,以便您可以在下一个版本中使用基于 API 的模块替换对模块实现的侵入性更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM