Our examples have only required users to be authenticated and have done so for every URL in our application. We can specify custom requirements for our URLs by adding multiple children to our http.authorizeRequests() method. For example:

我们的示例仅要求用户进行身份验证,并且已针对应用程序中的每个URL进行了身份验证。我们可以通过向http.authorizeRequests()方法添加多个子项来指定URL的自定义要求。例如:
 
  1. protected void configure(HttpSecurity http) throws Exception {
  2. http
  3. .authorizeRequests() 1
  4. .antMatchers("/resources/**", "/signup", "/about").permitAll() 2
  5. .antMatchers("/admin/**").hasRole("ADMIN") 3
  6. .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") 4
  7. .anyRequest().authenticated() 5
  8. .and()
  9. // ...
  10. .formLogin();
  11. }

1、There are multiple children to the http.authorizeRequests() method each matcher is considered in the order they were declared.

http.authorizeRequests()方法有多个子节点,每个匹配器按其声明的顺序进行考虑。

2、We specified multiple URL patterns that any user can access. Specifically, any user can access a request if the URL starts with "/resources/", equals "/signup", or equals "/about".

我们指定了任何用户都可以访问的多种URL模式。具体来说,如果URL以“/ resources /”开头,等于“/ signup”或等于“/ about”,则任何用户都可以访问请求。

3、Any URL that starts with "/admin/" will be restricted to users who have the role "ROLE_ADMIN". You will notice that since we are invoking the hasRole method we do not need to specify the "ROLE_" prefix.

任何以“/ admin /”开头的URL都将仅限于具有“ROLE_ADMIN”角色的用户。您会注意到,由于我们正在调用hasRole方法,因此我们不需要指定“ROLE_”前缀。

4、Any URL that starts with "/db/" requires the user to have both "ROLE_ADMIN" and "ROLE_DBA". You will notice that since we are using the hasRole expression we do not need to specify the "ROLE_" prefix.

任何以“/ db /”开头的URL都要求用户同时拥有“ROLE_ADMIN”和“ROLE_DBA”。您会注意到,由于我们使用的是hasRole表达式,因此我们不需要指定“ROLE_”前缀。

5、Any URL that has not already been matched on only requires that the user be authenticated

任何尚未匹配的URL只需要对用户进行身份验证

5.5 Handling Logouts 处理注销

When using the WebSecurityConfigurerAdapter, logout capabilities are automatically applied. The default is that accessing the URL /logout will log the user out by:

使用WebSecurityConfigurerAdapter时,会自动应用注销功能。默认情况下,访问URL / logout将通过以下方式记录用户:
  • Invalidating the HTTP Session
  • 使HTTP会话无效
  • Cleaning up any RememberMe authentication that was configured
  • 清理已配置的任何RememberMe身份验证
  • Clearing the SecurityContextHolder
  • 清除SecurityContextHolder
  • Redirect to /login?logout
  • 重定向到/ login?logout

Similar to configuring login capabilities, however, you also have various options to further customize your logout requirements:

但是,与配置登录功能类似,您还可以使用各种选项来进一步自定义注销要求:
  1. protected void configure(HttpSecurity http) throws Exception {
  2. http
  3. .logout() 1
  4. .logoutUrl("/my/logout") 2
  5. .logoutSuccessUrl("/my/index") 3
  6. .logoutSuccessHandler(logoutSuccessHandler) 4
  7. .invalidateHttpSession(true) 5
  8. .addLogoutHandler(logoutHandler) 6
  9. .deleteCookies(cookieNamesToClear) 7
  10. .and()
  11. ...
  12. }

1、Provides logout support. This is automatically applied when using WebSecurityConfigurerAdapter.

提供注销支持。使用WebSecurityConfigurerAdapter时会自动应用此选项。

2、The URL that triggers log out to occur (default is /logout). If CSRF protection is enabled (default), then the request must also be a POST. For more information, please consult the JavaDoc.

触发注销的URL(默认为/ logout)。如果启用了CSRF保护(默认),则该请求也必须是POST。有关更多信息,请参阅JavaDoc。

3、The URL to redirect to after logout has occurred. The default is /login?logout. For more information, please consult the JavaDoc.

