|
@@ -1,18 +1,9 @@
|
|
|
package com.ym.mec.auth.config;
|
|
|
|
|
|
-import com.ym.mec.auth.config.provider.service.BaseUserDetailsService;
|
|
|
-import com.ym.mec.auth.config.provider.service.SmsCodeAuthenticationProvider;
|
|
|
-import com.ym.mec.auth.filter.BaseAuthenticationFilter;
|
|
|
-import com.ym.mec.auth.filter.SmsCodeAuthenticationFilter;
|
|
|
-import com.ym.mec.auth.handler.BaseAuthenticationFailureEvenHandler;
|
|
|
-import com.ym.mec.auth.handler.BaseAuthenticationSuccessEventHandler;
|
|
|
-import com.ym.mec.common.security.BaseAccessDeniedHandler;
|
|
|
-import com.ym.mec.common.security.BaseAuthenticationEntryPoint;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
-import org.springframework.security.authentication.ProviderManager;
|
|
|
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
|
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
|
|
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
|
@@ -22,8 +13,16 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
|
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
|
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
+import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
|
|
|
-import java.util.Arrays;
|
|
|
+import com.ym.mec.auth.core.filter.PhoneLoginAuthenticationFilter;
|
|
|
+import com.ym.mec.auth.core.filter.UsernameAuthenticationFilter;
|
|
|
+import com.ym.mec.auth.core.handler.BaseAuthenticationFailureEvenHandler;
|
|
|
+import com.ym.mec.auth.core.handler.BaseAuthenticationSuccessEventHandler;
|
|
|
+import com.ym.mec.auth.core.provider.PhoneAuthenticationProvider;
|
|
|
+import com.ym.mec.auth.core.provider.service.PhoneUserDetailService;
|
|
|
+import com.ym.mec.auth.core.provider.service.UsernameUserDetailsService;
|
|
|
+import com.ym.mec.common.validcode.SmsCodeService;
|
|
|
|
|
|
@Configuration
|
|
|
@EnableWebSecurity
|
|
@@ -31,57 +30,57 @@ import java.util.Arrays;
|
|
|
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
|
@Autowired
|
|
|
- private BaseUserDetailsService baseUserDetailsService;
|
|
|
+ private UsernameUserDetailsService usernameUserDetailsService;
|
|
|
+
|
|
|
@Autowired
|
|
|
- private SmsCodeAuthenticationProvider smsCodeAuthenticationProvider;
|
|
|
+ private PhoneUserDetailService phoneUserDetailService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PhoneAuthenticationProvider phoneAuthenticationProvider;
|
|
|
+
|
|
|
@Autowired
|
|
|
private BaseAuthenticationSuccessEventHandler successEventHandler;
|
|
|
+
|
|
|
@Autowired
|
|
|
private BaseAuthenticationFailureEvenHandler failureEvenHandler;
|
|
|
+
|
|
|
@Autowired
|
|
|
- private BaseAccessDeniedHandler baseAccessDeniedHandler;
|
|
|
- @Autowired
|
|
|
- private BaseAuthenticationEntryPoint baseAuthenticationEntryPoint;
|
|
|
-
|
|
|
+ private SmsCodeService smsCodeService;
|
|
|
+
|
|
|
@Override
|
|
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
|
|
auth.authenticationProvider(daoAuthenticationProvider());
|
|
|
+
|
|
|
+ PhoneAuthenticationProvider provider = phoneAuthenticationProvider();
|
|
|
+ provider.setSmsCodeService(smsCodeService);
|
|
|
+ auth.authenticationProvider(provider);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void configure(HttpSecurity http) throws Exception {
|
|
|
// 表单登录 方式
|
|
|
http
|
|
|
- .formLogin()
|
|
|
- .loginPage("/loginIn")
|
|
|
- .loginPage("/smsLogin")
|
|
|
- .successHandler(successEventHandler)
|
|
|
- .failureHandler(failureEvenHandler)
|
|
|
- .and()
|
|
|
- .exceptionHandling()
|
|
|
- .accessDeniedHandler(baseAccessDeniedHandler)
|
|
|
- .authenticationEntryPoint(baseAuthenticationEntryPoint)// 当未登录访问资源时
|
|
|
- .and()// 请求授权
|
|
|
+ .addFilterBefore(getUsernameAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
|
|
|
+ .addFilterBefore(getPhoneLoginAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
|
|
|
+ .formLogin().loginPage("/loginIn").loginPage("/smsLogin").and()
|
|
|
+ //.exceptionHandling().accessDeniedHandler(baseAccessDeniedHandler).authenticationEntryPoint(baseAuthenticationEntryPoint).and()// 当未登录访问资源时
|
|
|
+ // 请求授权
|
|
|
.authorizeRequests()// 不需要权限认证的url
|
|
|
- .antMatchers("/oauth/**").permitAll()// 任何请求
|
|
|
+ .antMatchers("/usernameLogin","/smsLogin", "/refreshToken", "/v2/api-docs").permitAll()// 任何请求
|
|
|
.anyRequest()// 需要身份认证
|
|
|
- .authenticated()
|
|
|
- .and()// 关闭跨站请求防护
|
|
|
- .csrf()
|
|
|
- .disable();
|
|
|
+ .authenticated().and()// 关闭跨站请求防护
|
|
|
+ .csrf().disable();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void configure(WebSecurity web) throws Exception {
|
|
|
- web.ignoring().antMatchers("/loginIn","/smsLogin", "/refreshToken","/v2/api-docs");
|
|
|
+ web.ignoring().antMatchers("/usernameLogin", "/smsLogin", "/refreshToken", "/v2/api-docs");
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
@Override
|
|
|
public AuthenticationManager authenticationManagerBean() throws Exception {
|
|
|
- ProviderManager authenticationManager = new ProviderManager(Arrays.asList(smsCodeAuthenticationProvider,daoAuthenticationProvider()));
|
|
|
- authenticationManager.setEraseCredentialsAfterAuthentication(false);
|
|
|
- return authenticationManager;
|
|
|
+ return super.authenticationManagerBean();
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
@@ -93,7 +92,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
public DaoAuthenticationProvider daoAuthenticationProvider() {
|
|
|
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
|
|
// 设置userDetailsService
|
|
|
- provider.setUserDetailsService(baseUserDetailsService);
|
|
|
+ provider.setUserDetailsService(usernameUserDetailsService);
|
|
|
// 禁止隐藏用户未找到异常
|
|
|
provider.setHideUserNotFoundExceptions(false);
|
|
|
// 使用BCrypt进行密码的hash
|
|
@@ -101,17 +100,30 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
return provider;
|
|
|
}
|
|
|
|
|
|
+ @Bean
|
|
|
+ public PhoneAuthenticationProvider phoneAuthenticationProvider(){
|
|
|
+ PhoneAuthenticationProvider provider = new PhoneAuthenticationProvider();
|
|
|
+ // 设置userDetailsService
|
|
|
+ provider.setUserDetailsService(phoneUserDetailService);
|
|
|
+ //provider.setSmsCodeService(smsCodeService);
|
|
|
+ // 禁止隐藏用户未找到异常
|
|
|
+ provider.setHideUserNotFoundExceptions(false);
|
|
|
+
|
|
|
+ return provider;
|
|
|
+ }
|
|
|
+
|
|
|
@Bean
|
|
|
- public BaseAuthenticationFilter getBaseAuthenticationFilter() throws Exception {
|
|
|
- BaseAuthenticationFilter filter = new BaseAuthenticationFilter();
|
|
|
+ public UsernameAuthenticationFilter getUsernameAuthenticationFilter() throws Exception {
|
|
|
+ UsernameAuthenticationFilter filter = new UsernameAuthenticationFilter();
|
|
|
filter.setAuthenticationManager(authenticationManagerBean());
|
|
|
filter.setAuthenticationSuccessHandler(successEventHandler);
|
|
|
filter.setAuthenticationFailureHandler(failureEvenHandler);
|
|
|
return filter;
|
|
|
}
|
|
|
+
|
|
|
@Bean
|
|
|
- public SmsCodeAuthenticationFilter getSmsCodeAuthenticationFilter() throws Exception {
|
|
|
- SmsCodeAuthenticationFilter filter = new SmsCodeAuthenticationFilter();
|
|
|
+ public PhoneLoginAuthenticationFilter getPhoneLoginAuthenticationFilter() throws Exception {
|
|
|
+ PhoneLoginAuthenticationFilter filter = new PhoneLoginAuthenticationFilter();
|
|
|
filter.setAuthenticationManager(authenticationManagerBean());
|
|
|
filter.setAuthenticationSuccessHandler(successEventHandler);
|
|
|
filter.setAuthenticationFailureHandler(failureEvenHandler);
|