
package me.zhengjIE.core.config;import me.zhengjIE.core.security.JwtAuthenticationEntryPoint;import me.zhengjIE.core.security.JwtAuthorizationTokenFilter;import me.zhengjIE.core.service.JwtUserDetailsService;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.httpMethod;import org.springframework.security.authentication.AuthenticationManager;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;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.builders.WebSecurity;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 WebSecurityConfig extends WebSecurityConfigurerAdapter { @autowired private JwtAuthenticationEntryPoint unauthorizedHandler; @autowired private JwtUserDetailsService jwtUserDetailsService; /** * 自定义基于JWT的安全过滤器 */ @autowired JwtAuthorizationTokenFilter authenticationTokenFilter; @Value("${jwt.header}") private String tokenheader; @Value("${jwt.auth.path}") private String authenticationPath; @autowired public voID configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .userDetailsService(jwtUserDetailsService) .passwordEncoder(passwordEncoderBean()); } @Bean public PasswordEncoder passwordEncoderBean() { return new BCryptPasswordEncoder(); } @Bean @OverrIDe public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @OverrIDe protected voID configure(httpSecurity httpSecurity) throws Exception { httpSecurity // 禁用 CSRF .csrf().disable() // 授权异常 .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() // 不创建会话 .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() .authorizeRequests() .antMatchers("/auth/**").permitAll() .antMatchers("/websocket/**").permitAll() .antMatchers("/druID/**").anonymous() // 支付宝回调 .antMatchers("/API/aliPay/return").anonymous() .antMatchers("/API/aliPay/notify").anonymous() // swagger start .antMatchers("/swagger-ui.HTML").anonymous() .antMatchers("/swagger-resources/**").anonymous() .antMatchers("/webjars/**").anonymous() .antMatchers("/*/API-docs").anonymous() // swagger end .antMatchers("/test/**").anonymous() .antMatchers(httpMethod.OPTIONS,"/**").anonymous() // 所有请求都需要认证 .anyRequest().authenticated(); httpSecurity .addFilterBefore(authenticationTokenFilter,UsernamePasswordAuthenticationFilter.class); } @OverrIDe public voID configure(WebSecurity web) throws Exception { // AuthenticationTokenFilter will ignore the below paths web.ignoring() .antMatchers( httpMethod.POST,authenticationPath ) // allow anonymous resource requests .and() .ignoring() .antMatchers( httpMethod.GET,"/*.HTML","/**/*.HTML","/**/*.CSS","/**/*.Js" ); }}总结
以上是内存溢出为你收集整理的WebSecurityConfig全部内容,希望文章能够帮你解决WebSecurityConfig所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)