Spring Security 实战干货:如何实现不一样的接口不一样的安全策略

1. 前言

欢迎阅读 Spring Security 实战干货 系列文章 。最近有开发小伙伴提了一个有趣的问题。他正在作一个项目,涉及两种风格,一种是给小程序出接口,安全上使用无状态的JWT Token;另外一种是管理后台使用的是Freemarker,也就是先后端不分离的Session机制。用Spring Security该怎么办?html

2. 解决方案

咱们能够经过屡次继承WebSecurityConfigurerAdapter构建多个HttpSecurityHttpSecurity 对象会告诉咱们如何验证用户的身份,如何进行访问控制,采起的何种策略等等。java

若是你看过以前的教程,咱们是这么配置的:web

/**
 * 单策略配置
 *
 * @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。伴随而来的还有很多的问题要解决。spring

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注解将优先级最低。session

2.3 如何配置不一样的UserDetailsService

不少状况下咱们但愿普通用户和管理用户彻底隔离,咱们就须要多个UserDetailsService,你能够在下面的方法中对AuthenticationManagerBuilder进行具体的设置来配置UserDetailsService,同时也能够配置不一样的密码策略。app

@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 最终的配置模板

上面的几个问题解决以后,咱们基本上掌握了在一个应用中执行多种安全策略。配置模板以下:ide

/**
 * 多个策略配置
 *
 * @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

相关文章
相关标签/搜索