2

我有一个使用 Spring 安全性的 grails 应用程序(2.5.0),并使用 spring-security-ldap 插件(2.0-RC2)针对 Windows AD 域进行身份验证。

这非常适合进行身份验证,但现在我需要允许用户更改他们的密码(实际上是需要它!)。

尽管搜索了文档,阅读代码并使用谷歌搜索我能找到的只是对 LdapUserDetailsManager.changePassword 的引用,但我找不到如何使用它的单个示例。

我在插件里找到

public class GrailsLdapUserDetailsManager extends LdapUserDetailsManager 
    implements GrailsUserDetailsService {....

但这没有changePassword,如果有,我不明白如何调用它。

我查看了所有 StackOverflow 问题,例如

如何使用spring ldap和spring security更改密码

但是答案似乎是用其他语言编写的,并且谈论了我没有的东西,例如 xml 文件。

有人可以告诉我,最好是一个可以理解的例子,我可以如何结合 grails spring-security-ldap 插件在 Grails 中针对 ldap AD 源实现更改密码功能?无法管理更改密码的身份验证是错误的!

4

1 回答 1

1

您可以通过注入控制器来使用 ldapUserDetailsManager,例如

普惠制:

<!DOCTYPE html>
<html>
    <head>
        <meta name="layout" content="main">
        <title><g:message code="menu.item.change.password" /></title>
    </head>
    <body>
        <div class="maincontentdiv" role="main">

        <div class="alert alert-info alert-dismissible" role="alert">
          <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          flash.changePasswordMessage
        </div>

            <h3><g:message code="menu.item.change.password" /></h3>

            <g:form class="form-horizontal">

                <div class="form-group">
                    <label class="col-md-4 control-label" for="currentPassword">Current password</label>
                    <div class="col-md-4">
                        <g:field type="password" name="currentPassword" class="form-control" required="true" />
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-4 control-label" for="newPassword">New password</label>
                    <div class="col-md-4">
                        <g:field type="password" name="newPassword" class="form-control" pattern=".{6,15}" required title="Password must be a minimum of 6 and a maximum of to 10 characters" />
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-4 control-label" for="confirmNewPassword">Confirm new password</label>
                    <div class="col-md-4">
                        <g:field type="password" name="confirmNewPassword" class="form-control" pattern=".{6,15}" required title="Password must be a minimum of 6 and a maximum of to 10 characters" />
                    </div>
                </div>

                <g:render template="/templates/generic_submit_button" model="[btnname: 'changePassword', btntxt: 'Change password']" />

            </g:form>

        </div>
    </body>
</html>

控制器:

class ChangePasswordController {

    def ldapUserDetailsManager

    def index() {
        if ( params.changePassword ) {
            try {
                if ( params.newPassword.equals( params.confirmNewPassword ) ) {
                    ldapUserDetailsManager.changePassword( params.currentPassword, params.newPassword )
                }
                else { 
                    throw new InvalidParameterException( 'Please ensure the new password and confirm new password fields match' )
                }
            }
            catch( all ) {
                flash.changePasswordMessage= all.message
            }
        }
    }
}
于 2017-05-26T08:27:00.267 回答