General support for Java Configuration was added to Spring Framework in Spring 3.1. Since Spring Security 3.2 there has been Spring Security Java Configuration support which enables users to easily configure Spring Security without the use of any XML.

Spring 3.1在Spring Framework中添加了对Java Configuration的一般支持。自Spring Security 3.2以来,Spring Security Java Configuration支持使用户无需使用任何XML即可轻松配置Spring Security。
 
If you are familiar with the Chapter 6, Security Namespace Configuration then you should find quite a few similarities between it and the Security Java Configuration support.
如果您熟悉第6章安全命名空间配置,那么您应该发现它与安全Java配置支持之间有很多相似之处。
 

Spring Security provides lots of sample applications which demonstrate the use of Spring Security Java Configuration.

Spring Security提供了许多示例应用程序,用于演示Spring Security Java Configuration的使用。
 

5.1 Hello Web Security Java Configuration

The first step is to create our Spring Security Java Configuration. The configuration creates a Servlet Filter known as the springSecurityFilterChain which is responsible for all the security (protecting the application URLs, validating submitted username and passwords, redirecting to the log in form, etc) within your application. You can find the most basic example of a Spring Security Java Configuration below:

第一步是创建Spring Security Java配置。该配置创建一个名为springSecurityFilterChain的Servlet过滤器,它负责应用程序中的所有安全性(保护应用程序URL,验证提交的用户名和密码,重定向到登录表单等)。您可以在下面找到Spring Security Java配置的最基本示例:
 
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.*;
import org.springframework.security.config.annotation.authentication.builders.*;
import org.springframework.security.config.annotation.web.configuration.*; @EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Bean
public UserDetailsService userDetailsService() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withUsername("user").password("password").roles("USER").build());
return manager;
}
}

There really isn’t much to this configuration, but it does a lot. You can find a summary of the features below:

这种配置确实没什么用,但它做了很多。您可以在下面找到以下功能的摘要:
 

5.1.1 AbstractSecurityWebApplicationInitializer

The next step is to register the springSecurityFilterChain with the war. This can be done in Java Configuration with Spring’s WebApplicationInitializer support in a Servlet 3.0+ environment. Not suprisingly, Spring Security provides a base class AbstractSecurityWebApplicationInitializer that will ensure the springSecurityFilterChain gets registered for you. The way in which we use AbstractSecurityWebApplicationInitializer differs depending on if we are already using Spring or if Spring Security is the only Spring component in our application.

下一步是使用war注册springSecurityFilterChain。这可以在Java配置中使用Spring的WebApplicationInitializer支持在Servlet 3.0+环境中完成。不出所料,Spring Security提供了一个基类AbstractSecurityWebApplicationInitializer,它将确保为您注册springSecurityFilterChain。我们使用AbstractSecurityWebApplicationInitializer的方式取决于我们是否已经使用Spring,或者Spring Security是否是我们应用程序中唯一的Spring组件。
 

5.1.2 AbstractSecurityWebApplicationInitializer without Existing Spring  (没有现有的)

If you are not using Spring or Spring MVC, you will need to pass in the WebSecurityConfig into the superclass to ensure the configuration is picked up. You can find an example below:

如果您不使用Spring或Spring MVC,则需要将WebSecurityConfig传递到超类中以确保获取配置。你可以在下面找到一个例子:
 
import org.springframework.security.web.context.*;

public class SecurityWebApplicationInitializer
extends AbstractSecurityWebApplicationInitializer { public SecurityWebApplicationInitializer() {
super(WebSecurityConfig.class);
}
}

The SecurityWebApplicationInitializer will do the following things:

SecurityWebApplicationInitializer将执行以下操作:
  • Automatically register the springSecurityFilterChain Filter for every URL in your application
  • 自动为应用程序中的每个URL注册springSecurityFilterChain过滤器
     
  • Add a ContextLoaderListener that loads the WebSecurityConfig.
  • 添加一个加载WebSecurityConfig的ContextLoaderListener。

5.1.3 AbstractSecurityWebApplicationInitializer with Spring MVC

5.1.3使用Spring MVC的AbstractSecurityWebApplicationInitializer

If we were using Spring elsewhere in our application we probably already had a WebApplicationInitializer that is loading our Spring Configuration. If we use the previous configuration we would get an error. Instead, we should register Spring Security with the existing ApplicationContext. For example, if we were using Spring MVC our SecurityWebApplicationInitializer would look something like the following:

如果我们在应用程序的其他地方使用Spring,我们可能已经有了一个加载Spring配置的WebApplicationInitializer。如果我们使用以前的配置,我们会收到错误。相反,我们应该使用现有的ApplicationContext注册Spring Security。例如,如果我们使用Spring MVC,我们的SecurityWebApplicationInitializer将如下所示:
 
This would simply only register the springSecurityFilterChain Filter for every URL in your application. After that we would ensure that WebSecurityConfig was loaded in our existing ApplicationInitializer. For example, if we were using Spring MVC it would be added in the getRootConfigClasses()
 
这只会为应用程序中的每个URL注册springSecurityFilterChain过滤器。之后,我们将确保在现有的ApplicationInitializer中加载WebSecurityConfig。例如,如果我们使用Spring MVC,它将被添加到getRootConfigClasses()中
 
