您想要请求分隔符资源或 URL 处理程序映射。这在春天很容易。
Servlet 上下文
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
和
<default-servlet-handler />
此标记允许将 DispatcherServlet 映射到“/”(从而覆盖容器默认 Servlet 的映射),同时仍然允许容器的默认 Servlet 处理静态资源请求 [...]
在此处输入链接描述
也许你有用这个春天的安全内容。
CustomWebSecurityConfigurerAdapter
我们的 HelloWebSecurityConfiguration 示例演示了 Spring Security Java 配置可以为我们提供一些非常好的默认值。让我们看一些基本的自定义。
@EnableWebSecurity
@Configuration
public class CustomWebSecurityConfigurerAdapter extends
WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) {
auth
.inMemoryAuthentication()
.withUser("user") // #1
.password("password")
.roles("USER")
.and()
.withUser("admin") // #2
.password("password")
.roles("ADMIN","USER");
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**"); // #3
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeUrls()
.antMatchers("/signup","/about").permitAll() // #4
.antMatchers("/admin/**").hasRole("ADMIN") // #6
.anyRequest().authenticated() // 7
.and()
.formLogin() // #8
.loginUrl("/login") // #9
.permitAll(); // #5
}
}
假设我们调整 AbstractAnnotationConfigDispatcherServletInitializer 以加载我们的新配置,我们的 CustomWebSecurityConfigurerAdapter 将执行以下操作:
- 允许使用名为“user”的用户进行内存身份验证</li>
- 允许使用名为“admin”的管理用户进行内存身份验证</li>
- 忽略任何以“/resources/”开头的请求。这类似于在使用 XML 命名空间配置时配置 http@security=none。
- 允许任何人(包括未经身份验证的用户)访问网址“/signup”和“/about”</li>
- 允许任何人(包括未经身份验证的用户)访问 URL“/login”和“/login?error”。在这种情况下, permitAll() 意味着允许访问 formLogin() 使用的任何 URL。
- 任何以“/admin/”开头的 URL 都必须是管理用户。对于我们的示例,这将是用户“admin”。
- 所有剩余的 URL 都要求用户成功通过身份验证
- 使用 Java 配置默认设置基于表单的身份验证。当使用参数“username”和“password”向 URL“/login”提交 POST 时执行身份验证。
- 显式声明登录页面,这意味着当请求 GET /login 时,需要开发人员呈现登录页面。
对于熟悉基于 XML 的配置的人来说,上面的配置与下面的 XML 配置非常相似:
<http security="none" pattern="/resources/**"/>
<http use-expressions="true">
<intercept-url pattern="/logout" access="permitAll"/>
<intercept-url pattern="/login" access="permitAll"/>
<intercept-url pattern="/signup" access="permitAll"/>
<intercept-url pattern="/about" access="permitAll"/>
<intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
<logout
logout-success-url="/login?logout"
logout-url="/logout"
/>
<form-login
authentication-failure-url="/login?error"
login-page="/login"
login-processing-url="/login"
password-parameter="password"
username-parameter="username"
/>
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="user"
password="password"
authorities="ROLE_USER"/>
<user name="admin"
password="password"
authorities="ROLE_USER,ROLE_ADMIN"/>
</user-service>
</authentication-provider>
</authentication-manager>
与 XML 命名空间的相似之处
在查看了我们稍微复杂一点的示例之后,您可能会发现 XML 命名空间和 Java 配置之间的一些相似之处。以下是一些更有用的点:
- HttpSecurity 与 http 命名空间元素非常相似。它允许为特定选择(在本例中为所有)请求配置基于 Web 的安全性。
- WebSecurity 与任何用于Web 且不需要父级的Security 命名空间元素非常相似(即security=none、debug 等)。它允许配置影响所有网络安全的事物。
- WebSecurityConfigurerAdapter 是一个方便类,它允许对 WebSecurity 和 HttpSecurity 进行自定义。我们可以多次扩展 WebSecurityConfigurerAdapter(在不同的对象中)以复制具有多个 http 元素的行为。
- 通过格式化我们的 Java 配置代码,它更容易阅读。它可以类似于等效的 XML 命名空间来读取,其中“and()”表示可选地关闭 XML 元素。
Spring Security Java 配置预览