In a Spring MVC 3.1 application I'm trying to implement a remember-me feature (with info saved in the database).
Here's what I currently have :
I have this in my security context file :
<form-login login-page="/login"
authentication-failure-url="/login?err=true"
default-target-url="/"
username-parameter="username"
password-parameter="password"
login-processing-url="/validatelogin" />
<remember-me key="some_random_key"
token-validity-seconds="31536000"
data-source-ref="dataSource" />
In my login.jsp, I have :
<form action="/validatelogin" method="post">
username : <input type='text' id='username' name='username' value='${SPRING_SECURITY_LAST_EXCEPTION.authentication.principal}' />
<br />
password : <input type='password' id='password' name='password' />
<br /><br />
remember me : <input type="checkbox" name="_spring_security_remember_me" />
<br /><br />
<input type="submit" value="submit" />
</form>
In the "<form-login>" bean, I've been able to rename the default "j_password" and "j_username" fields that have to be used in the jsp, using username-parameter and password-parameter. But I don't find a way to rename the "_spring_security_remember_me" checkbox field.
Any idea on how to rename it?
That is set using the parameter
property of RememberMeServices
.
Sadly this isn't settable using the namespace config. Here are a couple of ways you could set it:
Create a custom RememberMeServices and use it using <remember-me services-ref="myRememberMeServices">
. Set the property on your bean.
Use a BeanPostProcessor (see 1.8) to set the property on the default RememberMeServices
.
You should use BeanPostProcessor to set correct property:
Let's consider that you want to call your property "myRememberMeProperty"
Then your code should look like this:
public class MyBeanPostProcessor implements BeanPostProcessor {
String myRememberMeProperty;
public Object postProcessAfterInitialization(Object bean, String name) {
if (bean instanceof AbstractRememberMeServices) {
AbstractRememberMeServices rememberMe = (AbstractRememberMeServices) bean;
rememberMe.setParameter(getMyRememberMeProperty());
}
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String name) {
return bean;
}
public void setMyRememberMeProperty(String myRememberMeProperty){
this.myRememberMeProperty = myRememberMeProperty;
}
public String getMyRememberMeProperty(){
return this.myRememberMeProperty;
}
}
<bean id="myBeanPostProcessor"
class="x.y.z.MyBeanPostProcessor">
<property name="myRememberMeProperty" value="myRememberMeProperty" />
</bean>
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With