注销后重定向到的URL。默认为/ login?logout。有关更多信息,请参阅JavaDoc。

4、Let’s you specify a custom LogoutSuccessHandler. If this is specified, logoutSuccessUrl() is ignored. For more information, please consult the JavaDoc.

我们指定一个自定义的LogoutSuccessHandler。如果指定了此参数,则忽略logoutSuccessUrl()。有关更多信息,请参阅JavaDoc。

5、Specify whether to invalidate the HttpSession at the time of logout. This is true by default. Configures the SecurityContextLogoutHandler under the covers. For more information, please consult the JavaDoc.

指定在注销时是否使HttpSession无效。默认情况下这是真的。配置封面下的SecurityContextLogoutHandler。有关更多信息,请参阅JavaDoc。

6、Adds a LogoutHandler. SecurityContextLogoutHandler is added as the last LogoutHandler by default.

添加LogoutHandler。默认情况下,SecurityContextLogoutHandler被添加为最后一个LogoutHandler。

7、Allows specifying the names of cookies to be removed on logout success. This is a shortcut for adding a CookieClearingLogoutHandler explicitly.

允许指定在注销成功时删除的cookie的名称。这是显式添加CookieClearingLogoutHandler的快捷方式。
 
Logouts can of course also be configured using the XML Namespace notation. Please see the documentation for the logout element in the Spring Security XML Namespace section for further details.
当然也可以使用XML命名空间表示法配置注销。有关更多详细信息,请参阅Spring Security XML Namespace部分中logout元素的文档。
 
Generally, in order to customize logout functionality, you can add LogoutHandler and/or LogoutSuccessHandler implementations. For many common scenarios, these handlers are applied under the covers when using the fluent API.
通常,为了自定义注销功能,您可以添加LogoutHandler和/或LogoutSuccessHandler实现。对于许多常见场景,这些处理程序在使用流畅的API时应用于幕后。

5.5.1 LogoutHandler (登出处理)

Generally, LogoutHandler implementations indicate classes that are able to participate in logout handling. They are expected to be invoked to perform necessary clean-up. As such they should not throw exceptions. Various implementations are provided:

通常,LogoutHandler实现指示能够参与注销处理的类。预计将调用它们以进行必要的清理。因此,他们不应该抛出异常。提供了各种实现:
 

Please see Section 17.4, “Remember-Me Interfaces and Implementations” for details.

有关详细信息,请参见第17.4节“记住我的接口和实现”。
 
Instead of providing LogoutHandler implementations directly, the fluent API also provides shortcuts that provide the respective LogoutHandler implementations under the covers. E.g. deleteCookies() allows specifying the names of one or more cookies to be removed on logout success. This is a shortcut compared to adding aCookieClearingLogoutHandler.
 
而不是直接提供LogoutHandler实现,流畅的API还提供了快捷方式,提供了相应的LogoutHandler实现。例如。 deleteCookies()允许指定在注销成功时删除的一个或多个cookie的名称。与添加CookieClearingLogoutHandler相比,这是一种快捷方式。
 
The following implementations are provided:
提供以下实现:

As mentioned above, you don’t need to specify the SimpleUrlLogoutSuccessHandler directly. Instead, the fluent API provides a shortcut by setting the logoutSuccessUrl(). This will setup the SimpleUrlLogoutSuccessHandler under the covers. The provided URL will be redirected to after a logout has occurred. The default is /login?logout.

如上所述,您不需要直接指定SimpleUrlLogoutSuccessHandler。相反,fluent API通过设置logoutSuccessUrl()来提供快捷方式。这将在封面下设置SimpleUrlLogoutSuccessHandler。发生注销后,提供的URL将重定向到。默认为/ login?logout。
 

5.5.3 Further Logout-Related References

