在过去的3-4天里,我第一次使用了Security (4.0.2),并且能够让它开始工作,这要归功于对Security示例的大量介绍,以及在SO和其他博客上的大量帖子。
然而,由于我一直在尝试不同的选项,我在HttpSecurity中添加了HttpSecurity,这使我陷入了几个小时的困境。看来选项的顺序很重要,我真的很好奇,为什么在Spring安全文档中没有提到它,也没有提到其他我能找到的其他地方?
例如,如果您将sessionManagement放在第一个位置,那么下一个配置(在本例中是authorizeRequests,但不管下一个是哪个)都会得到下面代码示例中指出的语法错误。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.sessionManagement()
.invalidSessionUrl("/login?invalid=1")
.maximumSessions(1)
.expiredUrl("/login?time=1")
.maxSessionsPreventsLogin(true);
.authorizeRequests() //<<< The method authorizeRequests() is undefined for the type SecurityConfig
.antMatchers("/", "/home", "/login**", "/thankyou").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?err=1")
.permitAll()
.and()
.logout()
.logoutSuccessUrl("/thankyou")
.deleteCookies( "JSESSIONID" )
.invalidateHttpSession(false);
}看来sessionManagement必须是最后一次配置。为什么?
而且,一旦我最后一次放置sessionManagement,它就会使invalidSessionUrl方法放置的位置有所不同。我最初使用的是最后一个语法错误,如下所示:
.sessionManagement()
.maximumSessions(1)
.expiredUrl("/login?time=1")
.maxSessionsPreventsLogin(true)
.invalidSessionUrl("/login?invalid=1");
//<<< The method invalidSessionUrl(String) is undefined for the type SessionManagementConfigurer<HttpSecurity>.ConcurrencyControlConfigurer几个小时后,我发现invalidSessionUrl和maximumSessions是SessionManagementConfigurer的方法,expiredUrl和maxSessionsPreventsLogin属于SessionManagementConfigurer,而代码编译的唯一方法是在(SessionManagementConfigurer)方法之后放置ConcurrencyControlConfigurer方法。
同样,我真的很想知道为什么,所以在我学习其他Spring接口时,我可以对这种事情保持警惕。换句话说,我真的很想知道这里是否涉及到作为Spring新手的架构设计或编程约定。
顺便说一句,罗布·温奇的网络研讨会非常有用!如果有人感兴趣,下面是链接:网络研讨会重播: Spring安全3.2
发布于 2015-09-07 07:32:39
添加几个and(),它将编译:
protected void configure(HttpSecurity http) throws Exception {
http
.sessionManagement()
.invalidSessionUrl("/login?invalid=1")
.maximumSessions(1)
.expiredUrl("/login?time=1")
.maxSessionsPreventsLogin(true)
.and()
.and()
.authorizeRequests()
...一个缩进级别指示一个新的返回类型(仅为清晰起见)。and()返回到前一个类型。
https://stackoverflow.com/questions/32416764
复制相似问题