com.healthy.common.security.authentication.mobile.SmsCodeAuthenticationSecurityConfig Maven / Gradle / Ivy
package com.healthy.common.security.authentication.mobile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.stereotype.Component;
import java.util.Objects;
import java.util.UUID;
/**
* SmsCodeAuthenticationSecurityConfig
*
* @author xiaomingzhang
*/
@Component
public class SmsCodeAuthenticationSecurityConfig extends SecurityConfigurerAdapter {
@Autowired
private AuthenticationSuccessHandler healthyAuthenticationSuccessHandler;
@Autowired
private AuthenticationFailureHandler healthyAuthenticationFailureHandler;
@Autowired
private MobileUserDetailsService userDetailsService;
@Autowired(required = false)
private PersistentTokenRepository persistentTokenRepository;
@Override
public void configure(HttpSecurity http) {
SmsCodeAuthenticationFilter smsCodeAuthenticationFilter = new SmsCodeAuthenticationFilter();
smsCodeAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
smsCodeAuthenticationFilter.setAuthenticationSuccessHandler(healthyAuthenticationSuccessHandler);
smsCodeAuthenticationFilter.setAuthenticationFailureHandler(healthyAuthenticationFailureHandler);
if (Objects.nonNull(persistentTokenRepository)) {
String key = UUID.randomUUID().toString();
PersistentTokenBasedRememberMeServices persistentTokenBasedRememberMeServices = new PersistentTokenBasedRememberMeServices(key, (UserDetailsService) userDetailsService, persistentTokenRepository);
smsCodeAuthenticationFilter.setRememberMeServices(persistentTokenBasedRememberMeServices);
}
SmsCodeAuthenticationProvider smsCodeAuthenticationProvider = new SmsCodeAuthenticationProvider();
smsCodeAuthenticationProvider.setUserDetailsService(userDetailsService);
http.authenticationProvider(smsCodeAuthenticationProvider)
.addFilterAfter(smsCodeAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}
}