在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面。在这篇文章中,我们来看一看一个简单的基于web security配置的例子。之后我们再来作更多的个人定制。
在这个部分,我们对一个基于web的security作一些基本的配置。可以分成四个部分:
@EnableWebSecurity注解以及WebSecurityConfigurerAdapter一起配合提供基于web的security。继承了WebSecurityConfigurerAdapter之后,再加上几行代码,我们就能实现以下的功能:
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配置,不过有几个特殊配置:
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`>
AbstractAnnotationConfigDispatcherServletInitializer
下一步就是保证ApplicationContext包含我们刚刚定义的HelloWebSecurityConfiguration。有几种方法都可行,我们这里使用Spring的AbstractAnnotationConfigDispatcherServletInitializer:
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中包含下面几行代码进行初始化:
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实现,并可以有选择的通过覆盖方法来定制映射。
下面是最基本的配置,它可以接受默认的映射路径,springSecurityFilterChain具有以下的特性:
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配置的不同之处
Java配置有一些不同的默认URL和参数。当要创建自定义的登陆页面的时候要将这一条牢记在心。默认的URL使我们的URL更加RESTful。另外,使用Spring Security可以帮我避免信息泄露)。例如:
/login登陆页面,而不是访问/spring_security_login/login,而不是/j_spring_security_checkform-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
[ 转载请保留原文出处、译者和译文链接。]
Original url: Access
Created at: 2019-11-19 14:44:40
Category: default
Tags: none
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论