How can i log out from my own created gsp view with spring security installed in grails app?
I have installed spring security plug in to gra开发者_如何学Goils project.i have made my default action as auth. and when i login i get gsp view created by me.now how can i log out from there..
Thanks, LAxmi
After changes in Spring Security Plugin 2.0-XX, you can use a POST request using the remoteLink tag like in the following snippet:
<g:remoteLink class="logout" controller="logout" method="post" asynchronous="false" onSuccess="location.reload()">Logout</g:remoteLink>
You can customize asynchronous attribute or onSuccess event handler to adapt your usage case.
Add a link to /logout
- LogoutController
will log you out.
For example: <g:link controller='logout'>Logout</g:link>
If you want to use a link to logout, first you must disable de logout.postOnly property as described in https://grails-plugins.github.io/grails-spring-security-core/3.2.x/index.html#configGroovy
in your application.groovy:
grails.plugin.springsecurity.logout.postOnly = false
then just call it as a link in any html tag
<a href="/logoff">Salir</a>
Hope this helps
Add in Config grails.plugin.springsecurity.logout.postOnly = false
uses
<g:link controller='logout'>Logout</g:link>
Can be done with a form too:
<g:form controller="logout">
<g:submitButton name="logout" value="Logout" />
</g:form>
add a logout link to url: /logout
As other users mentioned that you have to send the request as POST
request. Another way to do that in an elegant way using a hidden form and JavaScript to submit it.
//jQuery
$(function(){
$('#logout-btn').on('click',function () {
$('.logout-form').submit()
})
})
Your GSP should have a hidden form and an anchor.
//GSP
<a class="dropdown-item" href="#" id="logout-btn">Logout</a>
<g:form class="form-inline d-none logout-form" controller="logout"></g:form>
To logout from application, you need to call the LogoutController
like:
<g:link controller="logout" action="index">Exit</g:link>
By default, the action index
of LogoutController
only accept 'POST' method.
You need to build a form to call it or set the next property in the application.yml
:
grails.plugin.springsecurity.logout.postOnly = false
If you want to keep that option with its default value, this is how should the form look like:
<g:form controller="logout" action="index" method="POST">
<g:submitButton name="Exit"/>
</g:form>
INFO: action key is not needed (optional), specifying only the controller will get you to the index action.
精彩评论