我们可以从 Hello Spring Security 扩展 Spring Security 配置,以通过 URL 改变访问控制。在本节中,您将找到一种配置,该配置允许更精细地控制资源的访问方式。在配置中,Spring Security 执行以下任务:
- It completely ignores any request that starts with /resources/. This is beneficial since our images, CSS, and JavaScript do not need to use Spring Security.
- It allows anonymous users to access the Welcome, Login, and Logout pages.
- It only allows administrators access to the All Events page.
- It adds an administrator that can access the All Events page.
看看下面的代码片段:
请注意,我们不包括
/calendar,应用程序的上下文根,在 Spring Security 配置中,因为 Spring Security 透明地为我们处理上下文根。这样,如果我们决定将其部署到不同的上下文根,我们就不需要更新我们的配置。
在 Spring Security 4,2 中,您可以使用构建器模式指定多个 RequestMatcher 条目,该构建器模式允许您更好地控制如何将安全性应用于应用程序的不同部分。第一个 antMatchers() 方法声明 Spring Security 应该忽略任何以 /resources/ 开头的 URL,第二个 antMatchers() 方法声明任何其他请求都将由它处理。关于使用多个 antMatchers 方法,有几点需要注意,如下所示:
- If no path attribute is specified, it is the equivalent of using a path of /**, which matches all requests.
- Each antMatchers() method is considered in order, and only the first match is applied. So, the order in which they appear in your configuration file is important. The implication is that only the last antMatchers() method can use a path that matches every request. If you do not follow this rule, Spring Security will produce an error. The following is invalid because the first matcher matches every request and will never get to the second mapping:
- The default pattern is backed by o.s.s.web.util.AntPathRequestMatcher, which will compare the specified pattern to an ant pattern to determine whether it matches the servletPath and pathInfo methods of HttpServletRequest. Note that query strings are ignored when determining whether a request is a match. Internally, Spring Security uses o.s.u.AntPathMatcher to do all the work. A summary of the rules is listed as follows:
- The path attribute on the antMatchers() method further refines the filtering of the request and allows access control to be applied. You can see that the updated configuration allows different types of access, depending on the URL pattern. The role ANONYMOUS is of particular interest since we have not defined it anywhere in SecurityConfig.java. This is the default authority assigned to a user that is not logged in. The following line, from the updates to our SecurityConfig.java file, is what allows anonymous (unauthenticated) users and users with the role USER authority to access the Login page. We will cover access control options in more detail in the second half of the book:
在定义 antMatchers() 方法时,需要牢记许多事项,包括以下内容:
-
- Just as each http method is considered from top to bottom, so are the antMatchers() methods. This means it is important to specify the most specific elements first. The following example illustrates a configuration that does not specify the more specific pattern first, which will result in warnings from Spring Security at startup:
- It is important to note that if http.authorizeRequests() is marked anyRequest(), there can be no child antMatchers() method defined. This is because anyRequest() will match all requests that match this http.authorizeRequests() tag. Defining an antMatchers() child method with anyRequest() contradicts the antMatchers() declaration. An example is as follows:
- The path attribute of the antMatchers() element is independent and is not aware of the anyRequest() attribute of the http method.
如果您还没有这样做,请重新启动应用程序并访问 http://localhost:8080。试用该应用程序以查看您所做的所有更新,如下所示:
- Select a link that requires authentication and observes the new login page.
- Try typing an invalid username/password and view the error message.
- Try logging in as an admin ([email protected]/admin1), and view all of the events. Note that we are able to view all the events.
- Try logging out and view the logout success message.
- Try logging in as a regular user ([email protected]/user1), and view all of the events. Note that we get an Access Denied page.
您的代码现在应该类似于 chapter02.03-calendar。