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的不同配置的示例。
 
@EnableWebSecurity
public class MultiHttpSecurityConfig {
@Bean
public UserDetailsService userDetailsService() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withUsername("user").password("password").roles("USER").build());
manager.createUser(User.withUsername("admin").password("password").roles("USER","ADMIN").build());
return manager;
} @Configuration
@Order(1) 1
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/**") 2
.authorizeRequests()
.anyRequest().hasRole("ADMIN")
.and()
.httpBasic();
}
} @Configuration 3
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { @Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}
}
}

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. SSH框架用法,及作用(在一位菜鸟使用半年之后归纳的总结)

    SSH框架从接触以来改变了我对代码的编写方式,从最初开始学习到勉强掌握可以说得到不少心得,以下内容出自java初学者对SSH的理解,如有不对的地方还请谅解,希望可以提出来与我交流,谢谢! SSH顾名思 ...

  2. Java 导出 Excel 列号数字与字母互相转换工具

    package test; /** * Deal with Excel column indexToStr and strToIndex * @author * @version 2015-7-8 * ...

  3. Vue2 几种常见开局方式

    在SF问题中看到了一个关于vue-cli中的template问题,问题是这样的:用vue-cli工具生成的main.js中: import Vue from 'vue' import App from ...

  4. 如何用ABP框架快速完成项目(9) - 用ABP一个人快速完成项目(5) - 不要执着于设计模式和DDD理论,避免原教旨主义

    为什么要写这节文章呢?   首先主动看我这系列文章和参加活动课程的同学, 肯定是积极好学的. 所以很大概率是学过设计模式和DDD理论的. 很大概率不是走一点都不懂设计模式和DDD理论这个极端, 而是走 ...

  5. 3Delight feats. OpenVDB

    Full GI, no multiple scattering now (no point-cloud similar solution in 3Delight now) Blackbody Cart ...

  6. 搞懂Xamarin.Forms布局,看这篇应该就够了吧

    Xamarin.Forms 布局介绍 什么是布局?可以简单的理解为,我们通过将布局元素有效的组织起来,让屏幕变成我们想要的样子! 我们通过画图的方式来描述一下Xamarin.Forms的布局. 小节锚 ...

  7. 个人项目:Java实现WC

    Java实现WC Github项目地址:https://github.com/auxshaw/WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个 ...

  8. ELK的sentinl告警配置详解

    背景 sentinl的监控&告警是通过watch实现的. 一.Watch Execution 执行开始的时候, watcher为watch创建watch执行上下文. 执行上下文提供脚本和模板, ...

  9. 【Apache运维基础(6)】Apache的日志管理与分析

    简述 Apache 访问日志在实际工作中非常有用,比较典型的例子是进行网站流量统计,查看用户访问时间.地理位置分布.页面点击率等.Apache 的访问日志具有如下4个方面的作用: 记录访问服务器的远程 ...

  10. 以太坊之——golang以太坊接口调用

    Go语言具有简单易学.功能强大,可跨平台编译等众多优势,所以这里选择以Go语言切入以太坊. 开始之前需要以下环境: Ubuntu(这里以ubuntu16.04为例) geth Ubuntu16.04安 ...