简体   繁体   English

spring-security login?logout重定向到登录

[英]spring-security login?logout redirects to login

I am using spring-security 3.2.0.RC2 with java config and two HttpSecurity configurations. 我使用spring-security 3.2.0.RC2与java配置和两个HttpSecurity配置。 One for REST API and one for UI. 一个用于REST API,一个用于UI。 When I post to /logout it redirects to /login?logout but then (incorrectly) redirects to /login. 当我发布到/ logout它重定向到/ login?logout然后(错误地)重定向到/ login。 When i enter username and password successfully I get redirected to login?logout and have to enter credentials a second time to get to the main page. 当我成功输入用户名和密码后,我被重定向到登录?注销并且必须再次输入凭证才能进入主页面。 So it seems like the permitAll for login is not being honored for login?logout. 因此,登录的permitAll似乎没有因登录而受到尊重?注销。

My security config looks like this: 我的安全配置如下所示:

@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();
    }

}

}

Can anyone explain why this is happening or what is wrong with my configuration? 谁能解释为什么会发生这种情况或者我的配置有什么问题?

A secondary problem that I see with this configuration is that the jsp tag sec:authorize url=... does not work although sec:authorize access=... does work. 我在这个配置中看到的第二个问题是jsp标签sec:authorize url = ...不起作用,尽管sec:authorize access = ...确实有效。 In the url=... case it always shows the content even if the user is not authorized. 在url = ...情况下,即使用户未获得授权,它也始终显示内容。 I know the user is not authorized becuase hitting the link that should have been hidden by the sec:authorize tag results in a 403 Forbidden. 我知道用户没有被授权,因为命中应该被sec:authorize标签隐藏的链接导致403 Forbidden。

Any help on this greatly appreciated! 对此的任何帮助非常感谢!

I found a workaround for this apparent bug. 我找到了这个明显错误的解决方法。 I added permitAll() on /login/** as follows: 我在/ 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();
}

Answering my own question in case it helps anyone else who runs into this bug. 回答我自己的问题,以防它遇到任何碰到这个bug的人。

Instead of this: 而不是这个:

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

I think the better solution would be this: 我认为更好的解决方案是:

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();

The reason being, url pattern for permitAll(), in this case has limited scope when compared to "/login/**" 原因是,与"/login/**"相比,permitAll()的url模式在这种情况下的范围有限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM