Spring安全性:多次登录url显示

时间:2016-11-30 05:54:33

标签: java spring-mvc spring-security

我有以下弹簧安全配置: -

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/footer").authenticated()
        .antMatchers("/index").authenticated()
        .anyRequest().permitAll()

        .and().formLogin()
            .loginPage("/login.html")

        .and().rememberMe().tokenValiditySeconds(2419200)  //valid for 4 weeks

        .and().httpBasic();

    //.and()
        //.requiresChannel()
            //.antMatchers("/").requiresSecure();
}

当我点击url“/ index”时,我看到一个正确的登录屏幕。

登录后,当我将浏览器中的URL更改为“/ footer”时,我再次看到登录屏幕。但是我之前已经登录了,这种行为是否正常?

我的要求是登录页面和页脚页面都需要进行身份验证。如果用户在登录前进入页脚页面,则应该提示他进行身份验证。 如果他已经是身份验证,那么应该向他显示页面。

2 个答案:

答案 0 :(得分:0)

更改这样的匹配器,它将根据您的需要工作。

http.authorizeRequests()
            .antMatchers("/footer", "/index").authenticated()
            //.antMatchers("/footer", "/index").access("hasRole('ADMIN')")  by this you can allow a particular role to access this url
            .and().formLogin().loginPage("/login.html")
            .and().rememberMe().tokenValiditySeconds(2419200) //valid for 4 weeks
            .and().httpBasic();

这里你可以在authenticated()的地方使用注释行。

答案 1 :(得分:0)

只需放入antMatchers(" / footer"," / index")。authenticated()。这符合您的要求。