【问题标题】:Spring Security and RequestCache with Anonymous User匿名用户的 Spring Security 和 RequestCache
【发布时间】:2020-08-19 21:47:54
【问题描述】:

这是我的安全配置

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
            .requestCache().requestCache(new CustomRequestCache())
            .and().authorizeRequests()
            .antMatchers("/").permitAll()
            .anyRequest().hasAnyAuthority(Role.getAllRoles())
            .and().formLogin().loginPage(LOGIN_URL).permitAll()
                  .loginProcessingUrl(LOGIN_PROCESSING_URL)
            .failureUrl(LOGIN_FAILURE_URL)
            .successHandler(new SavedRequestAwareAuthenticationSuccessHandler())
            .and().logout().logoutSuccessUrl(LOGOUT_SUCCESS_URL);
}

问题是当我从 / 导航到受保护的 URL 时未调用 CustomRequestCache,因此 SavedRequestAwareAuthenticationSuccessHandler 在登录后不会重定向到请求的页面。

我认为这是因为 permitAll 创建了一个匿名用户。

我必须如何配置 Spring Security 才能使 SavedRequestAwareAuthenticationSuccessHandler 工作?

【问题讨论】:

  • 我尝试在没有自定义登录/注销 URL 的情况下重新创建它,它对我有用。我导航到/,然后尝试导航到/test,这会将我重定向到登录表单。登录后,我被重定向到/test

标签: java spring spring-boot spring-security


【解决方案1】:

作为答案发布,以便我可以包含我的代码。我尝试重新创建您的设置,这是我的安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final String THE_AUTHORITY = "ROLE_ONE";

    @Override
    protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user").password(passwordEncoder().encode("user")).authorities(THE_AUTHORITY);
    }

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.csrf().disable()
                .requestCache().requestCache(new CustomRequestCache())
                .and().authorizeRequests()
                .antMatchers("/").permitAll()
                .anyRequest().hasAnyAuthority(THE_AUTHORITY)
                .and().formLogin().loginPage("/login").permitAll()
                    .loginProcessingUrl("/login")
                    .failureUrl("/")
                .successHandler(new SavedRequestAwareAuthenticationSuccessHandler())
                .and().logout().logoutSuccessUrl("/");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

和我的控制器

@RestController
public class TestController {

    @GetMapping("/test")
    public String getTest() {
        return "You did it!";
    }

    @GetMapping("/")
    public String getRoot() {
        return "<a href=/test>Go to test</a>";
    }

    @GetMapping("/login")
    public String getLogin() {
        return "<form action=/login method=POST><input name=username /><input name=password /><input type=submit /></form>";
    }
}

我可以打开它,导航到/,单击指向/test 的链接,此时我被重定向到登录表单。登录后,我被重定向到/test

这是我的 CustomReuqestCache:

public class CustomRequestCache extends HttpSessionRequestCache {
    @Override
    public void saveRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        System.out.println("Saving request to " + httpServletRequest.getRequestURI());
        super.saveRequest(httpServletRequest, httpServletResponse);
    }

    @Override
    public HttpServletRequest getMatchingRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
         System.out.println("Returning request for " + httpServletRequest.getRequestURI());
         return super.getMatchingRequest(httpServletRequest, httpServletResponse);
    }
}

【讨论】:

  • 非常感谢您的示例,这确实有效!所以肯定还有另一个问题,因为我的案例中的 saveRequest 方法在 htting / 第一次之后没有被调用。
  • 我发现了错误。我正在使用的 Web 框架正在拦截请求,因此不使用 CustomRequestCache。感谢您的帮助
  • 很高兴你知道了!
猜你喜欢
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
  • 2014-08-09
  • 2019-06-03
  • 2015-06-22
  • 2016-04-15
  • 2016-04-27
  • 2018-12-08
相关资源
最近更新 更多