国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Spring Security實現不同接口安全策略方法詳解

瀏覽:33日期:2023-08-16 08:00:28

1. 前言

歡迎閱讀 Spring Security 實戰干貨 系列文章 。最近有開發小伙伴提了一個有趣的問題。他正在做一個項目,涉及兩種風格,一種是給小程序出接口,安全上使用無狀態的JWT Token;另一種是管理后臺使用的是Freemarker,也就是前后端不分離的Session機制。用Spring Security該怎么辦?

2. 解決方案

我們可以通過多次繼承WebSecurityConfigurerAdapter構建多個HttpSecurity。HttpSecurity 對象會告訴我們如何驗證用戶的身份,如何進行訪問控制,采取的何種策略等等。

我們是這么配置的:

/** * 單策略配置 * * @author felord.cn * @see org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration * @since 14 :58 2019/10/15 */@Configuration@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true, securedEnabled = true)@EnableWebSecurity@ConditionalOnClass(WebSecurityConfigurerAdapter.class)@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)public class CustomSpringBootWebSecurityConfiguration { /** * The type Default configurer adapter. */ @Configuration @Order(SecurityProperties.BASIC_AUTH_ORDER) static class DefaultConfigurerAdapter extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { super.configure(auth); } @Override public void configure(WebSecurity web) { super.configure(web); } @Override protected void configure(HttpSecurity http) throws Exception { // 配置 httpSecurity } }}

上面的配置了一個HttpSecurity,我們如法炮制再增加一個WebSecurityConfigurerAdapter的子類來配置另一個HttpSecurity。伴隨而來的還有不少的問題要解決。

2.1 如何路由不同的安全配置我們配置了兩個HttpSecurity之后,程序如何讓小程序接口和后臺接口走對應的HttpSecurity?

HttpSecurity.antMatcher(String antPattern)可以提供過濾機制。比如我們配置:

@Override protected void configure(HttpSecurity http) throws Exception { // 配置 httpSecurity http.antMatcher('/admin/v1'); }

那么該HttpSecurity將只提供給以/admin/v1開頭的所有URL。這要求我們針對不同的客戶端指定統一的URL前綴。

舉一反三只要HttpSecurity提供的功能都可以進行個性化定制。比如登錄方式,角色體系等。

2.2 如何指定默認的 HttpSecurity

我們可以通過在WebSecurityConfigurerAdapter實現上使用@Order注解來指定優先級,數值越大優先級越低,沒有@Order注解將優先級最低。

2.3 如何配置不同的 UserDetailsService

很多情況下我們希望普通用戶和管理用戶完全隔離,我們就需要多個UserDetailsService,你可以在下面的方法中對AuthenticationManagerBuilder進行具體的設置來配置UserDetailsService,同時也可以配置不同的密碼策略。

@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception { DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); daoAuthenticationProvider.setUserDetailsService(new UserDetailsService() { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 自行實現 return null ; } }); // 也可以設計特定的密碼策略 BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder); auth.authenticationProvider(daoAuthenticationProvider);}

2.4 最終的配置模板

上面的幾個問題解決之后,我們基本上掌握了在一個應用中執行多種安全策略。配置模板如下:

/** * 多個策略配置 * * @author felord.cn * @see org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration * @since 14 :58 2019/10/15 */@Configuration@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true, securedEnabled = true)@EnableWebSecurity@ConditionalOnClass(WebSecurityConfigurerAdapter.class)@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)public class CustomSpringBootWebSecurityConfiguration { /** * 后臺接口安全策略. 默認配置 */ @Configuration @Order(1) static class AdminConfigurerAdapter extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); //用戶詳情服務個性化 daoAuthenticationProvider.setUserDetailsService(new UserDetailsService() {@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 自行實現 return null;} }); // 也可以設計特定的密碼策略 BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder); auth.authenticationProvider(daoAuthenticationProvider); } @Override public void configure(WebSecurity web) { super.configure(web); } @Override protected void configure(HttpSecurity http) throws Exception { // 根據需求自行定制 http.antMatcher('/admin/v1') .sessionManagement(Customizer.withDefaults()) .formLogin(Customizer.withDefaults()); } } /** * app接口安全策略. 沒有{@link Order}注解優先級比上面低 */ @Configuration static class AppConfigurerAdapter extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); //用戶詳情服務個性化 daoAuthenticationProvider.setUserDetailsService(new UserDetailsService() {@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 自行實現 return null;} }); // 也可以設計特定的密碼策略 BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder); auth.authenticationProvider(daoAuthenticationProvider); } @Override public void configure(WebSecurity web) { super.configure(web); } @Override protected void configure(HttpSecurity http) throws Exception { // 根據需求自行定制 http.antMatcher('/app/v1') .sessionManagement(Customizer.withDefaults()) .formLogin(Customizer.withDefaults()); } }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 亚洲国产成人久久一区二区三区 | 91精品国 | 欧美人拘一级毛片 | 国产亚洲精品看片在线观看 | 在线观看片成人免费视频 | 欧美成人激情 | 在线观看亚洲精品国产 | 亚洲网站视频在线观看 | 午夜性生活视频 | 亚洲欧美一区二区久久香蕉 | 性感毛片| 欧美大胆一级视频 | 久久午夜鲁丝片午夜精品 | 久久99精品久久久久久h | 理论在线看 | 一级特黄a免费大片 | 国产成人深夜福利在线观看 | 亚洲国产一区二区在线 | 欧美成人午夜 | 女人张开腿让男人桶免费网站 | 亚洲综合精品一二三区在线 | 亚洲产国偷v产偷v自拍涩爱 | 真人毛片视频 | 国内精品久久久久久久久久影视 | 亚洲精品视频在线看 | 中国女人18xnxx视频 | 91精品国产爱久久久久 | 亚洲欧美日韩国产精品久久 | 黄色三级视频网站 | 特级毛片全部免费播放a一级 | 亚洲成成品网站有线 | 在线播放性xxx欧美 在线播放亚洲视频 | 亚洲国产一区二区a毛片日本 | 欧美精品久久久久久久免费观看 | 成人亚洲精品一区 | 亚洲国产成人在人网站天堂 | 成人综合婷婷国产精品久久免费 | 欧美一级鲁丝片免费看 | 国产免费一区二区在线看 | 欧美一区二区aa大片 | 国产成人久久久精品毛片 |