public class MvcWebApplicationInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer { @Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { WebSecurityConfig.class };
} // ... other overrides ...
}

  

 
 
 
 
 
 

Spring Security(十二):5. Java Configuration的更多相关文章

  1. Spring Boot(十二):spring boot如何测试打包部署

    Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...

  2. Spring Security(二十七):Part II. Architecture and Implementation

    Once you are familiar with setting up and running some namespace-configuration based applications, y ...

  3. 三十二、Java图形化界面设计——布局管理器之CardLayout(卡片布局)

    摘自 http://blog.csdn.net/liujun13579/article/details/7773945 三十二.Java图形化界面设计--布局管理器之CardLayout(卡片布局) ...

  4. JAVA之旅(三十二)——JAVA网络请求,IP地址,TCP/UDP通讯协议概述,Socket,UDP传输,多线程UDP聊天应用

    JAVA之旅(三十二)--JAVA网络请求,IP地址,TCP/UDP通讯协议概述,Socket,UDP传输,多线程UDP聊天应用 GUI写到一半电脑系统挂了,也就算了,最多GUI还有一个提示框和实例, ...

  5. Spring Security(二)

    Spring Security(二) 注:凡是源码部分,我已经把英文注释去掉了,有兴趣的同学可以在自己项目里进去看看.:-) 定义用户认证逻辑 用户登录成功后,用户的信息会被 Security 封装在 ...

  6. 20155301第十二周java课程程序

    20155301第十二周java课程程序 内容一:在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 String类 charAt split Ar ...

  7. Spring Security 解析(二) —— 认证过程

    Spring Security 解析(二) -- 认证过程   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security .S ...

  8. Spring Security教程(二):自定义数据库查询

    Spring Security教程(二):自定义数据库查询   Spring Security自带的默认数据库存储用户和权限的数据,但是Spring Security默认提供的表结构太过简单了,其实就 ...

  9. 《手把手教你》系列技巧篇(三十二)-java+ selenium自动化测试-select 下拉框(详解教程)

    1.简介 在实际自动化测试过程中,我们也避免不了会遇到下拉选择的测试,因此宏哥在这里直接分享和介绍一下,希望小伙伴或者童鞋们在以后工作中遇到可以有所帮助. 2.select 下拉框 2.1Select ...

  10. 《手把手教你》系列技巧篇(五十二)-java+ selenium自动化测试-处理面包屑(详细教程)

    1.简介 面包屑(Breadcrumb),又称面包屑导航(BreadcrumbNavigation)这个概念来自童话故事"汉赛尔和格莱特",当汉赛尔和格莱特穿过森林时,不小心迷路了 ...

随机推荐

  1. H5新增特性、方法

    1.FileReader和progress实现实时监控文件上传进度 2.HTML5新增的客户端校验 1.调用checkValidity方法进行校验 2.setCustomValidity自定义错误

  2. Tomcat post参数长处理

    如下图所示:增加maxPostSize="-1"属性即可

  3. JNI NDK (AndroidStudio+CMake )实现C C++调用Java代码流程

    JNI/NDK Java调用C/C++前言  通过第三篇文章讲解在实际的开发过程中Java层调用C/C++层的处理流程.其实我们在很大的业务里也需要C/C+ +层去调用Java层,这两层之间的相互调用 ...

  4. DAY3(PYTHON)

    一.or 和and的区别 X OR Y,如果X非0,则为X X OR Y,如果X为真,则为Y 二.continue 跳出当次循环 break 跳出循环 三.#输出1-2+3-4+5-6+......- ...

  5. 编写寄宿于windows服务的WCF服务

    由于业务中有些任务需要在后台静默长期运行,或者有些服务队响应的要求比较苛刻,这样的WCF服务就不适合寄宿于IIS中.IIS每隔一段时间w3wp进程会闲置超时,造成服务的运行停止,因此这种耗时或者定时任 ...

  6. 【底层原理】深入理解Cache (下)

    得到了我的PC的cache参数如下: L1 Cache : 32KB , 8路组相连,linesize为 64Byte 64个组 L2 Cache:256KB 8路组相连,linesize为 64By ...

  7. python爬虫之12306网站--车站信息查询

    python爬虫查询车站信息 目录: 1.找到要查询的url 2.对信息进行分析 3.对信息进行处理 python爬虫查询全拼相同的车站 目录: 1.找到要查询的url 2.对信息进行分析 3.对信息 ...

  8. [Hive_add_4] Hive 命令行客户端 Beeline 的使用

    0. 说明 Hive 命令行客户端 beeline 的使用,建立在启动  Hadoop 集群和启动 hiveserver2 的基础之上 1. 使用指南 在确保集群启动和 hiveserver2 启动的 ...

  9. 常用判断重复记录的SQL语句

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from people where peopleId in (select   peopleId  fro ...

  10. jQuery -- 光阴似箭(三):jQuery 操作 HTML 元素和属性

    jQuery -- 知识点回顾篇(三):jQuery拥有操作 HTML 元素和属性的强大方法. 1. 获取HTML 元素的内容和属性 (1) 获得内容:  text().html() 以及 val() ...