Spring Security(十四):5.4 Authorize Requests的更多相关文章

  1. Spring Boot(十四):spring boot整合shiro-登录认证和权限管理

    Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...

  2. Spring Security 解析(四) ——短信登录开发

    Spring Security 解析(四) -- 短信登录开发   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security ...

  3. Spring 学习十四 Spring security安全

    Spring security: 我用过的安全机制:   oauth2, filter,  secured方法保护 9.2  保护web请求: 9.2.1  代理Servlet过滤器: Delegat ...

  4. Spring Security(四):2.1 Introduction What is Spring Security?

    Spring Security provides comprehensive security services for Java EE-based enterprise software appli ...

  5. 【Spring Security】四、自定义页面

    在前面例子中,登陆页面都是用的Spring Security自己提供的,这明显不符合实际开发场景,同时也没有退出和注销按钮,因此在每次测试的时候都要通过关闭浏览器来注销达到清除session的效果. ...

  6. Spring Security教程(四):自定义登录页

    在前面的例子中,登陆页面都是用的Spring Security自己提供的,这明显不符合实际开发场景,同时也没有退出和注销按钮,因此在每次测试的时候都要通过关闭浏览器来注销达到清除session的效果. ...

  7. Spring Security教程(四)

    在前面三个博客的例子中,登陆页面都是用的Spring Security自己提供的,这明显不符合实际开发场景,同时也没有退出和注销按钮,因此在每次测试的时候都要通过关闭浏览器来注销达到清除session ...

  8. (转)Spring Boot (十四): Spring Boot 整合 Shiro-登录认证和权限管理

    http://www.ityouknow.com/springboot/2017/06/26/spring-boot-shiro.html 这篇文章我们来学习如何使用 Spring Boot 集成 A ...

  9. spring boot(十四)shiro登录认证与权限管理

    这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在Java领域一般有Spring Security ...

  10. Spring Boot (十四): Spring Boot 整合 Shiro-登录认证和权限管理

    这篇文章我们来学习如何使用 Spring Boot 集成 Apache Shiro .安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在 Java 领域一般有 Spring S ...

随机推荐

  1. Spider-two

    一.网络数据加密:1. md5 / sha1 不可逆加密算法: 结果是十六进制数, 结果不可逆, 多用于文件验证 import hashlib md5_obj = hashlib.md5() sha1 ...

  2. 获取邮箱的DNS和MX 工具类

    1.导入Maven  DNS  包: <dependency> <groupId>dnsjava</groupId> <artifactId>dnsja ...

  3. file上传图片,base64转换、压缩图片、预览图片、将图片旋转到正确的角度

    /** * 将base64转换为文件对象 * (即用文件上传输入框上传文件得到的对象) * @param {String} base64 base64字符串 */ function convertBa ...

  4. nginx中有关 root 和 alias的主要区别

    举个例子给伙伴们区别就明显看出来了,例子如下: location /img/ { alias /var/www/image/; }注意:如果按照上述配置的话,则访问/img/目录里面的文件时,ning ...

  5. C# 利用ZXing.Net来生成条形码和二维码

    本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正. 什么是ZXing.Net? ZXing是一个开放源码的,用Java实现的多种格式的 ...

  6. 微信小程序-全国快递查询

    微信小程序-全国快递查询 摘要:WeChat.小程序.JS 开发过程 源码下载 1. GitHub 2. 百度云 链接:https://pan.baidu.com/s/1XVbtT2JsZslg4Y0 ...

  7. (后端)springboot 在idea中实现热部署(转)

    自己用到了iIntelliJ IDEA 这个ide工具,但是和以前的工具写html,css,js直接刷新页面不同,这个需要去热部署,网上搜的解决方法: SpringBoot的web项目,在每一次修改了 ...

  8. JavaWeb:servlet实现下载与上传功能

    本文内容: servlet实现下载功能 servlet实现上传功能 首发日期:2018-07-21 servlet实现下载功能 实现流程 1.首先制作一个jsp页面,主要是用来触发下载的.这里可以根据 ...

  9. 使用VSTS的Git进行版本控制(三)——评审历史记录

    使用VSTS的Git进行版本控制(三)--评审历史记录 Git使用存储在每个提交中的父引用信息来管理开发的完整历史记录.评审该提交历史记录,能够找出文件更改的时间,并确定代码版本之间的差异. Git使 ...

  10. [20190306]奇怪的查询结果.txt

    [20190306]奇怪的查询结果.txt--//链接http://www.itpub.net/thread-2108588-1-1.html提到一个非常古怪的问题,我自己重复测试看看:1.环境:SC ...