2017-06-13 122 views
2

我有一個Web應用程序,我的後端使用彈簧安全。我設法實現了正常的登錄,但現在我想擴展它,以便用戶在每次sessionid cookie過期時不必登錄。我也不希望延長上述cookie的有效性。相反,我想把這個選擇留給用戶。誰想保持登錄狀態可以這樣做,其他用戶應該在一段時間後自動註銷。我想借助登錄表單中的複選框來實現此目的。這怎麼可能實現?我設法找到關於記得我的cookie信息,但最好的我的理解這個有着全球效應,使得所有用戶處於登錄狀態彈簧安全「記住我」複選框

這是我的WebConfig:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.addFilterBefore(new CORSFilter(), ChannelProcessingFilter.class).authorizeRequests() 
     // ...Long series of .antMatchers... 
     .antMatchers("/login", "/getActiveUser", "/logout") 
     .permitAll() 
     .anyRequest().authenticated() 
     .and().authenticationProvider(authenticationProvider()) 
     .exceptionHandling() 
     .authenticationEntryPoint(authenticationEntryPoint) 
     .and() 
     .formLogin() 
     .loginPage("/login") 
     .permitAll() 
     .successHandler(authSuccessHandler) 
     .failureHandler(authFailureHandler) 
     .and() 
     .logout() 
     .permitAll() 
     .and() 
     .csrf().disable(); 
    http.formLogin().defaultSuccessUrl("http://localhost:8100", true); 
} 

而且我的應用程序。屬性文件:

spring.jpa.open-in-view=false 
spring.datasource.url=jdbc:mysql://localhost:3307/projectdb?serverTimezone=UTC 
spring.datasource.username=*** 
spring.datasource.password=*** 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.jackson.serialization.write_dates_as_timestamps=false 
server.session.cookie.max-age=600 

在此先感謝!

回答

-1

下面的配置添加到HTTP對象,

http.rememberMe().key("mySecretKey").tokenValiditySeconds(86400); 

及以下的登陸頁面,

<input type="checkbox" name="remember-me" /> 
+0

請你解釋一下這種方法一點? –

+0

加我不想有限的時間 –