|
|
@ -12,6 +12,8 @@ import org.springframework.security.config.annotation.authentication.configurati
|
|
|
|
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
|
|
|
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
|
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.EnableWebSecurity;
|
|
|
|
|
|
|
|
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
|
|
|
|
|
|
|
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
|
|
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
|
@ -38,34 +40,37 @@ public class SecurityConfig {
|
|
|
|
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
|
|
|
|
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
|
|
|
|
httpSecurity
|
|
|
|
httpSecurity
|
|
|
|
// cors启用
|
|
|
|
// cors启用
|
|
|
|
.cors().and()
|
|
|
|
.cors(httpSecurityCorsConfigurer -> {
|
|
|
|
|
|
|
|
})
|
|
|
|
// CSRF(跨站请求伪造)禁用,因为不使用session
|
|
|
|
// CSRF(跨站请求伪造)禁用,因为不使用session
|
|
|
|
.csrf().disable()
|
|
|
|
.csrf(AbstractHttpConfigurer::disable)
|
|
|
|
// 认证失败处理类
|
|
|
|
// 认证失败处理类
|
|
|
|
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
|
|
|
|
.exceptionHandling(configurer -> configurer.authenticationEntryPoint(unauthorizedHandler))
|
|
|
|
// 基于token,所以不需要session
|
|
|
|
// 基于token,所以不需要session
|
|
|
|
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
|
|
|
|
.sessionManagement(configurer -> configurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
|
|
|
// 过滤请求
|
|
|
|
// 过滤请求
|
|
|
|
.authorizeHttpRequests()
|
|
|
|
.authorizeHttpRequests(
|
|
|
|
// 对于登录login 验证码captchaImage 允许匿名访问
|
|
|
|
registry -> {
|
|
|
|
.requestMatchers("favicon.ico", "/actuator/**", "/login", "/registry", "/sendEmailCode", "/test/**", "/seckill/**", "/captcha").anonymous()
|
|
|
|
registry
|
|
|
|
.requestMatchers("/home/**", "/category/**", "/comment/**", "/goods/detail/**", "/cart/goodsCount", "/diamond/**").permitAll()
|
|
|
|
.requestMatchers("favicon.ico", "/actuator/**", "/login", "/registry", "/sendEmailCode", "/test/**", "/seckill/**", "/captcha").anonymous()
|
|
|
|
.requestMatchers("/upload/**").anonymous()
|
|
|
|
.requestMatchers("/home/**", "/category/**", "/comment/**", "/goods/detail/**", "/cart/goodsCount", "/diamond/**").permitAll()
|
|
|
|
.requestMatchers("/common/download**").anonymous()
|
|
|
|
.requestMatchers("/upload/**").anonymous()
|
|
|
|
.requestMatchers("/doc.html").anonymous()
|
|
|
|
.requestMatchers("/common/download**").anonymous()
|
|
|
|
.requestMatchers("/swagger-ui/**").anonymous()
|
|
|
|
.requestMatchers("/doc.html").anonymous()
|
|
|
|
.requestMatchers("/swagger-resources/**").anonymous()
|
|
|
|
.requestMatchers("/swagger-ui/**").anonymous()
|
|
|
|
.requestMatchers("/webjars/**").anonymous()
|
|
|
|
.requestMatchers("/swagger-resources/**").anonymous()
|
|
|
|
.requestMatchers("/*/api-docs").anonymous()
|
|
|
|
.requestMatchers("/webjars/**").anonymous()
|
|
|
|
.requestMatchers("/druid/**").anonymous()
|
|
|
|
.requestMatchers("/*/api-docs").anonymous()
|
|
|
|
.requestMatchers("/message/**").anonymous()
|
|
|
|
.requestMatchers("/druid/**").anonymous()
|
|
|
|
// 除上面外的所有请求全部需要鉴权认证
|
|
|
|
.requestMatchers("/message/**").anonymous()
|
|
|
|
.anyRequest().authenticated().and()
|
|
|
|
// 除上面外的所有请求全部需要鉴权认证
|
|
|
|
.headers().frameOptions().disable();
|
|
|
|
.anyRequest().authenticated();
|
|
|
|
httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
|
|
|
|
}
|
|
|
|
// 添加JWT filter
|
|
|
|
)
|
|
|
|
httpSecurity.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
|
|
|
|
.logout(configurer -> configurer.logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler))
|
|
|
|
httpSecurity.userDetailsService(userDetailsService);
|
|
|
|
.headers(configurer -> configurer.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
|
|
|
|
|
|
|
|
.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
|
|
|
|
|
|
|
|
.userDetailsService(userDetailsService);
|
|
|
|
return httpSecurity.build();
|
|
|
|
return httpSecurity.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|