Spring Security实现不同接口安全策略方法详解

 更新时间:2020年09月04日 09:40:48   作者:码农小胖哥  
这篇文章主要介绍了Spring Security实现不同接口安全策略方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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,同时也可以配置不同的密码策略。

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

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

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

    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java执行JavaScript代码

    Java执行JavaScript代码

    这篇文章主要为大家详细介绍了Java执行JavaScript代码的具体操作方法,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • 深入理解Java中的弱引用

    深入理解Java中的弱引用

    这篇文章主要介绍了深入理解Java中的弱引用,本文讲解了强引用、弱引用、引用队列、四种引用、软引用、虚引用等内容,需要的朋友可以参考下
    2015-01-01
  • Jackson将json string转为Object,org.json读取json数组的实例

    Jackson将json string转为Object,org.json读取json数组的实例

    下面小编就为大家带来一篇Jackson将json string转为Object,org.json读取json数组的实例,具有很好的参考价值,希望对大家有所帮助
    2017-12-12
  • java把字符串转化成公式计算的示例

    java把字符串转化成公式计算的示例

    今天小编就为大家分享一篇java把字符串转化成公式计算的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Spring的BeanFactoryPostProcessor接口示例代码详解

    Spring的BeanFactoryPostProcessor接口示例代码详解

    这篇文章主要介绍了Spring的BeanFactoryPostProcessor接口,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • Java Stream map, Collectors(toMap, toList, toSet, groupingBy, collectingAndThen)使用案例

    Java Stream map, Collectors(toMap, toLis

    这篇文章主要介绍了Java Stream map, Collectors(toMap, toList, toSet, groupingBy, collectingAndThen)使用案例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-09-09
  • SpringBoot实现网站的登陆注册逻辑记录

    SpringBoot实现网站的登陆注册逻辑记录

    登陆注册功能是我们日常开发中经常遇到的一个功能,下面这篇文章主要给大家介绍了关于SpringBoot实现网站的登陆注册逻辑的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-10-10
  • Java日期时间与正则表达式超详细整理(适合新手入门)

    Java日期时间与正则表达式超详细整理(适合新手入门)

    如果使用得当,正则表达式是匹配各种模式的强大工具,下面这篇文章主要给大家介绍了关于Java日期时间与正则表达式超详细整理的相关资料,本文非常适合新手入门,需要的朋友可以参考下
    2023-04-04
  • springMVC的RequestMapping请求不到路径的解决

    springMVC的RequestMapping请求不到路径的解决

    这篇文章主要介绍了springMVC的RequestMapping请求不到路径的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • jasypt SaltGenerator接口定义方法源码解读

    jasypt SaltGenerator接口定义方法源码解读

    这篇文章主要为大家介绍了jasypt SaltGenerator接口定义方法源码解读,,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09

最新评论