在前一篇,我已经介绍了Spring Security Java配置,也归纳的介绍了一下这个项目方方面面。在这篇文章中,咱们来看一看一个简单的基于web security配置的例子。以后咱们再来做更多的我的定制。php
在这个部分,咱们对一个基于web的security做一些基本的配置。能够分红四个部分:html
@EnableWebSecurity注解以及WebSecurityConfigurerAdapter一块儿配合提供基于web的security。继承了WebSecurityConfigurerAdapter以后,再加上几行代码,咱们就能实现如下的功能:java
1
2
3
4
5
6
7
8
9
10
|
@Configuration
@EnableWebSecurity
public
class
HelloWebSecurityConfiguration
extends
WebSecurityConfigurerAdapter {
@Override
protected
void
registerAuthentication(AuthenticationManagerBuilder auth) {
auth.inMemoryAuthentication()
.withUser(
"user"
).password(
"password"
).roles(
"USER"
);
}
}
|
做为参考,咱们在这里也给出类似的XML配置,不过有几个特殊配置:git
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<
http
use-expressions
=
"true"
>
<
intercept-url
pattern
=
"/**"
access
=
"authenticated"
/>
<
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-service
>
</
authentication-provider
>
</
authentication-manager
>
|
AbstractAnnotationConfigDispatcherServletInitializergithub
下一步就是保证ApplicationContext包含咱们刚刚定义的HelloWebSecurityConfiguration。有几种方法均可行,咱们这里使用Spring的AbstractAnnotationConfigDispatcherServletInitializer:web
1
2
3
4
5
6
7
8
9
|
public
class
SpringWebMvcInitializer
extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected
Class[] getRootConfigClasses() {
return
new
Class[] { HelloWebSecurityConfiguration.
class
};
}
...
}
|
Spring Security一般在web.xml中包含下面几行代码进行初始化:spring
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<!-- Load all Spring XML configuration including our security.xml file -->
<
context-param
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>/WEB-INF/spring/*.xml</
param-value
>
</
context-param
>
|
最后一步,咱们须要对springSecurityFilterChain定义映射路径。咱们很容易经过继承AbstractSecurityWebApplicationInitializer实现,并能够有选择的经过覆盖方法来定制映射。express
下面是最基本的配置,它能够接受默认的映射路径,springSecurityFilterChain具备如下的特性:api
1
2
3
|
public
class
SecurityWebApplicationInitializer
extends
AbstractSecurityWebApplicationInitializer {
}
|
上面的代码等同于将这几行代码放在web.xml中:安全
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<
filter
>
<
filter-name
>springSecurityFilterChain</
filter-name
>
<
filter-class
>
org.springframework.web.filter.DelegatingFilterProxy
</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>springSecurityFilterChain</
filter-name
>
<
url-pattern
>/*</
url-pattern
>
<
dispatcher
>ERROR</
dispatcher
>
<
dispatcher
>REQUEST</
dispatcher
>
</
filter-mapping
>
|
WebApplicationInitializer的次序
在AbstractSecurityWebApplicationInitializer启动以后再加入的servlet过滤器映射,它们有可能会加在springSecurityFilterChain以前。除非这个应用不须要安全验证,不然springSecurityFilterChain须要放在其它全部的过滤器映射以前。@Order能够保证任何WebApplicationInitializer都使用特定的顺序加载。
这个HelloWebSecurityConfiguration范例,为咱们很好的展现了Spring Security Java配置是如何工做的。让咱们来看看更多定制的配置吧。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
@EnableWebSecurity
@Configuration
public
class
CustomWebSecurityConfigurerAdapter
extends
WebSecurityConfigurerAdapter {
@Override
protected
void
registerAuthentication(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
能够实现如下功能:
下面的XML配置和上面的Java配置相似:
在看过了更复杂的例子以后,你可能已经找到了一些XML命名空间和Java配置的类似之处。我在这里说明几条有用的信息:
你已经意识到了XML和Java配置的不一样之处
/login
登录页面,而不是访问/spring_security_login
/login,而不是/j_spring_security_check
form-login
和intercept-url
中重复两次”/login”,而在Java配置中,咱们靠#5就轻易作到了让用户都能访问到和formLogin()相关的URL。咱们还提供了更多的示例,你应该想跃跃欲试了吧:
若是你以为从XML转变成Java配置有必定困难,你能够先看看这些测试。这些测试中,XML元素的名称以”Namespace”开头,中间是XML元素的名称,而后以”Tests”结尾。例如,你想学习如何将http元素转换成Java配置,你能够看看NamespaceHttpTests;你若是想学习如何将remember-me命名空间转换成Java配置,参见NamespaceRememberMeTests
若是你发现了bug,或者以为有什么地方值得改进,请你不要犹豫,给咱们留言!咱们但愿听到你的想法,以便在大部分人得到代码以前,咱们便确保代码的正确性。很早的尝试新功能是一种简单有效的回馈社区的方法,这样作的好处就是能帮助你得到你但愿得到的功能。
请到“Java Config”目录下的Spring Security JIRA记录下任何问题。记录了一个JIRA以后,咱们但愿(固然并非必须的)你在pull request中提交你的代码。你能够在贡献者指引中阅读更详细的步骤。若是你有任何不清楚的,请使用Spring Security论坛或者Stack Overflow,并使用”spring-security”标签(我会一直查看这个标签)。若是你针对这个博客有任何意见,也请留言。使用合适的工具对每一个人来讲都会带来便利。
你可能已经对基于web的security的Java配置已经有了必定的认识了。下一篇中,咱们将会带你来看下如何用Java配置来配置基于method的security。
原文连接: Springsource 翻译: ImportNew.com - 唐小娟
译文连接: http://www.importnew.com/5641.html
[ 转载请保留原文出处、译者和译文连接。]