Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Security : How to rename the "_spring_security_remember_me" checkbox field?

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 created a persistent_logins table.
  • 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?

like image 559
electrotype Avatar asked Apr 29 '12 00:04

electrotype


2 Answers

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:

  1. Create a custom RememberMeServices and use it using <remember-me services-ref="myRememberMeServices">. Set the property on your bean.

  2. Use a BeanPostProcessor (see 1.8) to set the property on the default RememberMeServices.

like image 172
sourcedelica Avatar answered Nov 14 '22 12:11

sourcedelica


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.

like image 44
danny.lesnik Avatar answered Nov 14 '22 12:11

danny.lesnik