Spring Security(十二):5. Java Configuration

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 ...
}

  

原文地址:https://www.cnblogs.com/shuaiandjun/p/10134138.html

时间: 2024-08-29 05:54:32

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

Java学习笔记—第十二章 Java网络编程入门

第十二章  Java网络编程入门 Java提供的三大类网络功能: (1)URL和URLConnection:三大类中最高级的一种,通过URL网络资源表达方式,可以很容易确定网络上数据的位置.利用URL的表示和建立,Java程序可以直接读入网络上所放的数据,或把自己的数据传送到网络的另一端. (2)Socket:又称"套接字",用于描述IP地址和端口(在Internet中,网络中的每台主机都有一个唯一的IP地址,而每台主机又通过提供多个不同端口来提供多种服务).在客户/服务器网络中,当客

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

摘自 http://blog.csdn.net/liujun13579/article/details/7773945 三十二.Java图形化界面设计--布局管理器之CardLayout(卡片布局) 卡片布局能够让多个组件共享同一个显示空间,共享空间的组件之间的关系就像一叠牌,组件叠在一起,初始时显示该空间中第一个添加的组件,通过CardLayout类提供的方法可以切换该空间中显示的组件. 1.  CardLayout类的常用构造函数及方法 2.  使用CardLayout类提供的方法可以切换显

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

Spring Security 解析(二) -- 认证过程 ??在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security .Spring Security Oauth2 等权限.认证相关的内容.原理及设计学习并整理一遍.本系列文章就是在学习的过程中加强印象和理解所撰写的,如有侵权请告知. 项目环境: JDK1.8 Spring boot 2.x Spring Security 5.x 一.@EnableGlobalAuth

Spring Security(二十二):6.4 Method Security

From version 2.0 onwards Spring Security has improved support substantially for adding security to your service layer methods. It provides support for JSR-250 annotation security as well as the framework's original @Secured annotation. From 3.0 you c

Spring Boot2(十二):手摸手教你搭建Shiro安全框架

一.前言 SpringBoot+Shiro+Mybatis完成的. 之前看了一位小伙伴的Shiro教程,跟着做了,遇到蛮多坑的(′?皿?`) 修改整理了一下,成功跑起来了.可以通过postman进行测试 不多比比∠( ? 」∠)_,直接上源码:https://github.com/niaobulashi/spring-boot-learning/tree/master/spring-boot-20-shiro 二.Shiro是啥 Apache Shiro是一个功能强大.灵活的.开源的安全框架.可

【Spring Security】二、数据库管理用户权限

一 引入相关的jar包 这个例子用的是mysql数据库和c3p0开源的jdbc连接池,在项目的pom.xml中引入jar包 <!-- Mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency>

Spring Security(二):2.1 Introduction What is Spring Security?

Spring Security provides comprehensive security services for Java EE-based enterprise software applications. There is a particular emphasis on supporting projects built using The Spring Framework, which is the leading Java EE solution for enterprise

Spring Security(二)--WebSecurityConfigurer配置以及filter顺序

“致"高级"工程师(BUG工程师) 一颗折腾的心?? 原创不易,点个赞??,支持支持   在认证过程和访问授权前必须了解spring Security如何知道我们要求所有用户都经过身份验证? Spring Security如何知道我们想要支持基于表单的身份验证?因此必须了解WebSecurityConfigurerAdapter配置类如何工作的.而且也必须了解清楚filter的顺序,才能更好了解其调用工作流程. 1. WebSecurityConfigurerAdapter   在使用

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

Spring Security自带的默认数据库存储用户和权限的数据,但是Spring Security默认提供的表结构太过简单了,其实就算默认提供的表结构很复杂,也不一定能满足项目对用户信息和权限信息管理的要求.那么接下来就讲解如何自定义数据库实现对用户信息和权限信息的管理. 一.自定义表结构 这里还是用的mysql数据库,所以pom.xml文件都不用修改.这里只要新建三张表即可,user表.role表.user_role表.其中user用户表,role角色表为保存用户权限数据的主表,user_

Spring Security 入门(二)

Spring Security 入门(一)中说到,Spring Security执行流程第一步是容器启动时加载系统资源与权限列表,第二步是WEB容器启动时加载拦截器链,并介绍了自定义拦截器的方法.接下来第三步步就是用户登录.介绍下用户登录的流程: 获取用户名和密码,并放入一个 UsernamePasswordAuthenticationToken 实例中(Authentication接口的一个实例); 这个token被传递到一个 AuthenticationManager 实例中进行验证; 若验