我花了最近3-4天的时间与Spring Security(4.0.2)一起工作,并且由于倾注了Spring Security样本,SO和其他博客上的大量帖子,我已经能够使用它.
然而,正如我一直在尝试不同的选项,我已经被困扰了几个小时,将sessionManagement添加到HttpSecurity.似乎选项的顺序很重要,我真的很好奇为什么会这样,以及为什么它似乎没有在Spring Security文档中的任何地方提及,或者我能找到的任何其他地方?
例如,如果先放置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);
}
Run Code Online (Sandbox Code Playgroud)
似乎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
Run Code Online (Sandbox Code Playgroud)
几个小时后,我发现invalidSessionUrl和maximumSessions是SessionManagementConfigurer的方法,expiredUrl和maxSessionsPreventsLogin属于SessionManagementConfigurer.ConcurrencyControlConfigurer,代码编译的唯一方法是在 SessionManagementConfigurer方法之后放置ConcurrencyControlConfigurer 方法.
再一次,我真的很想知道为什么所以我可以警惕这种事情,因为我学习了其他的Spring接口.换句话说,我真的想知道这里是否涉及到一个建筑设计或编程约定,我还没有意识到它是Spring的新手.
顺便说一下,Rob Winch的网络研讨会非常有帮助!如果有人有兴趣,这里是链接:网络研讨会重播:Spring Security 3.2
添加几个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()
...
Run Code Online (Sandbox Code Playgroud)
中的一个缩进级别表示一种新的返回类型(仅为了清楚起见)。返回and()到之前的类型。
| 归档时间: |
|
| 查看次数: |
1502 次 |
| 最近记录: |