2013-12-12 163 views
4

我使用的是具有java配置和两个HttpSecurity配置的spring-security 3.2.0.RC2。一个用于REST API,另一个用于UI。 当我发布到/注销它重定向到/ login?注销,但然后(不正确)重定向到/登录。 当我成功输入用户名和密码时,我将重定向到登录?注销,并且必须再次输入凭据以进入主页面。 所以看起来像permitAll登录是不是荣幸的登录?注销。弹簧安全登录?注销重定向登录

我的安全配置是这样的:

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

@Resource 
private MyUserDetailsService userDetailsService; 

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) 
     throws Exception { 
    StandardPasswordEncoder encoder = new StandardPasswordEncoder(); 
    auth.userDetailsService(userDetailsService).passwordEncoder(encoder); 
} 

@Configuration 
@Order(1) 
public static class RestSecurityConfig 
     extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .antMatcher("/v1/**").authorizeRequests() 
       .antMatchers("/v1/admin/**").hasRole("admin") 
       .antMatchers("/v1/account/**").hasRole("admin") 
       .antMatchers("/v1/plant/**").access("hasRole('admin') or hasRole('dataProvider')") 
       .antMatchers("/v1/upload/**").access("hasRole('admin') or hasRole('dataProvider')") 
       .antMatchers("/v1/**").authenticated() 
      .and().httpBasic(); 
    } 
} 

@Configuration 
@Order(2) 
public static class UiSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring().antMatchers("/resources/**"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/account/**").hasRole("admin") 
       .antMatchers("/admin/**").hasRole("admin") 
       .antMatchers("/plant/**").access("hasRole('admin') or hasRole('dataProvider')") 
       .antMatchers("/upload/**").access("hasRole('admin') or hasRole('dataProvider')") 
       .anyRequest().authenticated() 
      .and().formLogin().loginPage("/login").permitAll(); 
    } 

} 

} 

任何人都可以解释为什么发生这种情况或有什么不对我的配置?

我看到这个配置的第二个问题是,虽然sec:authorize access = ... does not work,但是jsp标签sec:authorize url = ...不起作用。 在url = ...大小写的情况下,即使用户未被授权,也会始终显示内容。 我知道用户没有被授权,因为它应该已经被sec隐藏的链接:授权标签导致403 Forbidden。

对此的任何帮助非常感谢!

回答

4

我找到了解决这个明显错误的解决方法。 我加permitAll()上/登录/ **如下:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
     .authorizeRequests() 
      .antMatchers("/account/request/**").permitAll() 
      .antMatchers("/login/**").permitAll() 
      .antMatchers("/account/change_password/**").authenticated() 
      .antMatchers("/account/**").hasAuthority("admin") 
      .antMatchers("/admin/**").hasAuthority("admin") 
      .antMatchers("/plant/**").hasAnyAuthority("admin", "dataProvider") 
      .antMatchers("/upload/**").hasAnyAuthority("admin", "dataProvider") 
      .anyRequest().authenticated() 
     .and().formLogin().loginPage("/login").permitAll(); 
} 

回答我的问题的情况下,它可以帮助别人谁运行到这个bug。

+0

这解决了我的问题 - 但我仍然不确定是什么让重定向登录?注销 - 我没有在我的代码在任何地方......是一个春季安全的事情? –

+0

@wwarren很高兴帮助。当春季安全处理注销时,重定向到登录?注销以显示“您已被注销”的登录页面。信息。 – shawnim

2

取而代之的是:

.antMatchers("/login/**").permitAll() 

我认为更好的解决办法是这样的:相比时

http 
    .authorizeRequests() 
     .antMatchers("/account/request/**").permitAll() 
     .*antMatchers("/login").permitAll() 
     .antMatchers("/account/change_password/**").authenticated() 
     .antMatchers("/account/**").hasAuthority("admin") 
     .antMatchers("/admin/**").hasAuthority("admin") 
     .antMatchers("/plant/**").hasAnyAuthority("admin", "dataProvider") 
     .antMatchers("/upload/**").hasAnyAuthority("admin", "dataProvider") 
     .anyRequest().authenticated() 
    .and().formLogin().loginPage("/login").permitAll(). 
    .and().logout().logoutSuccessUrl("/login?logout").permitAll(); 

其原因是,在这种情况下permitAll(),URL模式有范围限制到"/login/**"