1. 程式人生 > >Spring Security 實戰乾貨:如何實現不同的介面不同的安全策略

Spring Security 實戰乾貨:如何實現不同的介面不同的安全策略

![](https://img2020.cnblogs.com/other/1739473/202006/1739473-20200611085644869-1818899529.jpg) ## 1. 前言 歡迎閱讀 [Spring Security 實戰乾貨](https://www.felord.cn/categories/spring-security/) 系列文章 。最近有開發小夥伴提了一個有趣的問題。他正在做一個專案,涉及兩種風格,一種是給小程式出介面,安全上使用無狀態的**JWT Token**;另一種是管理後臺使用的是**Freemarker**,也就是前後端不分離的**Session**機制。用**Spring Security**該怎麼辦? ## 2. 解決方案 我們可以通過多次繼承[WebSecurityConfigurerAdapter](https://felord.cn/spring-security-httpsecurity.html)構建多個`HttpSecurity`。`HttpSecurity` 物件會告訴我們如何驗證使用者的身份,如何進行訪問控制,採取的何種策略等等。 如果你看過之前的教程,我們是這麼配置的: ```java /** * 單策略配置 * * @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)`可以提供過濾機制。比如我們配置: ```java @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`,同時也可以配置不同的密碼策略。 ```java @Override protected 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 最終的配置模板 上面的幾個問題解決之後,我們基本上掌握了在一個應用中執行多種安全策略。配置模板如下: ```java /** * 多個策略配置 * * @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() { @Override public 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() { @Override public 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()); } } } ``` ## 3. 總結 今天我們解決了如何針對不同型別介面採取不同的安全策略的方法,希望對你有用,如果你有什麼問題可以留言。多多關注:**碼農小胖哥**,更多幹貨奉上。 `關注公眾號:Felordcn 獲取更多資訊` [個人部落格:https://felord.cn](https://fe