Я хочу реализовать remember-me
функциональность, используя PersistentTokenBasedRememberMeServices
, я пробовал использовать следующую сущность:
@Data
@Entity(name = "persistent_logins")
@NoArgsConstructor
@RequiredArgsConstructor
public class PersistentLogin implements Serializable {
@Id
@Column(name = "series", unique = true, nullable = false,length = 64)
private String series;
@NonNull
@Column(name = "username",length = 64,nullable = false)
private String username;
@NonNull
@Column(name = "token",length = 64,nullable = false)
private String token;
@NonNull
@Column(name = "last_userd",nullable = false)
private Date lastUsed;
}
и следующую конфигурацию:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private UserDetailsServiceImpl userDetailsService;
@Autowired
public SecurityConfiguration(UserDetailsServiceImpl userDetailsService) {
this.userDetailsService = userDetailsService;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/register", "/confirm", "/post_register").permitAll()
.anyRequest()
.authenticated()
.and()
// omitting login and logout related configs
.and()
.rememberMe()
.rememberMeParameter("remember-me")
.rememberMeServices(persistentTokenBasedRememberMeServices());
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.authenticationProvider(daoAuthenticationProvider());
}
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider(){
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(userDetailsService);
provider.setPasswordEncoder(bCryptPasswordEncoder());
return provider;
}
@Bean
public PasswordEncoder bCryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean// browser remember me
PersistentTokenBasedRememberMeServices persistentTokenBasedRememberMeServices(){
PersistentTokenBasedRememberMeServices p =
new PersistentTokenBasedRememberMeServices(
REMEMBER_ME_KEY,
userDetailsService,
new JdbcTokenRepositoryImpl()
);
p.setCookieName(JSESSIONID);
p.setTokenValiditySeconds(24 * 60 * 60);
return p;
}
@Bean
RememberMeAuthenticationFilter rememberMeAuthenticationFilter() throws Exception {
return new RememberMeAuthenticationFilter(authenticationManager(), persistentTokenBasedRememberMeServices());
}
@Bean
RememberMeAuthenticationProvider rememberMeAuthenticationProvider(){
return new RememberMeAuthenticationProvider(REMEMBER_ME_KEY);
}
}
с вышеуказанной конфигурацией Я получаю:
java.lang.NullPointerException: null at org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl.getTokenForSeries(JdbcTokenRepositoryImpl.java:87)
с полным StackTrace, приведенным ниже:
2020-03-25T06:02:10.583521+00:00 app[web.1]: java.lang.NullPointerException: null
2020-03-25T06:02:10.583522+00:00 app[web.1]: at org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl.getTokenForSeries(JdbcTokenRepositoryImpl.java:87) ~[sp
ring-security-web-5.0.3.RELEASE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583528+00:00 app[web.1]: at org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices.processAutoLoginCookie(PersistentTokenBa
sedRememberMeServices.java:104) ~[spring-security-web-5.0.3.RELEASE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583529+00:00 app[web.1]: at org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices.autoLogin(AbstractRememberMeServices.java:136) ~[spr
ing-security-web-5.0.3.RELEASE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583529+00:00 app[web.1]: at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:98)
~[spring-security-web-5.0.3.RELEASE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583530+00:00 app[web.1]: at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.3.RELEA
SE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583531+00:00 app[web.1]: at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:
170) ~[spring-security-web-5.0.3.RELEASE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583531+00:00 app[web.1]: at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.3.RELEA
SE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583532+00:00 app[web.1]: at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) ~[spring-security-web-5.0.
3.RELEASE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583532+00:00 app[web.1]: at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.3.RELEA
SE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583532+00:00 app[web.1]: at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.jav
a:200) ~[spring-security-web-5.0.3.RELEASE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583533+00:00 app[web.1]: at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.3.RELEA
SE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583533+00:00 app[web.1]: at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) ~[spring-security-web-5.0.3.RELEASE.ja
r!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583534+00:00 app[web.1]: at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.3.RELEA
SE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583534+00:00 app[web.1]: at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:100) ~[spring-security-web-5.0.3.RELEASE.jar!/:5.0.3.REL
EASE]
2020-03-25T06:02:10.583536+00:00 app[web.1]: at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.4.RELEASE.jar!/:5.0.4.RELEASE
]
2020-03-25T06:02:10.583536+00:00 app[web.1]: at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.3.RELEA
SE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583537+00:00 app[web.1]: at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66) ~[spring-security-web-5.0.3.RELEAS
E.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583537+00:00 app[web.1]: at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.4.RELEASE.jar!/:5.0.4.RELEASE
]
2020-03-25T06:02:10.583537+00:00 app[web.1]: at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.3.RELEA
SE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583538+00:00 app[web.1]: at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) ~[spring-sec
urity-web-5.0.3.RELEASE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583538+00:00 app[web.1]: at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.3.RELEA
SE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583544+00:00 app[web.1]: at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.
java:56) ~[spring-security-web-5.0.3.RELEASE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583545+00:00 app[web.1]: at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.4.RELEASE.jar!/:5.0.4.RELEASE
]
2020-03-25T06:02:10.583545+00:00 app[web.1]: at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.3.RELEA
SE.jar!/:5.0.3.RELEASE]
2020-03-25T06:02:10.583546+00:00 app[web.1]: at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215) ~[spring-security-web-5.0.3.RELEASE.jar!/:5.
0.3.RELEASE]
2020-03-25T06:02:10.583546+00:00 app[web.1]: at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178) ~[spring-security-web-5.0.3.RELEASE.jar!/:5.0.3.RELE
ASE]
2020-03-25T06:02:10.583547+00:00 app[web.1]: at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357) ~[spring-web-5.0.4.RELEASE.jar!/:5.0.4
.RELEASE]
2020-03-25T06:02:10.583547+00:00 app[web.1]: at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270) ~[spring-web-5.0.4.RELEASE.jar!/:5.0.4.RELEA
SE]
2020-03-25T06:02:10.583548+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583548+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583548+00:00 app[web.1]: at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.0.4.RELEASE.jar!/:5.0.4.
RELEASE]
2020-03-25T06:02:10.583549+00:00 app[web.1]: at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.4.RELEASE.jar!/:5.0.4.RELEASE
]
2020-03-25T06:02:10.583550+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583550+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583550+00:00 app[web.1]: at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[spring-web-5.0.4.RELEASE.jar
!/:5.0.4.RELEASE]
2020-03-25T06:02:10.583550+00:00 app[web.1]: at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.4.RELEASE.jar!/:5.0.4.RELEASE
]
2020-03-25T06:02:10.583551+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583551+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583551+00:00 app[web.1]: at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-5.0.4.RELEASE.jar!/:5.
0.4.RELEASE]
2020-03-25T06:02:10.583552+00:00 app[web.1]: at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.4.RELEASE.jar!/:5.0.4.RELEASE
]
2020-03-25T06:02:10.583552+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583553+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583561+00:00 app[web.1]: at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.0.4.RELEASE.jar!/
:5.0.4.RELEASE]
2020-03-25T06:02:10.583562+00:00 app[web.1]: at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.4.RELEASE.jar!/:5.0.4.RELEASE
]
2020-03-25T06:02:10.583562+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583562+00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583563+00:00 app[web.1]: at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583564+00:00 app[web.1]: at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583565+00:00 app[web.1]: at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583569+00:00 app[web.1]: at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583569+00:00 app[web.1]: at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583570+00:00 app[web.1]: at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583570+00:00 app[web.1]: at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:677) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583570+00:00 app[web.1]: at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583571+00:00 app[web.1]: at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583571+00:00 app[web.1]: at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583574+00:00 app[web.1]: at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583575+00:00 app[web.1]: at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583575+00:00 app[web.1]: at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583576+00:00 app[web.1]: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_242-heroku]
2020-03-25T06:02:10.583576+00:00 app[web.1]: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_242-heroku]
2020-03-25T06:02:10.583577+00:00 app[web.1]: at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.28.jar!/:8.5.28]
2020-03-25T06:02:10.583577+00:00 app[web.1]: at java.lang.Thread.run(Thread.java:748) [na:1.8.0_242-heroku]
2020-03-25T06:02:10.583578+00:00 app[web.1]:
2020-03-25T06:02:10.712684+00:00 app[web.1]: 2020-03-25 06:02:10.647 ERROR 4 --- [io-22186-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherSer
vlet] threw exception
Вопрос: Как правильно реализовать PersistentTokenBasedRememberMeServices
? Я искал Net, нашел примеры и последовал за ними, но это не сработало, как ожидалось. Я что-то упускаю?