Spring Security實(shí)現(xiàn)不同接口安全策略方法詳解
1. 前言
歡迎閱讀 Spring Security 實(shí)戰(zhàn)干貨 系列文章 。最近有開(kāi)發(fā)小伙伴提了一個(gè)有趣的問(wèn)題。他正在做一個(gè)項(xiàng)目,涉及兩種風(fēng)格,一種是給小程序出接口,安全上使用無(wú)狀態(tài)的JWT Token;另一種是管理后臺(tái)使用的是Freemarker,也就是前后端不分離的Session機(jī)制。用Spring Security該怎么辦?
2. 解決方案
我們可以通過(guò)多次繼承WebSecurityConfigurerAdapter構(gòu)建多個(gè)HttpSecurity。HttpSecurity 對(duì)象會(huì)告訴我們?nèi)绾悟?yàn)證用戶(hù)的身份,如何進(jìn)行訪(fǎng)問(wèn)控制,采取的何種策略等等。
我們是這么配置的:
/** * 單策略配置 * * @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 } }}
上面的配置了一個(gè)HttpSecurity,我們?nèi)绶ㄅ谥圃僭黾右粋€(gè)WebSecurityConfigurerAdapter的子類(lèi)來(lái)配置另一個(gè)HttpSecurity。伴隨而來(lái)的還有不少的問(wèn)題要解決。
2.1 如何路由不同的安全配置我們配置了兩個(gè)HttpSecurity之后,程序如何讓小程序接口和后臺(tái)接口走對(duì)應(yīng)的HttpSecurity?
HttpSecurity.antMatcher(String antPattern)可以提供過(guò)濾機(jī)制。比如我們配置:
@Override protected void configure(HttpSecurity http) throws Exception { // 配置 httpSecurity http.antMatcher('/admin/v1'); }
那么該HttpSecurity將只提供給以/admin/v1開(kāi)頭的所有URL。這要求我們針對(duì)不同的客戶(hù)端指定統(tǒng)一的URL前綴。
舉一反三只要HttpSecurity提供的功能都可以進(jìn)行個(gè)性化定制。比如登錄方式,角色體系等。
2.2 如何指定默認(rèn)的 HttpSecurity
我們可以通過(guò)在WebSecurityConfigurerAdapter實(shí)現(xiàn)上使用@Order注解來(lái)指定優(yōu)先級(jí),數(shù)值越大優(yōu)先級(jí)越低,沒(méi)有@Order注解將優(yōu)先級(jí)最低。
2.3 如何配置不同的 UserDetailsService
很多情況下我們希望普通用戶(hù)和管理用戶(hù)完全隔離,我們就需要多個(gè)UserDetailsService,你可以在下面的方法中對(duì)AuthenticationManagerBuilder進(jìn)行具體的設(shè)置來(lái)配置UserDetailsService,同時(shí)也可以配置不同的密碼策略。
@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception { DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); daoAuthenticationProvider.setUserDetailsService(new UserDetailsService() { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 自行實(shí)現(xiàn) return null ; } }); // 也可以設(shè)計(jì)特定的密碼策略 BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder); auth.authenticationProvider(daoAuthenticationProvider);}
2.4 最終的配置模板
上面的幾個(gè)問(wèn)題解決之后,我們基本上掌握了在一個(gè)應(yīng)用中執(zhí)行多種安全策略。配置模板如下:
/** * 多個(gè)策略配置 * * @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 { /** * 后臺(tái)接口安全策略. 默認(rèn)配置 */ @Configuration @Order(1) static class AdminConfigurerAdapter extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); //用戶(hù)詳情服務(wù)個(gè)性化 daoAuthenticationProvider.setUserDetailsService(new UserDetailsService() {@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 自行實(shí)現(xiàn) return null;} }); // 也可以設(shè)計(jì)特定的密碼策略 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 { // 根據(jù)需求自行定制 http.antMatcher('/admin/v1') .sessionManagement(Customizer.withDefaults()) .formLogin(Customizer.withDefaults()); } } /** * app接口安全策略. 沒(méi)有{@link Order}注解優(yōu)先級(jí)比上面低 */ @Configuration static class AppConfigurerAdapter extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); //用戶(hù)詳情服務(wù)個(gè)性化 daoAuthenticationProvider.setUserDetailsService(new UserDetailsService() {@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 自行實(shí)現(xiàn) return null;} }); // 也可以設(shè)計(jì)特定的密碼策略 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 { // 根據(jù)需求自行定制 http.antMatcher('/app/v1') .sessionManagement(Customizer.withDefaults()) .formLogin(Customizer.withDefaults()); } }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 概述IE和SQL2k開(kāi)發(fā)一個(gè)XML聊天程序2. js開(kāi)發(fā)中的頁(yè)面、屏幕、瀏覽器的位置原理(高度寬度)說(shuō)明講解(附圖)3. ASP動(dòng)態(tài)include文件4. CSS百分比padding制作圖片自適應(yīng)布局5. vue跳轉(zhuǎn)頁(yè)面常用的幾種方法匯總6. 不要在HTML中濫用div7. XML入門(mén)的常見(jiàn)問(wèn)題(三)8. XML 非法字符(轉(zhuǎn)義字符)9. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)10. CSS清除浮動(dòng)方法匯總
