package com.mes.common.config;
|
|
|
import com.mes.common.filter.JwtAuthenticationTokenFilter;
|
import com.mes.common.handler.*;
|
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.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
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.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
@Configuration
|
@EnableWebSecurity
|
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {
|
@Autowired
|
private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
|
@Autowired
|
LoginFailureHandler loginFailureHandler;
|
|
@Autowired
|
LoginSuccessHandler loginSuccessHandler;
|
|
@Autowired
|
JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
|
|
@Autowired
|
JwtAccessDeniedHandler jwtAccessDeniedHandler;
|
|
@Autowired
|
JwtLogoutSuccessHandler jwtLogoutSuccessHandler;
|
|
@Bean
|
public PasswordEncoder passwordEncoder() {
|
return new BCryptPasswordEncoder();
|
}
|
|
/**
|
* 配置过滤规则
|
*/
|
@Override
|
protected void configure(HttpSecurity http) throws Exception {
|
|
http.cors().and().csrf().disable()
|
|
// 登录配置
|
.formLogin()
|
.successHandler(loginSuccessHandler)
|
.failureHandler(loginFailureHandler)
|
|
.and()
|
.logout()
|
.logoutSuccessHandler(jwtLogoutSuccessHandler)
|
|
// 禁用session
|
.and()
|
.sessionManagement()
|
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
|
// 配置拦截规则
|
.and()
|
.authorizeRequests()
|
.antMatchers("/sys/user/login").anonymous()
|
.antMatchers("/swagger-ui.html").permitAll()
|
.antMatchers("/webjars/**").permitAll()
|
.antMatchers("/v2/**").permitAll()
|
.antMatchers("/swagger-resources/**").permitAll()
|
.anyRequest().authenticated()
|
|
// 异常处理器
|
.and()
|
.exceptionHandling()
|
.authenticationEntryPoint(jwtAuthenticationEntryPoint)
|
.accessDeniedHandler(jwtAccessDeniedHandler)
|
|
// 配置自定义的过滤器
|
.and()
|
.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
|
}
|
|
@Bean
|
@Override
|
public AuthenticationManager authenticationManagerBean() throws Exception {
|
return super.authenticationManagerBean();
|
}
|
}
|