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

标签 java spring redirect 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();
    }

}

}

谁能解释为什么会这样或者我的配置有什么问题?

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

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

最佳答案

我找到了解决这个明显错误的方法。 我在/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();
}

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

关于java - spring-security 登录?注销重定向到登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20532737/

相关文章:

java - Spring Boot RestTemplate 交换 400 错误请求

java - Spring 重定向不起作用,只显示重定向 :/url in browser

JavaScript 构建选项/工具

java - CDI 通用 DAO 注入(inject)

java - 如何加快处理多个 HTTP 请求

spring - 使用包含单引号的字符串键进行 EL 映射查找

java - Spring:如何用常量值替换构造函数参数?

java - FTP 客户端 - 列表文件

java - 如何识别 PMD 规则中的换行符

c - 从 C 中的输入重定向读取整数