我跟着this guide在Spring Boot 2应用程序上设置了Remember Me机制,但没有成功。即使我在登录时检查记住我复选框,也不记得我生成了cookie。没有例外。
这是我的SecurityConfiguration.java:
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import it.niuma.cse.service.UserDetailsServiceImp;
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
public UserDetailsServiceImp userDetailsService;
@Autowired
DataSource dataSource;
@Bean
public BCryptPasswordEncoder passwordEncoder() {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
return bCryptPasswordEncoder;
}
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().antMatchers("/", "/loginMe", "/logout").permitAll()
.antMatchers("/fornitore/**").hasRole("USER")
.antMatchers("/ruolo/**").hasRole("ADMIN")
.anyRequest().authenticated();
http.authorizeRequests().and().exceptionHandling().accessDeniedPage("/accessDenied");
http.authorizeRequests().and().formLogin()//
// Submit URL of login page.
.loginProcessingUrl("/j_spring_security_check") // Submit URL
.loginPage("/loginMe")//
.defaultSuccessUrl("/loginOK")//
.failureUrl("/loginMe?error=true")//
.usernameParameter("username")//
.passwordParameter("password")
// Config for Logout Page
.and()
.rememberMe()
.rememberMeCookieName("remember-me")
.tokenValiditySeconds(24 * 60 * 60)
.tokenRepository(persistentTokenRepository())
.and().logout().logoutUrl("/logout").logoutSuccessUrl("/loginMe");
}
@Bean
public PersistentTokenRepository persistentTokenRepository() {
JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
tokenRepository.setDataSource(dataSource);
return tokenRepository;
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**", "/assets/**", "/css/**", "/js/**", "/img/**", "/plugins/**" , "/i18n/**");
}
}