简体   繁体   English

如何在grails中自定义spring security plugin登录页面

[英]How to customize spring security plugin Login page in grails

I've tried without success to customize the login page in spring security core 3.1.1. 我试过没有成功定制spring security core 3.1.1中的登录页面。 I created a login directory in the views directory and an auth.gsp in the login directory and put my custom form there. 我在views目录中创建了一个登录目录,在登录目录中创建了一个auth.gsp,并将我的自定义表单放在那里。 Here is the code: 这是代码:

<g:form name="form-login" method="POST" action="${resource('file': 'j_spring_security_check')}">
     <label for="j_username">Username:</label>
      <g:textField name="j_username"/>
                <label for="j_password">Password:</label>
                <g:passwordField name="j_password"/>
            <g:submitButton name="login" value="Log in" />
    </g:form>

It takes the username and password alright but upon clicking the login button, it still shows the form. 它需要用户名和密码,但点击登录按钮后,它仍会显示表单。 Any solutions. 任何解决方案

That's an old form using the old action address (the core problem that's causing the redirect) and old names for username/password (the lurking problem that would have been more frustrating because it will always fail with correct values, since they're not usable if they're set for the wrong parameter names). 这是一个旧的表单,使用旧的action地址(导致重定向的核心问题)和用户名/密码的旧名称(潜伏的问题本来会更令人沮丧,因为它总是会失败,正确的值,因为它们不可用如果他们设置了错误的参数名称)。

It's not as simple in 3.x as it was in 2.x to override plugin files. 它在3.x中并不像在2.x中那样简单地覆盖插件文件。 In 2.x plugins are zip files unpacked in your project root's target directory, and you can do valid and/or crazy things with plugin source. 2.x插件是在项目根target目录中解压缩的zip文件,你可以用插件源做有效和/或疯狂的事情。 In 3.x the plugins are compiled jars, so hopefully we reduced the total amount of crazy, but we also have made it harder to do valid things like this. 在3.x中,插件是编译的jar,所以我们希望减少疯狂的总量,但我们也更难做到这样的有效事情。

But this is open source software, and the exact .gsp file or one that's close enough is very easy to find in the source repo, which is here . 但这是一个开源软件,确切的.gsp文件或一个足够接近的文件很容易在源代码库中找到,就在这里

auth.gsp changes rarely so it's fine to get the latest file from the master branch , or you could look through the history (recentl builds are and future builds will continue to be tagged) so it would be easy to get an older version, eg from version 3.0.4 . auth.gsp很少更改,因此可以从主分支获取最新文件 ,或者您可以查看历史记录(Recentl构建版本和将来的版本将继续标记),因此很容易获得旧版本,例如从版本3.0.4

That's not neccessarily the final answer though because the plugin is very configurable, and you can override several variables used in the GSP in the config, so you should replace code that looks up values with just the values since your app is unlikely to benefit from that flexibility. 这不是最后的答案,因为插件是非常可配置的,并且您可以覆盖配置中GSP中使用的几个变量,因此您应该替换仅使用值查找值的代码,因为您的应用程序不太可能从中受益灵活性。 The defaults for config settings are in DefaultSecurityConfig.groovy , but I left this GSPs inside the lookup code, so you just need to remove that and retain the values. 配置设置的默认值在DefaultSecurityConfig.groovy ,但我将此GSP保留在查找代码中,因此您只需要删除它并保留值。

Here's the inner form element (you need the whole file but I'm not showing the parts that don't need changes): 这是内部form元素(您需要整个文件,但我没有显示不需要更改的部分):

<form action="${postUrl ?: '/login/authenticate'}" method="POST" id="loginForm" class="cssform" autocomplete="off">
    <p>
        <label for="username"><g:message code='springSecurity.login.username.label'/>:</label>
        <input type="text" class="text_" name="${usernameParameter ?: 'username'}" id="username"/>
    </p>

    <p>
        <label for="password"><g:message code='springSecurity.login.password.label'/>:</label>
        <input type="password" class="text_" name="${passwordParameter ?: 'password'}" id="password"/>
    </p>

    <p id="remember_me_holder">
        <input type="checkbox" class="chk" name="${rememberMeParameter ?: 'remember-me'}" id="remember_me" <g:if test='${hasCookie}'>checked="checked"</g:if>/>
        <label for="remember_me"><g:message code='springSecurity.login.remember.me.label'/></label>
    </p>

    <p>
        <input type="submit" id="submit" value="${message(code: 'springSecurity.login.button')}"/>
    </p>
</form>

and for the initial pass you would want to change it to this: 对于初始传递,您可能希望将其更改为:

<form action="/login/authenticate' method="POST" id="loginForm" class="cssform" autocomplete="off">
    <p>
        <label for="username"><g:message code='springSecurity.login.username.label'/>:</label>
        <input type="text" class="text_" name="username" id="username"/>
    </p>

    <p>
        <label for="password"><g:message code='springSecurity.login.password.label'/>:</label>
        <input type="password" class="text_" name="password" id="password"/>
    </p>

    <p id="remember_me_holder">
        <input type="checkbox" class="chk" name="remember-me" id="remember_me"/>
        <label for="remember_me"><g:message code='springSecurity.login.remember.me.label'/></label>
    </p>

    <p>
        <input type="submit" id="submit" value="${message(code: 'springSecurity.login.button')}"/>
    </p>
</form>

You can use whatever post url and username/password param names you want including the old ones, but you need to set the config properties so the Spring Security filter that processes logins is looking at the correct uri and is expecting the param names that you're sending. 你可以使用你想要的任何post url和username / password param名称,包括旧名称,但是你需要设置配置属性,以便处理登录的Spring Security过滤器正在查看正确的uri并期望你的param名称。重发。 If you're ok with the defaults then this will work, and you can of course make whatever regular HTML and CSS/JS changes you want. 如果您对默认值没问题,那么这将有效,您当然可以进行任何常规的HTML和CSS / JS更改。

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

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