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:

到目前为止,我们的WebSecurityConfig仅包含有关如何验证用户身份的信息。 Spring Security如何知道我们要求所有用户进行身份验证? Spring Security如何知道我们想要支持基于表单的身份验证?原因是WebSecurityConfigurerAdapter在configure(HttpSecurity http)方法中提供了一个默认配置,如下所示:
 
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:

您会注意到此配置与XML命名空间配置非常相似:
 
<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.

使用and()方法表示关闭XML标记的Java配置,这允许我们继续配置父标记。如果您阅读代码,它也是有道理的。我想配置授权请求并配置表单登录并配置HTTP基本身份验证。
 
However, Java Configuration has different defaults URLs and parameters. Keep this in mind when creating custom login pages. The result is that our URLs are more RESTful. Additionally, it is not quite so obvious we are using Spring Security which helps to prevent information leaks. For example:
 
但是,Java Configuration具有不同的默认URL和参数。创建自定义登录页面时请记住这一点。结果是我们的URL更加RESTful。此外,我们使用Spring Security有助于防止信息泄露,这一点并不十分明显。例如:
 

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.

当您被提示登录时,您可能想知道登录表单的来源,因为我们没有提及任何HTML文件或JSP。由于Spring Security的默认配置未明确设置登录页面的URL,因此Spring Security会根据启用的功能自动生成一个URL,并使用处理提交的登录的URL的标准值,用户将使用的默认目标URL登录后发送给等等。
 
While the automatically generated log in page is convenient to get up and running quickly, most applications will want to provide their own log in page. To do so we can update our configuration as seen below:
 
虽然自动生成的登录页面便于快速启动和运行,但大多数应用程序都希望提供自己的登录页面。为此,我们可以更新我们的配置,如下所示:
 
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login") 1
.permitAll(); 2
}
 
 1、The updated configuration specifies the location of the log in page.
更新的配置指定登录页面的位置。
 
 2、We must grant all users (i.e. unauthenticated users) access to our log in page. The formLogin().permitAll() method allows granting access to all users for all URLs associated with form based log in.
我们必须授予所有用户(即未经身份验证的用户)访问我们的登录页面的权限。 formLogin()。permitAll()方法允许为与基于表单的登录相关联的所有URL授予对所有用户的访问权限。
 
An example log in page implemented with JSPs for our current configuration can be seen below:

使用JSP为我们当前配置实现的示例登录页面如下所示:
 

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 

对/ login URL的POST将尝试对用户进行身份验证

2、If the query parameter error exists, authentication was attempted and failed

如果存在查询参数错误,则尝试进行身份验证并失败
3、If the query parameter logout exists, the user was successfully logged out
如果存在查询参数注销,则表示用户已成功注销
 4、The username must be present as the HTTP parameter named username
用户名必须作为名为username的HTTP参数出现
 5、The password must be present as the HTTP parameter named password
密码必须作为名为password的HTTP参数出现
 6、We must Section 18.4.3, “Include the CSRF Token” To learn more read the Chapter 18, Cross Site Request Forgery (CSRF) section of the reference
我们必须在第18.4.3节“包含CSRF令牌”中了解更多信息,请参阅第18章,跨站点请求伪造(CSRF)部分的参考
 
 
 
 
 
 

