|
@@ -1,77 +1,44 @@
|
|
|
package com.dragon.tj.portal.auth.config;
|
|
|
|
|
|
import com.dragon.tj.portal.auth.module.hmac.HmacAuthenticationFilter;
|
|
|
-import com.dragon.tj.portal.auth.service.JwtTokenAuthenticationFilter;
|
|
|
-import com.dragon.tj.portal.auth.service.JwtTokenLogoutSuccessHandler;
|
|
|
-import com.dragon.tj.portal.auth.service.MyCasAuthenticationEntryPoint;
|
|
|
-import com.dragon.tj.portal.auth.service.MySimpleUrlAuthenticationSuccessHandler;
|
|
|
-import com.dragon.tj.portal.auth.service.MyUserDetailsByNameServiceWrapper;
|
|
|
-import com.dragon.tj.portal.auth.service.MyUserDetailsService;
|
|
|
-import org.jasig.cas.client.validation.Cas20ServiceTicketValidator;
|
|
|
-import org.jasig.cas.client.validation.TicketValidator;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
-import org.springframework.security.authentication.ProviderManager;
|
|
|
-import org.springframework.security.cas.ServiceProperties;
|
|
|
-import org.springframework.security.cas.authentication.CasAuthenticationProvider;
|
|
|
-import org.springframework.security.cas.web.CasAuthenticationFilter;
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
|
|
|
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
-import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
|
import org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler;
|
|
|
-import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
@Configuration
|
|
|
@EnableWebSecurity
|
|
|
public class WebSecurityConfig {
|
|
|
- private static final StringBuilder whiteList = new StringBuilder();
|
|
|
- private static final String DELIMITER_COMMA = ",";
|
|
|
+
|
|
|
+ public static final List<String> WHITE_LIST;
|
|
|
|
|
|
static {
|
|
|
// 白名单
|
|
|
- whiteList.append("/test/login").append(DELIMITER_COMMA)
|
|
|
- .append("/file/**").append(DELIMITER_COMMA);
|
|
|
+ WHITE_LIST = new ArrayList<>();
|
|
|
+ WHITE_LIST.add("/test/login");
|
|
|
+ WHITE_LIST.add("/file/**");
|
|
|
}
|
|
|
|
|
|
- @Value("${cas.base.url}")
|
|
|
- private String casBaseUrl;
|
|
|
- @Value("${cas.login.url}")
|
|
|
- private String casLoginUrl;
|
|
|
- @Value("${app.logout.url}")
|
|
|
- private String appLogoutUrl;
|
|
|
- @Value("${cas.service.url}")
|
|
|
- private String casServiceUrl;
|
|
|
- @Value("${cas.filter.url}")
|
|
|
- private String casFilterUrl;
|
|
|
- @Value("${cas.target.url}")
|
|
|
- private String casTargetUrl;
|
|
|
- @Value("${cas.failure.url}")
|
|
|
- private String casFailureUrl;
|
|
|
+ private final JdbcTemplate jdbcTemplate;
|
|
|
|
|
|
- @Autowired
|
|
|
- private JdbcTemplate jdbcTemplate;
|
|
|
- @Autowired
|
|
|
- private JwtTokenAuthenticationFilter jwtTokenAuthenticationFilter;
|
|
|
- @Autowired
|
|
|
- private MyUserDetailsService userDetailsService;
|
|
|
- @Autowired
|
|
|
- private MySimpleUrlAuthenticationSuccessHandler mySimpleUrlAuthenticationSuccessHandler;
|
|
|
- @Autowired
|
|
|
- private JwtTokenLogoutSuccessHandler logoutSuccessHandler;
|
|
|
+ public WebSecurityConfig(JdbcTemplate jdbcTemplate) {
|
|
|
+ this.jdbcTemplate = jdbcTemplate;
|
|
|
+ }
|
|
|
|
|
|
@Bean
|
|
|
public WebSecurityCustomizer webSecurityCustomizer() {
|
|
@@ -115,76 +82,4 @@ public class WebSecurityConfig {
|
|
|
return filter;
|
|
|
}
|
|
|
|
|
|
- @Bean
|
|
|
- public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
|
|
- http
|
|
|
- // CSRF禁用,因为不使用session
|
|
|
- .csrf().disable()
|
|
|
- // Enable CORS
|
|
|
- .cors()
|
|
|
- .and()
|
|
|
- .authorizeRequests()
|
|
|
- .antMatchers(whiteList.toString().split(DELIMITER_COMMA)).permitAll()
|
|
|
- .anyRequest().authenticated()
|
|
|
- .and()
|
|
|
- .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
|
|
- .and()
|
|
|
- // 因为CasAuthenticationFilter仅拦截/sso/login,所以未认证前访问其他url失败时都走到这个兜底的exception处理
|
|
|
- .exceptionHandling(exceptions -> exceptions.authenticationEntryPoint(authenticationEntryPoint()))
|
|
|
- .addFilter(casAuthenticationFilter())
|
|
|
- .addFilterBefore(jwtTokenAuthenticationFilter, CasAuthenticationFilter.class)
|
|
|
- // .addFilterBefore(singleSignOutFilter(), CasAuthenticationFilter.class)
|
|
|
- .logout()
|
|
|
- .logoutUrl(appLogoutUrl)
|
|
|
- .logoutSuccessHandler(logoutSuccessHandler);
|
|
|
- return http.build();
|
|
|
- }
|
|
|
-
|
|
|
- public AuthenticationEntryPoint authenticationEntryPoint() {
|
|
|
- MyCasAuthenticationEntryPoint casAuthenticationEntryPoint = new MyCasAuthenticationEntryPoint();
|
|
|
- casAuthenticationEntryPoint.setLoginUrl(this.casLoginUrl);
|
|
|
- casAuthenticationEntryPoint.setServiceProperties(serviceProperties());
|
|
|
- return casAuthenticationEntryPoint;
|
|
|
- }
|
|
|
-
|
|
|
- public CasAuthenticationFilter casAuthenticationFilter() {
|
|
|
- CasAuthenticationFilter filter = new CasAuthenticationFilter();
|
|
|
- filter.setFilterProcessesUrl(casFilterUrl);
|
|
|
-
|
|
|
- CasAuthenticationProvider casAuthenticationProvider = casAuthenticationProvider(userDetailsService);
|
|
|
- filter.setAuthenticationManager(new ProviderManager(casAuthenticationProvider));
|
|
|
-
|
|
|
- mySimpleUrlAuthenticationSuccessHandler.setDefaultTargetUrl(casTargetUrl);
|
|
|
- filter.setAuthenticationSuccessHandler(mySimpleUrlAuthenticationSuccessHandler);
|
|
|
- filter.setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler(casFailureUrl));
|
|
|
-
|
|
|
- return filter;
|
|
|
- }
|
|
|
-
|
|
|
- public CasAuthenticationProvider casAuthenticationProvider(UserDetailsService userDetailsService) {
|
|
|
- CasAuthenticationProvider provider = new CasAuthenticationProvider();
|
|
|
- provider.setAuthenticationUserDetailsService(new MyUserDetailsByNameServiceWrapper<>(userDetailsService));
|
|
|
- provider.setServiceProperties(serviceProperties());
|
|
|
- provider.setTicketValidator(ticketValidator());
|
|
|
- provider.setKey("key");
|
|
|
- return provider;
|
|
|
- }
|
|
|
-
|
|
|
- public ServiceProperties serviceProperties() {
|
|
|
- ServiceProperties serviceProperties = new ServiceProperties();
|
|
|
- serviceProperties.setService(casServiceUrl);
|
|
|
- return serviceProperties;
|
|
|
- }
|
|
|
-
|
|
|
- private TicketValidator ticketValidator() {
|
|
|
- return new Cas20ServiceTicketValidator(this.casBaseUrl);
|
|
|
- }
|
|
|
-
|
|
|
-// @Bean
|
|
|
-// public SingleSignOutFilter singleSignOutFilter() {
|
|
|
-// SingleSignOutFilter singleSignOutFilter = new SingleSignOutFilter();
|
|
|
-// singleSignOutFilter.setIgnoreInitConfiguration(true);
|
|
|
-// return singleSignOutFilter;
|
|
|
-// }
|
|
|
-
|
|
|
}
|