Spring Security(十三):5.2 HttpSecurity
Thus far our WebSecurityConfig only contains information about how to authenticate our users. How does Spring Security know that we want to require all users to be authenticated? How does Spring Security know we want to support form based authentication? The reason for this is that the WebSecurityConfigurerAdapterprovides a default configuration in the configure(HttpSecurity http) method that looks like:
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
}
The default configuration above:
- Ensures that any request to our application requires the user to be authenticated
- 确保对我们的应用程序的任何请求都要求用户进行身份验证
- Allows users to authenticate with form based login
- 允许用户使用基于表单的登录进行身份验证
- Allows users to authenticate with HTTP Basic authentication
- 允许用户使用HTTP基本身份验证进行身份验证
You will notice that this configuration is quite similar the XML Namespace configuration:
<http>
<intercept-url pattern="/**" access="authenticated"/>
<form-login />
<http-basic />
</http>
The Java Configuration equivalent of closing an XML tag is expressed using the and() method which allows us to continue configuring the parent. If you read the code it also makes sense. I want to configure authorized requests and configure form login and configure HTTP Basic authentication.
5.3 Java Configuration and Form Login (Java配置和表单登录)
You might be wondering where the login form came from when you were prompted to log in, since we made no mention of any HTML files or JSPs. Since Spring Security’s default configuration does not explicitly set a URL for the login page, Spring Security generates one automatically, based on the features that are enabled and using standard values for the URL which processes the submitted login, the default target URL the user will be sent to after logging in and so on.
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login") 1
.permitAll(); 2
}
formLogin().permitAll() method allows granting access to all users for all URLs associated with form based log in.The login page below represents our current configuration. We could easily update our configuration if some of the defaults do not meet our needs.
<c:url value="/login" var="loginUrl"/>
<form action="${loginUrl}" method="post"> 1
<c:if test="${param.error != null}"> 2
<p>
Invalid username and password.
</p>
</c:if>
<c:if test="${param.logout != null}"> 3
<p>
You have been logged out.
</p>
</c:if>
<p>
<label for="username">Username</label>
<input type="text" id="username" name="username"/> 4
</p>
<p>
<label for="password">Password</label>
<input type="password" id="password" name="password"/> 5
</p>
<input type="hidden" 6
name="${_csrf.parameterName}"
value="${_csrf.token}"/>
<button type="submit" class="btn">Log in</button>
</form>
1、A POST to the /login URL will attempt to authenticate the user
2、If the query parameter error exists, authentication was attempted and failed
logout exists, the user was successfully logged outSpring Security(十三):5.2 HttpSecurity的更多相关文章
- Spring Security框架下实现两周内自动登录"记住我"功能
本文是Spring Security系列中的一篇.在上一篇文章中,我们通过实现UserDetailsService和UserDetails接口,实现了动态的从数据库加载用户.角色.权限相关信息,从而实 ...
- oauth2 Spring Security
oauth2四种授权方式小结 http://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html 密码模式(resource owner pas ...
- spring security之httpSecurity使用示例
如果在HttpSecurity中配置需要authenticate(),则如果没有登陆,或没有相关权限,则会无法访问 2017-01-02 23:39:32.027 DEBUG 10396 --- [n ...
- spring security之httpSecurity 专题
37.5.2 Resolving the CsrfToken Spring Security provides CsrfTokenArgumentResolver which can automati ...
- SpringBoot第二十三篇:安全性之Spring Security
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11350255.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 系统的安全 ...
- spring security 继承 WebSecurityConfigurerAdapter 的重写方法configure() 参数 HttpSecurity 常用方法及说明
HttpSecurity 常用方法及说明 方法 说明 openidLogin() 用于基于 OpenId 的验证 headers() 将安全标头添加到响应 cors() 配置跨域资源共享( CORS ...
- Spring Security(三十三):10.3 Password Encoding
Spring Security’s PasswordEncoder interface is used to support the use of passwords which are encode ...
- Spring Security(二十三):6.5 The Default AccessDecisionManager(默认接入策略管理)
This section assumes you have some knowledge of the underlying architecture for access-control withi ...
- 使用Spring MVC测试Spring Security Oauth2 API
不是因为看到希望了才去坚持,而坚持了才知道没有希望. 前言 在Spring Security源码分析十一:Spring Security OAuth2整合JWT和Spring Boot 2.0 整合 ...
随机推荐
- angular ng-repeat出来的数据 每条修改数据后返回给接口 如何取到每个对应修改的值
接口结构 $scope.DataList = [ { "dataA":"numA", "dataB":"numB"a } ...
- es6 语法 (数值扩展)
{ //二进制数值都是0b开头,八进制0o console.log(0b111110111) console.log(0o767); } { console.log('15',Number.isFin ...
- 2018-11-13 中文代码示例之Programming in Scala学习笔记第二三章
由于拷贝后文档格式有变, 仅摘几段如下. 完整而且代码带语法高亮的源版在: program-in-chinese/Programming_in_Scala_study_notes_zh 前言: 本书已 ...
- 2018-08-06 在Office的VBA代码里中文命名
在Excel处理数据时, 顺便试了一下VBA代码编辑器里输入中文, 结果显示为乱码. 查了一下发现VBA本身支持Unicode, 但需要设置系统配置使编辑器能够正常显示, 即设置简体中文为Curren ...
- [CSS] 点击事件触发的动画
源码 https://github.com/YouXianMing/CSS-Animations/tree/master/Event 效果 细节 1) 一个完整的可回溯的动画至少包括了两种状态,以及两 ...
- Floyd算法_MATLAB
%求图中任意两点之间的最短距离与最短路径 %floyd算法通用程序,输入a为赋权邻接矩阵 %输出为距离矩阵D,和最短路径矩阵path function D=floyd(a) n=size(a,);%行 ...
- button改变某div内文字内容的显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- (python)数据结构---字典
一.描述 由键值key-value组成的数据的集合 可变.无序的,key不可以重复 字典的键key要可hash(列表.字典.集合不可哈希),不可变的数据结构是可哈希的(字符串.元组.对象.bytes) ...
- 如何在 Flickr 上找到又酷,又有趣,且版权自由的照片?
[编者按]本文作者为 Alex Walker,主要介绍在 Flickr 上进行照片搜索时的一些技巧.本文系国内 ITOM 管理平台 OneAPM 编译呈现. 我们一直都在寻找新奇的,与众不同的设计.图 ...
- java----回文序列判断java
import java.util.Scanner; public class test02 { public static void main(String[] args) { Scanner in ...