spring-security 登录?注销重定向到登录

sha*_*nim 5 java redirect spring spring-security logout

我正在使用带有 java 配置和两个 HttpSecurity 配置的 spring-security 3.2.0.RC2。一种用于 REST API,一种用于 UI。当我发布到 /logout 时,它会重定向到 /login?logout 但随后(错误地)重定向到 /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();
    }

}

}
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么会发生这种情况或者我的配置有什么问题?

我在此配置中看到的第二个问题是 jsp 标记 sec:authorize url=... 不起作用,尽管 sec:authorize access=... 起作用。在 url=... 的情况下,即使用户未经授权,它也始终显示内容。我知道用户未获得授权,因为点击本应被 sec:authorize 标记隐藏的链接会导致 403 Forbidden。

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

sha*_*nim 5

我找到了解决这个明显错误的方法。我在 /login/** 上添加了 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();
}
Run Code Online (Sandbox Code Playgroud)

回答我自己的问题,以防它对遇到此错误的其他人有所帮助。