當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Security --SecurityConfig的详细配置
生活随笔
收集整理的這篇文章主要介紹了
Spring Security --SecurityConfig的详细配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、SecurityConfig
在之前的文章中,我們從底層源碼的層面了解到,要接管Spring Security的配置,就必須繼承WebSecurityConfigurerAdapter,并加上@EnableWebSecurity注解。
一個比較完整的SecurityConfig配置如下:
@Configuration //開啟判斷用戶對某個控制層的方法是否具有訪問權限的功能 @EnableGlobalMethodSecurity(prePostEnabled = true) @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {//注入自定義的UserDetailService@Autowired@Lazyprivate UserDetailsServiceImpl userDetailsServiceImpl;@Autowiredprivate StringRedisTemplate stringRedisTemplate;//替換默認AuthenticationManager中的UserDetailService,使用數據庫用戶認證方式登錄//1. 一旦通過 configure 方法自定義 AuthenticationManager實現 就回將工廠中自動配置AuthenticationManager 進行覆蓋//2. 一旦通過 configure 方法自定義 AuthenticationManager實現 需要在實現中指定認證數據源對象 UserDetailService 實例//3. 一旦通過 configure 方法自定義 AuthenticationManager實現 這種方式創建AuthenticationManager對象工廠內部本地一個 AuthenticationManager 對象 不允許在其他自定義組件中進行注入@Overrideprotected void configure(AuthenticationManagerBuilder builder) throws Exception {builder.userDetailsService(userDetailsServiceImpl);}/*** BCryptPasswordEncoder相關知識:* 用戶表的密碼通常使用MD5等不可逆算法加密后存儲,為防止彩虹表破解更會先使用一個特定的字符串(如域名)加密,然后再使用一個隨機的salt(鹽值)加密。* 特定字符串是程序代碼中固定的,salt是每個密碼單獨隨機,一般給用戶表加一個字段單獨存儲,比較麻煩。* BCrypt算法將salt隨機并混入最終加密后的密碼,驗證時也無需單獨提供之前的salt,從而無需單獨處理salt問題。*/@Beanpublic BCryptPasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}//將自定義AuthenticationManager在工廠中進行暴露,可以在任何位置注入@Override@Beanpublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}//HttpSecurity配置@Overrideprotected void configure(HttpSecurity http) throws Exception {http.cors(withDefaults())// 禁用 CSRF.csrf().disable().authorizeRequests()// 指定的接口直接放行// swagger.antMatchers(SecurityConstants.SWAGGER_WHITELIST).permitAll().antMatchers(SecurityConstants.H2_CONSOLE).permitAll().antMatchers(HttpMethod.POST, SecurityConstants.SYSTEM_WHITELIST).permitAll()// 其他的接口都需要認證后才能請求.anyRequest().authenticated().and()//添加自定義Filter.addFilter(new JwtAuthorizationFilter(authenticationManager(), stringRedisTemplate))// 不需要session(不創建會話).sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()// 授權異常處理.exceptionHandling()// json提示用戶沒有登錄不需要用戶跳轉到登錄頁面去.authenticationEntryPoint(new JwtAuthenticationEntryPoint())// 權限攔截器,提示用戶沒有當前權限.accessDeniedHandler(new JwtAccessDeniedHandler());// 防止H2 web 頁面的Frame 被攔截http.headers().frameOptions().disable();}/*** Cors配置優化**/@BeanCorsConfigurationSource corsConfigurationSource() {org.springframework.web.cors.CorsConfiguration configuration = new CorsConfiguration();configuration.setAllowedOrigins(singletonList("*"));// configuration.setAllowedOriginPatterns(singletonList("*"));configuration.setAllowedHeaders(singletonList("*"));configuration.setAllowedMethods(Arrays.asList("GET", "POST", "DELETE", "PUT", "OPTIONS"));configuration.setExposedHeaders(singletonList(SecurityConstants.TOKEN_HEADER));configuration.setAllowCredentials(false);configuration.setMaxAge(3600L);UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", configuration);return source;} }二、詳細講解
而WebSecurityConfigurerAdapter 有三個重要的configure 可以覆寫,一個與驗證相關的AuthenticationManagerBuilder,另外兩個是與Web 相關的HttpSecurity 和WebSecurity。
參考文章
總結
以上是生活随笔為你收集整理的Spring Security --SecurityConfig的详细配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【干货】打造优秀B端产品需求分析流程要点
- 下一篇: 拼多多数据全面解析报告