首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >配置--为什么选项的顺序很重要?

配置--为什么选项的顺序很重要?
EN

Stack Overflow用户
提问于 2015-09-05 19:11:42
回答 1查看 2.2K关注 0票数 6

在过去的3-4天里,我第一次使用了Security (4.0.2),并且能够让它开始工作,这要归功于对Security示例的大量介绍,以及在SO和其他博客上的大量帖子。

然而,由于我一直在尝试不同的选项,我在HttpSecurity中添加了HttpSecurity,这使我陷入了几个小时的困境。看来选项的顺序很重要,我真的很好奇,为什么在Spring安全文档中没有提到它,也没有提到其他我能找到的其他地方?

例如,如果您将sessionManagement放在第一个位置,那么下一个配置(在本例中是authorizeRequests,但不管下一个是哪个)都会得到下面代码示例中指出的语法错误。

代码语言:javascript
复制
@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方法放置的位置有所不同。我最初使用的是最后一个语法错误,如下所示:

代码语言:javascript
复制
    .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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-07 07:32:39

添加几个and(),它将编译:

代码语言:javascript
复制
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()返回到前一个类型。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32416764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档