Spring Security入门(2-3)HttpSecurity的使用

到目前为止我们的 SecurityConfig 只包含了关于如何验证我们的用户的信息。

Spring Security怎么知道我们想对所有的用户进行验证?
Spring Security怎么知道我们需要支持基于表单的验证?
原因是我们的SecurityConfig类继承的WebSecurityConfigurerAdapter在
configure(HttpSecurity http) 方法提供了一个默认的配置,
看起来和下面类似:

protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
}

上面的默认配置说明:
确保我们应用中的所有请求都需要用户被认证
允许用户进行基于表单的认证
允许用户使用HTTP基本验证进行认证
你可以看到这个配置和下面的XML命名配置相似:

<http>

<intercept-url pattern="/**" access="authenticated"/>

<form-login />

<http-basic />

</http>

authorizeRequests(),formLogin()、httpBasic()这三个方法返回的分别是ExpressionUrlAuthorizationConfigurer、FormLoginConfigurer、HttpBasicConfigurer,他们都是SecurityConfigurer接口的实现类,分别代表的是不同类型的安全配置器。
总的来说:HttpSecurity是SecurityBuilder接口的一个实现类,从名字上我们就可以看出这是一个HTTP安全相关的构建器。当然我们在构建的时候可能需要一些配置,当我们调用HttpSecurity对象的方法时,实际上就是在进行配置。
配置的最终结果是什么?
基本上每个SecurityConfigurer子类都对应一个或多个过滤器
可见ExpressionUrlAuthorizationConfigurer、FormLoginConfigurer、HttpBasicConfigurer三个配置器对应的Filter分别是FilterSecurityInterceptor、UsernamePasswordAuthenticationFilter、BasicAuthenticationFilter。
而HttpSecuirty内部维护了一个Filter的List集合,我们添加的各种安全配置器对应的Filter最终都会被加入到这个List集合中。
配置表单登录
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage( "/login")// 1
.permitAll(); // 2
}

1、更新后的配置,指定了登录页面的位置
2、我们必须允许所有的用户,不管是否登录,都可以访问这个页面。 formLogin().permitAll()允许所有用户访问这个页面。
可以自定义用户名和密码的参数名,但是无法修改POST方法请求/login这个URL

.formLogin()
.loginPage( "/login")
.usernameParameter("uname")//自定义用户名参数名称
.passwordParameter("pwd")//自定义密码参数名称

时间: 2024-08-08 13:54:43

Spring Security入门(2-3)HttpSecurity的使用的相关文章

Spring Security 入门(三)

在说完了Spring Security框架的功能和执行流程后,就到了写它Spring Boot的集成,先来看最简的配置: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> </parent>

Spring Security入门

Spring Security入门: http://www.mossle.com/docs/auth/html/index.html Spring Security 默认的过滤器链 https://blog.csdn.net/u013421749/article/details/78626275 原文地址:https://www.cnblogs.com/aligege/p/9396278.html

Spring Security 入门(二)

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

Spring Security 入门原理及实战

参考博客: https://www.cnblogs.com/demingblog/p/10874753.html Spring Security 是spring项目之中的一个安全模块,可以非常方便与spring项目无缝集成. 特别是在spring boot项目中加入spring security更是十分简单, 这里就简单介绍一下入门案例和使用原理吧! 写一个简单的springboot测试例子: @Controller public class AppController { @RequestMa

Spring Security 入门(一)

当你看到这篇文章时,我猜你肯定是碰到令人苦恼的问题了,我希望本文能让你有所收获. 本人几个月前还是 Spring 小白,几个月走来,看了 Spring,Spring boot,到这次的 Spring Security. 为了避免大家踩坑,本人将入门的经验传授给那些和我一样正在学习 Spring Security 的小白. 我们从官方例子学习起: 1.我们先来创建一个 Spring Boot 的项目 HelloWorld: 我用的开发工具是 eclipse,如果没装 STS 的话,请点击 Help

Spring Security 入门(1-3)Spring Security oauth2.0 指南

入门 这是支持OAuth2.0的用户指南.对于OAuth1.0,一切都是不同的,所以看它的用户指南. 本用户指南分为两个部分,第一部分是OAuth2.0提供端(OAuth 2.0 Provider),第二部分是OAuth2.0的客户端(OAuth 2.0 Client) OAuth2.0提供端 OAuth2.0的提供端的用途是负责将受保护的资源暴露出去.建立一个可以访问受保护的资源的客户端列表. 提供端是通过管理和验证可用于访问受保护的资源的OAuth 2令牌来做的. 在适当的地方,提供端必须为

Spring Security 入门(1-14)Spring Security 开发指南(转)

开发指南:http://www.cnblogs.com/xingxueliao/p/5911292.html Spring OAuth2.0 提供者实现原理: Spring OAuth2.0提供者实际上分为: 授权服务 Authorization Service. 资源服务 Resource Service. 虽然这两个提供者有时候可能存在同一个应用程序中,但在Spring Security OAuth中你可以把 他它们各自放在不同的应用上,而且你可以有多个资源服务,它们共享同一个中央授权服 务

Spring Security入门Demo

一.spring Security简介 SpringSecurity,这是一种基于Spring AOP和Servlet过滤器的安全框架.它提供全面的安全性解决方案,同时在Web请求级和方法调用级处理身份确认和授权.在Spring Framework基础上,Spring Security充分利用了依赖注入(DI,Dependency Injection)和面向切面技术. 二.建立工程 参考http://blog.csdn.net/haishu_zheng/article/details/51490

Spring Security 入门详解

序:本文主要参考 spring实战 对里面的知识做一个梳理 1.Spring Security介绍 Spring Security是基于spring的应用程序提供声明式安全保护的安全性框架,它提供了完整的安全性解决方案,能够在web请求级别和方法调用级别处理身份证验证和授权.它充分使用了依赖注入和面向切面的技术. Spring security主要是从两个方面解决安全性问题: web请求级别:使用servlet过滤器保护web请求并限制URL级别的访问 方法调用级别:使用Spring AOP保护