您想要请求分隔符资源或 URL 处理程序映射。这在 Spring 中很容易。
Servelet 上下文
<!-- 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 [...]
enter link description here
也许你对这个春季安全内容有用。
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”的用户进行内存身份验证
- 允许使用名为的管理用户进行内存身份验证
“管理员”
- 忽略任何以“/resources/”开头的请求。这类似于
使用 XML 命名空间时配置 http@security=none
配置。
- 允许任何人(包括未经身份验证的用户)访问 URL
“/注册”和“/关于”
- 允许任何人(包括未经身份验证的用户)访问 URL
“/login”和“/login?error”。在这种情况下, permitAll() 意味着,
允许访问 formLogin() 使用的任何 URL。
- 任何以“/admin/”开头的 URL 都必须是管理用户。
在我们的示例中,这将是用户“admin”。
- 所有剩余的 URL 都要求用户成功
认证
- 使用 Java 配置设置基于表单的身份验证
默认值。当一个 POST 被提交到
带有参数“用户名”和“密码”的 URL“/login”。
- 明确说明登录页面,表示开发者是
需要在请求 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 与任何 Security 命名空间元素非常相似,
用于网络并且不需要父母(即安全=无,
调试等)。它允许配置影响所有网络的东西
安全性。
- WebSecurityConfigurerAdapter 是一个便利类,它允许
WebSecurity 和 HttpSecurity 的自定义。我们可以扩展
WebSecurityConfigurerAdapter 多次(在不同的对象中)
复制具有多个 http 元素的行为。
- 通过格式化我们的 Java 配置代码,它更易于阅读。
它的读法类似于 XML 命名空间等价物,其中“and()”
表示可选地关闭一个 XML 元素。
Spring Security Java Config Preview