2020-01-31 08:59:12 +00:00
|
|
|
package com.codesdream.ase.configure;
|
|
|
|
|
2020-03-01 07:13:16 +00:00
|
|
|
import com.codesdream.ase.component.permission.*;
|
2020-02-04 17:31:14 +00:00
|
|
|
import com.codesdream.ase.service.ASEUserDetailsService;
|
2020-03-01 07:13:16 +00:00
|
|
|
import org.springframework.context.annotation.Bean;
|
2020-01-31 08:59:12 +00:00
|
|
|
import org.springframework.context.annotation.Configuration;
|
2020-03-15 11:18:51 +00:00
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
2020-02-04 17:31:14 +00:00
|
|
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
2020-01-31 08:59:12 +00:00
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
2020-02-04 17:31:14 +00:00
|
|
|
import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
2020-01-31 08:59:12 +00:00
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
2020-03-02 01:34:39 +00:00
|
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
|
|
import org.springframework.security.core.session.SessionRegistry;
|
|
|
|
import org.springframework.security.core.session.SessionRegistryImpl;
|
2020-03-01 07:13:16 +00:00
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
2020-03-02 01:34:39 +00:00
|
|
|
import org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy;
|
|
|
|
import org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy;
|
|
|
|
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
|
|
|
|
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
|
|
|
|
import org.springframework.security.web.context.SecurityContextPersistenceFilter;
|
2020-03-01 07:13:16 +00:00
|
|
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
2020-01-31 08:59:12 +00:00
|
|
|
|
2020-02-04 17:31:14 +00:00
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Spring Security 配置类
|
|
|
|
* 用于Spring Security相关参数的配置
|
|
|
|
*/
|
2020-01-31 08:59:12 +00:00
|
|
|
@Configuration
|
|
|
|
@EnableWebSecurity
|
|
|
|
public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter {
|
2020-02-04 17:31:14 +00:00
|
|
|
|
2020-02-14 07:59:40 +00:00
|
|
|
@Resource
|
2020-02-04 17:31:14 +00:00
|
|
|
ASEUserDetailsService aseUserDetailService;
|
|
|
|
|
2020-02-14 07:59:40 +00:00
|
|
|
@Resource
|
2020-02-04 17:31:14 +00:00
|
|
|
ASEPasswordEncoder asePasswordEncoder;
|
|
|
|
|
2020-02-16 10:20:37 +00:00
|
|
|
@Resource
|
|
|
|
ASESecurityAuthenticationProvider aseSecurityAuthenticationProvider;
|
|
|
|
|
2020-03-01 07:13:16 +00:00
|
|
|
@Resource
|
|
|
|
ASEAuthenticationSuccessHandler successHandler;
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
ASEAuthenticationFailureHandler failureHandler;
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
ASEAuthenticationEntryPoint authenticationEntryPoint;
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
ASEAccessDeniedHandler accessDeniedHandler;
|
|
|
|
|
2020-03-15 11:18:51 +00:00
|
|
|
|
2020-01-31 08:59:12 +00:00
|
|
|
@Override
|
|
|
|
protected void configure(HttpSecurity http) throws Exception {
|
2020-03-01 07:13:16 +00:00
|
|
|
http
|
|
|
|
.authorizeRequests()
|
2020-01-31 08:59:12 +00:00
|
|
|
.anyRequest().authenticated()
|
|
|
|
.and()
|
2020-03-01 07:13:16 +00:00
|
|
|
.csrf().disable()
|
2020-02-04 17:31:14 +00:00
|
|
|
.logout().permitAll();
|
|
|
|
|
2020-03-01 07:13:16 +00:00
|
|
|
http.exceptionHandling()
|
|
|
|
.authenticationEntryPoint(authenticationEntryPoint)
|
|
|
|
.accessDeniedHandler(accessDeniedHandler);
|
|
|
|
|
|
|
|
// 替换掉原有的UsernamePasswordAuthenticationFilter
|
2020-03-02 01:34:39 +00:00
|
|
|
http.addFilterAt(aseUsernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
|
2020-03-15 11:18:51 +00:00
|
|
|
.addFilterBefore(asejsonTokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
|
|
|
|
|
|
|
|
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
|
2020-03-02 01:34:39 +00:00
|
|
|
|
2020-02-04 17:31:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
2020-02-16 10:20:37 +00:00
|
|
|
auth.authenticationProvider(aseSecurityAuthenticationProvider)
|
|
|
|
.userDetailsService(aseUserDetailService)
|
2020-02-14 07:59:40 +00:00
|
|
|
.passwordEncoder(asePasswordEncoder);
|
2020-02-04 17:31:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void configure(WebSecurity web) throws Exception {
|
|
|
|
web
|
|
|
|
.ignoring()
|
2020-02-22 13:41:16 +00:00
|
|
|
.antMatchers(
|
|
|
|
"/assets/**",
|
|
|
|
"/register/**",
|
|
|
|
"/forget/**",
|
|
|
|
"/not_found/**",
|
|
|
|
"/error/**",
|
|
|
|
"/login/**");
|
2020-01-31 08:59:12 +00:00
|
|
|
}
|
2020-03-01 07:13:16 +00:00
|
|
|
|
2020-03-15 11:18:51 +00:00
|
|
|
//注册自定义的UsernamePasswordAuthenticationFilter
|
|
|
|
@Bean
|
|
|
|
ASEJSONTokenAuthenticationFilter asejsonTokenAuthenticationFilter() throws Exception {
|
|
|
|
return new ASEJSONTokenAuthenticationFilter();
|
|
|
|
}
|
|
|
|
|
2020-03-01 07:13:16 +00:00
|
|
|
//注册自定义的UsernamePasswordAuthenticationFilter
|
|
|
|
@Bean
|
2020-03-02 01:34:39 +00:00
|
|
|
ASEUsernamePasswordAuthenticationFilter aseUsernamePasswordAuthenticationFilter() throws Exception {
|
2020-03-01 07:13:16 +00:00
|
|
|
ASEUsernamePasswordAuthenticationFilter filter = new ASEUsernamePasswordAuthenticationFilter();
|
|
|
|
filter.setAuthenticationSuccessHandler(successHandler);
|
|
|
|
filter.setAuthenticationFailureHandler(failureHandler);
|
2020-03-02 01:34:39 +00:00
|
|
|
filter.setSessionAuthenticationStrategy(sessionAuthenticationStrategy(sessionRegistry()));
|
|
|
|
filter.setAllowSessionCreation(true);
|
2020-03-01 07:13:16 +00:00
|
|
|
filter.setRequiresAuthenticationRequestMatcher(
|
2020-03-15 11:18:51 +00:00
|
|
|
new AntPathRequestMatcher("/login/token", "POST"));
|
2020-03-01 07:13:16 +00:00
|
|
|
|
|
|
|
filter.setAuthenticationManager(authenticationManagerBean());
|
|
|
|
return filter;
|
|
|
|
}
|
2020-03-02 01:34:39 +00:00
|
|
|
|
|
|
|
@Bean
|
|
|
|
public SessionRegistry sessionRegistry() {
|
|
|
|
return new SessionRegistryImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public SessionAuthenticationStrategy sessionAuthenticationStrategy(SessionRegistry sessionRegistry){
|
|
|
|
return new ConcurrentSessionControlAuthenticationStrategy(sessionRegistry){{
|
|
|
|
setMaximumSessions(1);
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2020-01-31 08:59:12 +00:00
|
|
|
}
|