简体   繁体   English

在CQ5中实现自定义AuthenticationHandler

[英]Implement Custom AuthenticationHandler in CQ5

I want to have a remote system to do the user authentication for our CQ5. 我想要一个远程系统来为我们的CQ5进行用户身份验证。 I'm guessing AuthenticationHandler on a path is the direction to go. 我猜测路径上的AuthenticationHandler是方向。 If so, how does AuthenticationHandler works in general. 如果是这样,AuthenticationHandler一般如何工作。 And, In CQ5, how I implement a Custom AuthenticationHandler? 而且,在CQ5中,我如何实现Custom AuthenticationHandler? How do I go about making it an OSGi bundle (or fragment bundle) and install it into CQ5? 如何将其作为OSGi包(或片段包)并将其安装到CQ5中?

If possible, some code sample with OSGi manifest is appreciated. 如果可能的话,一些带有OSGi清单的代码示例表示赞赏。

You can find a description of how the Sling AuthenticationHandler works here . 您可以在此处找到Sling AuthenticationHandler如何工作的说明。 Also you can take a look at the Sling FormAuthenticationHandler source for an example. 您还可以查看Sling FormAuthenticationHandler源代码示例。 You can see the details of the OSGi configuration in the POM file for the project, under the configuration for the maven-bundle-plugin. 您可以在maven-bundle-plugin的配置下,在项目的POM文件中查看OSGi配置的详细信息。

If you just need to check passwords or sync user accounts you can use a custom CQ5 LoginModule . 如果您只需要检查密码或同步用户帐户,则可以使用自定义CQ5 LoginModule

I would start by looking into the jackrabbit AbstractLoginModule http://jackrabbit.apache.org/api/2.4/org/apache/jackrabbit/core/security/authentication/AbstractLoginModule.html 我将首先查看长耳兔AbstractLoginModule http://jackrabbit.apache.org/api/2.4/org/apache/jackrabbit/core/security/authentication/AbstractLoginModule.html

I have example of a custom solution/fragment bundle that was written, but it has a lot of pieces. 我有一个写的自定义解决方案/片段包的例子,但它有很多部分。 We were implementing stuff from Gigya (social network login). 我们正在实施来自Gigya(社交网络登录)的内容。

We have a few other classes that implement the MyAbstractLoginModule. 我们还有一些实现MyAbstractLoginModule的其他类。 I can dig in further and get you more examples if you need. 如果您需要,我可以深入挖掘并为您提供更多示例。 Hopefully this can get you started down the right path. 希望这可以让你开始走正确的道路。

public abstract class MyAbstractLoginModule extends AbstractLoginModule {
    static private final Logger logger = LoggerFactory.getLogger(MyAbstractLoginModule.class);
    protected Session session;
    protected UserManager userManager;
    protected ValueFactory valueFactory;
    protected long tokenExpiration = 7200000L;

    @Override
    public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) {
        if (options.containsKey("tokenExpiration")) {
            try {
                this.tokenExpiration = Long.parseLong(options.get("tokenExpiration").toString());
                logger.debug("- Token expiration -> '" + this.tokenExpiration + "'");
            } catch (NumberFormatException e) {
                logger.warn("Unabled to parse token expiration: ", e);
            }
        }
        super.initialize(subject, callbackHandler, sharedState, options);
    }

    /**
    * Initiates the login module
    *
    * @param ch
    * @param ses
    * @param map
    * @throws LoginException
    */
    @Override
    protected void doInit(CallbackHandler ch, Session ses, Map map) throws LoginException {
        logger.trace("doInit");

        SessionImpl session = (SessionImpl) ses;

        try {
            this.session = session;
            this.userManager = session.getUserManager();
            this.valueFactory = session.getValueFactory();
        } catch (RepositoryException e) {
            throw new LoginException("Unable to retrieve principal editor: " + e.toString());
        }
    }

    /**
    * Impersonates users
    *
    * @param prncpl
    * @param c
    * @return
    * @throws RepositoryException
    * @throws LoginException
    */
    @Override
    protected boolean impersonate(Principal prncpl, Credentials c) throws RepositoryException, LoginException {
        Authorizable authrz = this.userManager.getAuthorizable(principal);
        if ((authrz == null) || (authrz.isGroup())) {
            return false;
        }
        Subject impersSubject = getImpersonatorSubject(credentials);
        User user = (User) authrz;
        if (user.getImpersonation().allows(impersSubject)) {
            return true;
        }
        throw new FailedLoginException("attempt to impersonate denied for " + principal.getName());
    }

    @Override
    protected boolean isPreAuthenticated(Credentials creds) {
        return false;
    }
}

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

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