|
@@ -4,6 +4,9 @@ package com.fs.framework.config;
|
|
|
import com.fs.framework.security.filter.JwtAuthenticationTokenFilter;
|
|
|
import com.fs.framework.security.handle.AuthenticationEntryPointImpl;
|
|
|
import com.fs.framework.security.handle.LogoutSuccessHandlerImpl;
|
|
|
+import com.fs.framework.security.handle.LogoutSuccessScrmHandlerImpl;
|
|
|
+import com.fs.framework.service.UserDetailsScrmServiceImpl;
|
|
|
+import com.fs.framework.service.UserDetailsServiceImpl;
|
|
|
import com.fs.store.utils.MD5PasswordEncoder;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
@@ -18,7 +21,16 @@ import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
import org.springframework.security.web.authentication.logout.LogoutFilter;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
import org.springframework.web.filter.CorsFilter;
|
|
|
+import org.springframework.web.filter.OncePerRequestFilter;
|
|
|
+
|
|
|
+import javax.servlet.FilterChain;
|
|
|
+import javax.servlet.ServletException;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
|
|
|
/**
|
|
|
* spring security配置
|
|
@@ -32,7 +44,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
|
|
|
* 自定义用户认证逻辑
|
|
|
*/
|
|
|
@Autowired
|
|
|
- private UserDetailsService userDetailsService;
|
|
|
+ private UserDetailsServiceImpl userDetailsService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserDetailsScrmServiceImpl userDetailsScrmService;
|
|
|
|
|
|
/**
|
|
|
* 认证失败处理类
|
|
@@ -46,6 +61,12 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
|
|
|
@Autowired
|
|
|
private LogoutSuccessHandlerImpl logoutSuccessHandler;
|
|
|
|
|
|
+ /**
|
|
|
+ * 退出处理类
|
|
|
+ */
|
|
|
+ @Autowired
|
|
|
+ private LogoutSuccessScrmHandlerImpl logoutSuccessScrmHandler;
|
|
|
+
|
|
|
/**
|
|
|
* token认证过滤器
|
|
|
*/
|
|
@@ -89,6 +110,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
|
|
|
@Override
|
|
|
protected void configure(HttpSecurity httpSecurity) throws Exception
|
|
|
{
|
|
|
+
|
|
|
httpSecurity
|
|
|
// CSRF禁用,因为不使用session
|
|
|
.csrf().disable()
|
|
@@ -99,7 +121,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
|
|
|
// 过滤请求
|
|
|
.authorizeRequests()
|
|
|
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
|
|
|
- .antMatchers("/login", "/register", "/captchaImage").anonymous()
|
|
|
+ .antMatchers("/login", "/register", "/captchaImage","/store/login").anonymous()
|
|
|
.antMatchers(
|
|
|
HttpMethod.GET,
|
|
|
"/",
|
|
@@ -125,7 +147,15 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
|
|
|
.anyRequest().authenticated()
|
|
|
.and()
|
|
|
.headers().frameOptions().disable();
|
|
|
- httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
|
|
|
+ httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler((request, response, authentication) -> {
|
|
|
+ boolean isNewStore = "1".equals(request.getHeader("isNewStore"));
|
|
|
+
|
|
|
+ if (isNewStore) {
|
|
|
+ logoutSuccessScrmHandler.onLogoutSuccess(request, response, authentication);
|
|
|
+ } else {
|
|
|
+ logoutSuccessHandler.onLogoutSuccess(request, response, authentication);
|
|
|
+ }
|
|
|
+ });
|
|
|
// 添加JWT filter
|
|
|
httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
|
|
|
// 添加CORS filter
|
|
@@ -146,8 +176,21 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
|
|
|
* 身份认证接口
|
|
|
*/
|
|
|
@Override
|
|
|
- protected void configure(AuthenticationManagerBuilder auth) throws Exception
|
|
|
- {
|
|
|
- auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
|
|
|
+ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
|
|
+ auth.userDetailsService(username -> {
|
|
|
+ HttpServletRequest request = getCurrentRequest();
|
|
|
+ boolean isNewStore = request != null && "1".equals(request.getHeader("isNewStore"));
|
|
|
+
|
|
|
+ UserDetailsService service = isNewStore ? userDetailsScrmService : userDetailsService;
|
|
|
+ return service.loadUserByUsername(username);
|
|
|
+ }).passwordEncoder(bCryptPasswordEncoder());
|
|
|
+ }
|
|
|
+
|
|
|
+ private HttpServletRequest getCurrentRequest() {
|
|
|
+ try {
|
|
|
+ return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
}
|