Spring Security(十三):5.2 HttpSecurity的更多相关文章

  1. Spring Security框架下实现两周内自动登录"记住我"功能

    本文是Spring Security系列中的一篇.在上一篇文章中,我们通过实现UserDetailsService和UserDetails接口,实现了动态的从数据库加载用户.角色.权限相关信息,从而实 ...

  2. oauth2 Spring Security

    oauth2四种授权方式小结 http://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html 密码模式(resource owner pas ...

  3. spring security之httpSecurity使用示例

    如果在HttpSecurity中配置需要authenticate(),则如果没有登陆,或没有相关权限,则会无法访问 2017-01-02 23:39:32.027 DEBUG 10396 --- [n ...

  4. spring security之httpSecurity 专题

    37.5.2 Resolving the CsrfToken Spring Security provides CsrfTokenArgumentResolver which can automati ...

  5. SpringBoot第二十三篇:安全性之Spring Security

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11350255.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   系统的安全 ...

  6. spring security 继承 WebSecurityConfigurerAdapter 的重写方法configure() 参数 HttpSecurity 常用方法及说明

    HttpSecurity 常用方法及说明 方法 说明 openidLogin() 用于基于 OpenId 的验证 headers() 将安全标头添加到响应 cors() 配置跨域资源共享( CORS ...

  7. Spring Security(三十三):10.3 Password Encoding

    Spring Security’s PasswordEncoder interface is used to support the use of passwords which are encode ...

  8. Spring Security(二十三):6.5 The Default AccessDecisionManager(默认接入策略管理)

    This section assumes you have some knowledge of the underlying architecture for access-control withi ...

  9. 使用Spring MVC测试Spring Security Oauth2 API

    不是因为看到希望了才去坚持,而坚持了才知道没有希望. 前言 在Spring Security源码分析十一:Spring Security OAuth2整合JWT和Spring Boot 2.0 整合 ...

随机推荐

  1. JS中的可枚举属性与不可枚举属性以及扩展

    在JavaScript中,对象的属性分为可枚举和不可枚举之分,它们是由属性的enumerable值决定的.可枚举性决定了这个属性能否被for…in查找遍历到. 一.怎么判断属性是否可枚举 js中基本包 ...

  2. ICML 2018 | 从强化学习到生成模型:40篇值得一读的论文

    https://blog.csdn.net/y80gDg1/article/details/81463731 感谢阅读腾讯AI Lab微信号第34篇文章.当地时间 7 月 10-15 日,第 35 届 ...

  3. mysql的连接

    本文内容: mysql客户端连接服务端 断开连接 首发日期:2018-04-07 mysql客户端连接服务端: 连接服务端的命令基本语法是: mysql -h host -u user -p -P p ...

  4. AIOps 平台的误解,挑战及建议(中)— AIOps常见的误解

    本文篇幅较长,分为上,中,下,三个部分进行连载.内容分别为:AIOps 背景/所应具备技术能力分析(上),AIOps 常见的误解(中),挑战及建议(下). 前言 我大概是 5,6 年前开始接触 ITO ...

  5. Scala抽象类型

    package big.data.analyse.scala import scala.io.{BufferedSource, Source} /** * 抽象类型 * Created by zhen ...

  6. [20180813]刷新共享池与父子游标.txt

    [20180813]刷新共享池与父子游标.txt --//测试刷新共享池与父子游标含有那些信息保存在共享池.--//自己最近遇到的问题,感觉自己以前理解有点乱,测试看看. 1.环境SCOTT@book ...

  7. 上传github文件及所出现的问题

    上传github所发现的问题 准备工作 使用 git bush 输入下面的命令 git config --global user.email "you@example.com" g ...

  8. Python语法的转义字符

    Python语法的转义字符 转义字符 说 明 \ 续行符 \n 换行符 \0 空  \t 水平制表符,用于横向跳到下一制表位 \'' 双引号 \' 单引号 \\ 一个反斜杠 \f 换页 \0dd 八进 ...

  9. Windows单机最大TCP连接数的问题

    本文和大家分享一下Windows下单机最大TCP连接数,因为在做Socket 编程时,我们经常会要问,单机最多可以建立多少个 TCP 连接,本文将介绍如何调整系统参数来调整单机的最大TCP连接数. W ...

  10. ASP.NET -- WebForm -- Session的使用

    ASP.NET -- WebForm --  Session的使用 Session是服务器端状态保持机制. 1. Test4.aspx文件与Test4.aspx.cs文件 <%@ Page La ...