We can configure multiple HttpSecurity instances just as we can have multiple <http> blocks. The key is to extend the WebSecurityConfigurerAdapter multiple times. For example, the following is an example of having a different configuration for URL’s that start with /api/.

我们可以配置多个HttpSecurity实例,就像我们可以有多个<http>块一样。关键是多次扩展WebSecurityConfigurerAdapter。例如,以下是具有以/ api /开头的URL的不同配置的示例。
 
  1. @EnableWebSecurity
  2. public class MultiHttpSecurityConfig {
  3. @Bean
  4. public UserDetailsService userDetailsService() throws Exception {
  5. InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
  6. manager.createUser(User.withUsername("user").password("password").roles("USER").build());
  7. manager.createUser(User.withUsername("admin").password("password").roles("USER","ADMIN").build());
  8. return manager;
  9. }
  10.  
  11. @Configuration
  12. @Order(1) 1
  13. public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
  14. protected void configure(HttpSecurity http) throws Exception {
  15. http
  16. .antMatcher("/api/**") 2
  17. .authorizeRequests()
  18. .anyRequest().hasRole("ADMIN")
  19. .and()
  20. .httpBasic();
  21. }
  22. }
  23.  
  24. @Configuration 3
  25. public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
  26.  
  27. @Override
  28. protected void configure(HttpSecurity http) throws Exception {
  29. http
  30. .authorizeRequests()
  31. .anyRequest().authenticated()
  32. .and()
  33. .formLogin();
  34. }
  35. }
  36. }

Configure Authentication as normal

正常配置身份验证

1、Create an instance of WebSecurityConfigurerAdapter that contains @Order to specify which WebSecurityConfigurerAdapter should be considered first.

创建包含@Order的WebSecurityConfigurerAdapter实例,以指定应首先考虑哪个WebSecurityConfigurerAdapter。

2、The http.antMatcher states that this HttpSecurity will only be applicable to URLs that start with /api/

http.antMatcher声明此HttpSecurity仅适用于以/ api /开头的URL

3、Create another instance of WebSecurityConfigurerAdapter. If the URL does not start with /api/ this configuration will be used. This configuration is considered after

ApiWebSecurityConfigurationAdapter since it has an @Order value after 1 (no @Order defaults to last).

创建WebSecurityConfigurerAdapter的另一个实例。如果URL不以/ api /开头,则将使用此配置。此配置在ApiWebSecurityConfigurationAdapter之后考虑,因为它在1之后具有@Order值(没有@Order默认为last)。

Spring Security(十六):5.7 Multiple HttpSecurity的更多相关文章

  1. Spring Security 解析(六) —— 基于JWT的单点登陆(SSO)开发及原理解析

    Spring Security 解析(六) -- 基于JWT的单点登陆(SSO)开发及原理解析   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把 ...

  2. Spring Boot(十六):使用Jenkins部署Spring Boot

    Spring Boot(十六):使用Jenkins部署Spring Boot jenkins是devops神器,介绍如何安装和使用jenkins部署Spring Boot项目 jenkins搭建 部署 ...

  3. Spring Security(六):2.3 Release Numbering

    It is useful to understand how Spring Security release numbers work, as it will help you identify th ...

  4. 【Spring Security】六、自定义认证处理的过滤器

    这里接着上一章的自定义过滤器,这里主要的是配置自定义认证处理的过滤器,并加入到FilterChain的过程.在我们自己不在xml做特殊的配置情况下,security默认的做认证处理的过滤器为Usern ...

  5. Spring Security教程(六):自定义过滤器进行认证处理

    这里接着上篇的自定义过滤器,这里主要的是配置自定义认证处理的过滤器,并加入到FilterChain的过程. 在我们自己不在xml做特殊的配置情况下,security默认的做认证处理的过滤器为Usern ...

  6. Spring MVC(十六)--Spring MVC国际化实例

    上一篇文章总结了一下Spring MVC中实现国际化所需的配置,本文继上一文举一个完整的例子,我选择用XML的方式.我的场景是这样的: 访问一个页面时,这个页面有个表格,对表头中的列名实现国际化. 第 ...

  7. 网站开发进阶(十六)错误提示:Multiple annotations found at this line:- basePath cannot be resolved to a variable

    错误提示:Multiple annotations found at this line: basePath cannot be resolved to a variable 出现以上错误,主要是由下 ...

  8. (转)Spring Boot(十六):使用 Jenkins 部署 Spring Boot

    http://www.ityouknow.com/springboot/2017/11/11/spring-boot-jenkins.html enkins 是 Devops 神器,本篇文章介绍如何安 ...

  9. Spring Boot(十六):使用 Jenkins 部署 Spring Boot

    Jenkins 是 Devops 神器,本篇文章介绍如何安装和使用 Jenkins 部署 Spring Boot 项目 Jenkins 搭建.部署分为四个步骤: 第一步,Jenkins 安装 第二步, ...

  10. spring boot(十六)使用Jenkins部署spring boot

    jenkins是devops神器,本篇文章介绍如何安装和使用jenkins部署Spring Boot项目 jenkins搭建 部署分为三个步骤: 第一步,jenkins安装 第二步,插件安装和配置 第 ...

随机推荐

  1. 51NOD 1185 威佐夫游戏 V2(威佐夫博弈)

    1185 威佐夫游戏 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取 ...

  2. BZOJ 1874: [BeiJing2009 WinterCamp]取石子游戏(SG函数)

    Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 871  Solved: 365[Submit][Status][Discuss] Description ...

  3. 2018-12-03 VS Code英汉词典插件v0.0.7-尝试词性搭配

    续上文VS Code英汉词典插件v0.0.6-改为TS实现, 加测试后, 继续重构(提取常量, 避免var, 添加类型等等), 并完善测试. 测试方法参考: Testing Visual Studio ...

  4. SAP MM PR中的Fixed ID字段与MD04里PR单据号后的星号

    SAP MM PR中的Fixed ID字段与MD04里PR单据号后的星号 如下图是我手工使用ME51N 创建的一个采购申请单据, ​ MD04去看这个PR, ​ 这个PR号码后面有一个*号,代表它是一 ...

  5. Openlayer3之绚丽的界面框架-Materialize

    一群做C++的老伙计搞前端开发,徒手写html和css应该会折寿..在网上找了半天,Materialize算是用起来很方便的一款前端界面框架.Google的Material Design看起来感觉还是 ...

  6. JMeter 正则表达式提取器结合ForEach控制器遍历提取变量值

    正则表达式提取器结合ForEach控制器遍历提取变量值   by:授客 QQ:1033553122 1. 需要解决的问题 使用正则提取器提取了一组变量值,需要在其它sampler中,循环引用组中的某几 ...

  7. C# 开启一个新进程并为新进程设置工作目录

    Process p = new System.Diagnostics.Process(); //设置新进程的工作目录,如果不设置那么新进程的工作目录为开启这个进程的工作目录 p.StartInfo.W ...

  8. 接口的绑定方案和动态SQL

    1. 接口绑定方案 MyBatis中, 提供了一套接口绑定方案. 程序员可以提供一个接口, 然后提供对应接口的一个mapper.xml文件. MyBatis会自动将接口和xml文件进行绑定. 实际上就 ...

  9. LeetCode题解之Squares of a Sorted Array

    1.题目描述 2.问题分析 使用过两个计数器. 3.代码 class Solution { public: vector<int> sortedSquares(vector<int& ...

  10. java POI导出Excel文件数据库的数据

    在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.这里我简单实现导出Excel文件. POI jar ...